Files
carbon-lang/toolchain/check/testdata/var/shadowing.carbon
T
Jon Ross-Perkins 4ab42c0712 Fix for MakeUnqualifiedName conflicting with a namespace (#3707)
Unqualified names don't handle scopes the way that typical names do, so
a name conflict with a namespace needs to be handled specially. I'm
still favoring keeping code close as much as possible, particularly
since long-term this syntax will probably shift to be more consistent.
For now I'm just flagging when we shouldn't push scopes, so that
MakeUnqualifiedName doesn't need to clean up.

Note, a different approach would basically be:

```
PushScopeAndStartName
ApplyNameQualifierTo
result = decl_name_stack_.back();
decl_name_stack_.back().state = NameContext::State::Finished;
PopScope
return result;
```

But that approach feels worse to me, due to the additional stack
manipulations and the need to duplicate some of the FinishName logic
just to be able to pop the scope that didn't really need to be added.
2024-02-20 18:43:57 +00:00

66 lines
2.3 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
namespace NS;
fn Main() {
var NS: i32 = 0;
NS = 1;
var x: i32 = 0;
if (true) {
var x: i32 = 0;
// TODO: We should reject this use of the shadowed variable `x`.
x = 1;
}
}
// CHECK:STDOUT: --- shadowing.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %.1: i32 = int_literal 0 [template]
// CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
// CHECK:STDOUT: %.3: bool = bool_literal true [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace {.NS = %.loc7, .Main = %Main} [template]
// CHECK:STDOUT: %.loc7: <namespace> = namespace {} [template]
// CHECK:STDOUT: %Main: <function> = fn_decl @Main [template]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @Main() {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %NS.var: ref i32 = var NS
// CHECK:STDOUT: %NS: ref i32 = bind_name NS, %NS.var
// CHECK:STDOUT: %.loc10: i32 = int_literal 0 [template = constants.%.1]
// CHECK:STDOUT: assign %NS.var, %.loc10
// CHECK:STDOUT: %NS.ref: ref i32 = name_ref NS, %NS
// CHECK:STDOUT: %.loc11: i32 = int_literal 1 [template = constants.%.2]
// CHECK:STDOUT: assign %NS.ref, %.loc11
// CHECK:STDOUT: %x.var.loc13: ref i32 = var x
// CHECK:STDOUT: %x.loc13: ref i32 = bind_name x, %x.var.loc13
// CHECK:STDOUT: %.loc13: i32 = int_literal 0 [template = constants.%.1]
// CHECK:STDOUT: assign %x.var.loc13, %.loc13
// CHECK:STDOUT: %.loc14: bool = bool_literal true [template = constants.%.3]
// CHECK:STDOUT: if %.loc14 br !if.then else br !if.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.then:
// CHECK:STDOUT: %x.var.loc15: ref i32 = var x
// CHECK:STDOUT: %x.loc15: ref i32 = bind_name x, %x.var.loc15
// CHECK:STDOUT: %.loc15: i32 = int_literal 0 [template = constants.%.1]
// CHECK:STDOUT: assign %x.var.loc15, %.loc15
// CHECK:STDOUT: %x.ref: ref i32 = name_ref x, %x.loc15
// CHECK:STDOUT: %.loc18: i32 = int_literal 1 [template = constants.%.2]
// CHECK:STDOUT: assign %x.ref, %.loc18
// CHECK:STDOUT: br !if.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.else:
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: