mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:54:41 +01:00
Change implicit import handling to be namespace-oriented. (#4089)
This refactors how the implicit import is handled in order to retain more name scope information. As a consequence, private access control works better between api files and implementation files. Note though that this will also be essential for name poisoning between the API and implementation, as discussed in #3763. In implementing this, I ran into a couple issues with namespaces that I think point to flaws in their handling. I've fixed some and added a TODO for the biggest issue (in check.cpp line 281-288), which relates to the handling of namespaces of direct imports which are first evaluated indirectly.
This commit is contained in:
@@ -36,14 +36,10 @@ private fn Forward();
|
||||
// Test files
|
||||
// ============================================================================
|
||||
|
||||
// --- fail_todo_def.impl.carbon
|
||||
// --- def.impl.carbon
|
||||
|
||||
impl package Test library "def";
|
||||
|
||||
// CHECK:STDERR: fail_todo_def.impl.carbon:[[@LINE+4]]:13: ERROR: Name `Def` not found.
|
||||
// CHECK:STDERR: var f: () = Def();
|
||||
// CHECK:STDERR: ^~~
|
||||
// CHECK:STDERR:
|
||||
var f: () = Def();
|
||||
|
||||
// --- fail_local_def.carbon
|
||||
@@ -70,14 +66,10 @@ import Test library "def";
|
||||
// CHECK:STDERR:
|
||||
var f: () = Test.Def();
|
||||
|
||||
// --- fail_todo_forward_with_def.impl.carbon
|
||||
// --- forward_with_def.impl.carbon
|
||||
|
||||
impl package Test library "forward_with_def";
|
||||
|
||||
// CHECK:STDERR: fail_todo_forward_with_def.impl.carbon:[[@LINE+4]]:13: ERROR: Name `ForwardWithDef` not found.
|
||||
// CHECK:STDERR: var f: () = ForwardWithDef();
|
||||
// CHECK:STDERR: ^~~~~~~~~~~~~~
|
||||
// CHECK:STDERR:
|
||||
var f: () = ForwardWithDef();
|
||||
|
||||
// --- fail_local_forward_with_def.carbon
|
||||
@@ -104,14 +96,10 @@ import Test library "forward_with_def";
|
||||
// CHECK:STDERR:
|
||||
var f: () = Test.ForwardWithDef();
|
||||
|
||||
// --- fail_todo_forward.impl.carbon
|
||||
// --- forward.impl.carbon
|
||||
|
||||
impl package Test library "forward";
|
||||
|
||||
// CHECK:STDERR: fail_todo_forward.impl.carbon:[[@LINE+4]]:13: ERROR: Name `Forward` not found.
|
||||
// CHECK:STDERR: var f: () = Forward();
|
||||
// CHECK:STDERR: ^~~~~~~
|
||||
// CHECK:STDERR:
|
||||
var f: () = Forward();
|
||||
|
||||
fn Forward() {}
|
||||
@@ -211,28 +199,35 @@ private fn Redecl() {}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @Forward();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- fail_todo_def.impl.carbon
|
||||
// CHECK:STDOUT: --- def.impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
// CHECK:STDOUT: %.1: type = tuple_type () [template]
|
||||
// CHECK:STDOUT: %Def.type: type = fn_type @Def [template]
|
||||
// CHECK:STDOUT: %Def: %Def.type = struct_value () [template]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .Def [private] = %import_ref
|
||||
// CHECK:STDOUT: .Core = %Core
|
||||
// CHECK:STDOUT: .f = %f
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %import_ref: %Def.type = import_ref ir0, inst+2, loaded [template = constants.%Def]
|
||||
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
|
||||
// CHECK:STDOUT: %.loc8_9.1: %.1 = tuple_literal ()
|
||||
// CHECK:STDOUT: %.loc8_9.2: type = converted %.loc8_9.1, constants.%.1 [template = constants.%.1]
|
||||
// CHECK:STDOUT: %.loc4_9.1: %.1 = tuple_literal ()
|
||||
// CHECK:STDOUT: %.loc4_9.2: type = converted %.loc4_9.1, constants.%.1 [template = constants.%.1]
|
||||
// CHECK:STDOUT: %f.var: ref %.1 = var f
|
||||
// CHECK:STDOUT: %f: ref %.1 = bind_name f, %f.var
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @Def();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @__global_init() {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: %Def.ref: <error> = name_ref Def, <error> [template = <error>]
|
||||
// CHECK:STDOUT: assign file.%f.var, <error>
|
||||
// CHECK:STDOUT: %Def.ref: %Def.type = name_ref Def, file.%import_ref [template = constants.%Def]
|
||||
// CHECK:STDOUT: %Def.call: init %.1 = call %Def.ref()
|
||||
// CHECK:STDOUT: assign file.%f.var, %Def.call
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
@@ -289,28 +284,35 @@ private fn Redecl() {}
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- fail_todo_forward_with_def.impl.carbon
|
||||
// CHECK:STDOUT: --- forward_with_def.impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
// CHECK:STDOUT: %.1: type = tuple_type () [template]
|
||||
// CHECK:STDOUT: %ForwardWithDef.type: type = fn_type @ForwardWithDef [template]
|
||||
// CHECK:STDOUT: %ForwardWithDef: %ForwardWithDef.type = struct_value () [template]
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .ForwardWithDef [private] = %import_ref
|
||||
// CHECK:STDOUT: .Core = %Core
|
||||
// CHECK:STDOUT: .f = %f
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %import_ref: %ForwardWithDef.type = import_ref ir0, inst+2, loaded [template = constants.%ForwardWithDef]
|
||||
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
|
||||
// CHECK:STDOUT: %.loc8_9.1: %.1 = tuple_literal ()
|
||||
// CHECK:STDOUT: %.loc8_9.2: type = converted %.loc8_9.1, constants.%.1 [template = constants.%.1]
|
||||
// CHECK:STDOUT: %.loc4_9.1: %.1 = tuple_literal ()
|
||||
// CHECK:STDOUT: %.loc4_9.2: type = converted %.loc4_9.1, constants.%.1 [template = constants.%.1]
|
||||
// CHECK:STDOUT: %f.var: ref %.1 = var f
|
||||
// CHECK:STDOUT: %f: ref %.1 = bind_name f, %f.var
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @ForwardWithDef();
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @__global_init() {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: %ForwardWithDef.ref: <error> = name_ref ForwardWithDef, <error> [template = <error>]
|
||||
// CHECK:STDOUT: assign file.%f.var, <error>
|
||||
// CHECK:STDOUT: %ForwardWithDef.ref: %ForwardWithDef.type = name_ref ForwardWithDef, file.%import_ref [template = constants.%ForwardWithDef]
|
||||
// CHECK:STDOUT: %ForwardWithDef.call: init %.1 = call %ForwardWithDef.ref()
|
||||
// CHECK:STDOUT: assign file.%f.var, %ForwardWithDef.call
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
@@ -367,7 +369,7 @@ private fn Redecl() {}
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: --- fail_todo_forward.impl.carbon
|
||||
// CHECK:STDOUT: --- forward.impl.carbon
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: constants {
|
||||
// CHECK:STDOUT: %.1: type = tuple_type () [template]
|
||||
@@ -377,13 +379,14 @@ private fn Redecl() {}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: file {
|
||||
// CHECK:STDOUT: package: <namespace> = namespace [template] {
|
||||
// CHECK:STDOUT: .Forward [private] = %Forward.decl
|
||||
// CHECK:STDOUT: .Core = %Core
|
||||
// CHECK:STDOUT: .f = %f
|
||||
// CHECK:STDOUT: .Forward = %Forward.decl
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT: %import_ref: %Forward.type = import_ref ir0, inst+2, loaded [template = constants.%Forward]
|
||||
// CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
|
||||
// CHECK:STDOUT: %.loc8_9.1: %.1 = tuple_literal ()
|
||||
// CHECK:STDOUT: %.loc8_9.2: type = converted %.loc8_9.1, constants.%.1 [template = constants.%.1]
|
||||
// CHECK:STDOUT: %.loc4_9.1: %.1 = tuple_literal ()
|
||||
// CHECK:STDOUT: %.loc4_9.2: type = converted %.loc4_9.1, constants.%.1 [template = constants.%.1]
|
||||
// CHECK:STDOUT: %f.var: ref %.1 = var f
|
||||
// CHECK:STDOUT: %f: ref %.1 = bind_name f, %f.var
|
||||
// CHECK:STDOUT: %Forward.decl: %Forward.type = fn_decl @Forward [template = constants.%Forward] {}
|
||||
@@ -396,8 +399,9 @@ private fn Redecl() {}
|
||||
// CHECK:STDOUT:
|
||||
// CHECK:STDOUT: fn @__global_init() {
|
||||
// CHECK:STDOUT: !entry:
|
||||
// CHECK:STDOUT: %Forward.ref: <error> = name_ref Forward, <error> [template = <error>]
|
||||
// CHECK:STDOUT: assign file.%f.var, <error>
|
||||
// CHECK:STDOUT: %Forward.ref: %Forward.type = name_ref Forward, file.%import_ref [template = constants.%Forward]
|
||||
// CHECK:STDOUT: %Forward.call: init %.1 = call %Forward.ref()
|
||||
// CHECK:STDOUT: assign file.%f.var, %Forward.call
|
||||
// CHECK:STDOUT: return
|
||||
// CHECK:STDOUT: }
|
||||
// CHECK:STDOUT:
|
||||
|
||||
Reference in New Issue
Block a user