Skip to content

Commit 075b3ab

Browse files
authored
Implementation of unformed state for global variables. (carbon-language#1571)
- Allocate UninitializedValue on heap when a VariableDeclaration does not have initialization. - Added test cases.
1 parent fc0d7a8 commit 075b3ab

11 files changed

+89
-6
lines changed

explorer/interpreter/interpreter.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,9 @@ auto Interpreter::StepDeclaration() -> ErrorOr<Success> {
16271627
return todo_.FinishAction();
16281628
}
16291629
} else {
1630+
Nonnull<const Value*> v =
1631+
arena_->New<UninitializedValue>(&var_decl.binding().value());
1632+
todo_.Initialize(&var_decl.binding(), v);
16301633
return todo_.FinishAction();
16311634
}
16321635
}

explorer/testdata/uninitialized/fail_uninitialized_return.carbon renamed to explorer/testdata/uninitialized/fail_global_uninitialized.carbon

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
package ExplorerTest api;
1212

13+
var x: i32;
14+
1315
fn Main() -> i32 {
14-
var x: i32;
15-
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_uninitialized_return.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
16+
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_global_uninitialized.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
1617
return x;
1718
}

explorer/testdata/uninitialized/fail_uninitialized_assign.carbon renamed to explorer/testdata/uninitialized/fail_local_uninitialized_assign.carbon

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ package ExplorerTest api;
1313
fn Main() -> i32 {
1414
var x: i32;
1515
var y: i32;
16-
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_uninitialized_assign.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
16+
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_local_uninitialized_assign.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
1717
y = x;
1818
return y;
1919
}

explorer/testdata/uninitialized/fail_uninitialized_init.carbon renamed to explorer/testdata/uninitialized/fail_local_uninitialized_init.carbon

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ package ExplorerTest api;
1212

1313
fn Main() -> i32 {
1414
var x: i32;
15-
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_uninitialized_init.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
15+
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_local_uninitialized_init.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
1616
var y: i32 = x;
1717
return x;
1818
}

explorer/testdata/uninitialized/fail_uninitialized_param.carbon renamed to explorer/testdata/uninitialized/fail_local_uninitialized_param.carbon

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ fn AddInt(a: i32, b: i32) -> auto {
1616

1717
fn Main() -> i32 {
1818
var x: i32;
19-
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_uninitialized_param.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
19+
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_local_uninitialized_param.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
2020
return AddInt(x, 2);
2121
}

explorer/testdata/uninitialized/fail_uninitialized_pattern.carbon renamed to explorer/testdata/uninitialized/fail_local_uninitialized_pattern.carbon

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ package ExplorerTest api;
1313
fn Main() -> i32 {
1414
var (x: i32, y: i32);
1515
x = 1;
16-
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_uninitialized_pattern.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<y>>
16+
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_local_uninitialized_pattern.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<y>>
1717
return y;
1818
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// RUN: %{not} %{explorer} %s 2>&1 | \
6+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
7+
// RUN: %{not} %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
8+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
9+
// AUTOUPDATE: %{explorer} %s
10+
11+
package ExplorerTest api;
12+
13+
fn Main() -> i32 {
14+
var x: i32;
15+
// CHECK: RUNTIME ERROR: {{.*}}/explorer/testdata/uninitialized/fail_local_uninitialized_return.carbon:[[@LINE+1]]: undefined behavior: access to uninitialized value Uninit<Placeholder<x>>
16+
return x;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// RUN: %{explorer} %s 2>&1 | \
6+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
7+
// RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
8+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
9+
// AUTOUPDATE: %{explorer} %s
10+
// CHECK: result: 1
11+
12+
package ExplorerTest api;
13+
14+
var x: i32;
15+
16+
fn Main() -> i32 {
17+
x = 1;
18+
return x;
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// RUN: %{explorer} %s 2>&1 | \
6+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
7+
// RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
8+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
9+
// AUTOUPDATE: %{explorer} %s
10+
// CHECK: result: 1
11+
12+
package ExplorerTest api;
13+
14+
var x: i32;
15+
16+
fn Main() -> i32 {
17+
return 1;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
2+
// Exceptions. See /LICENSE for license information.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
//
5+
// RUN: %{explorer} %s 2>&1 | \
6+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
7+
// RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
8+
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
9+
// AUTOUPDATE: %{explorer} %s
10+
// CHECK: result: 42
11+
12+
package ExplorerTest api;
13+
14+
// `y` is unformed here.
15+
var y: i32;
16+
17+
fn AssignIntTo(x: i32, destination: i32*) {
18+
*destination = x;
19+
}
20+
21+
fn Main() -> i32 {
22+
AssignIntTo(42, &y);
23+
// `y` is fully formed and usable.
24+
return y;
25+
}

0 commit comments

Comments
 (0)