authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 17:44:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-11 17:44:19-07:00
log7873e4f5889f9e1b4d5b5f62d7701d7914b64ab1
tree8ee9468285c059f3e5c09ba26d5ca99ba4eadd03
parentcbbc7cc8b1edeeae1715248a5d13304027698367

stage2: lookupIdentifier can return error.AnalysisFailed

This avoids causing false positive compile errors when, for example, a file had ZIR errors, and then code tried to look up a public decl from the failed file.

2 files changed, 15 insertions(+), 7 deletions(-)

src/Module.zig+12-4
......@@ -3096,7 +3096,6 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool {
30963096 try mod.analyzeExport(&block_scope.base, export_src, mem.spanZ(decl.name), decl);
30973097 }
30983098
3099
31003099 return type_changed;
31013100 }
31023101}
......@@ -3881,10 +3880,14 @@ pub fn getNextAnonNameIndex(mod: *Module) usize {
38813880/// This looks up a bare identifier in the given scope. This will walk up the tree of namespaces
38823881/// in scope and check each one for the identifier.
38833882/// TODO emit a compile error if more than one decl would be matched.
3884pub fn lookupIdentifier(mod: *Module, scope: *Scope, ident_name: []const u8) ?*Decl {
3883pub fn lookupIdentifier(
3884 mod: *Module,
3885 scope: *Scope,
3886 ident_name: []const u8,
3887) error{AnalysisFail}!?*Decl {
38853888 var namespace = scope.namespace();
38863889 while (true) {
3887 if (mod.lookupInNamespace(namespace, ident_name, false)) |decl| {
3890 if (try mod.lookupInNamespace(namespace, ident_name, false)) |decl| {
38883891 return decl;
38893892 }
38903893 namespace = namespace.parent orelse break;
......@@ -3899,7 +3902,12 @@ pub fn lookupInNamespace(
38993902 namespace: *Scope.Namespace,
39003903 ident_name: []const u8,
39013904 only_pub_usingnamespaces: bool,
3902) ?*Decl {
3905) error{AnalysisFail}!?*Decl {
3906 const owner_decl = namespace.getDecl();
3907 if (owner_decl.analysis == .file_failure) {
3908 return error.AnalysisFail;
3909 }
3910
39033911 // TODO the decl doing the looking up needs to create a decl dependency
39043912 // TODO implement usingnamespace
39053913 if (namespace.decls.get(ident_name)) |decl| {
src/Sema.zig+3-3
......@@ -2044,7 +2044,7 @@ fn zirDeclVal(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError
20442044
20452045fn lookupIdentifier(sema: *Sema, block: *Scope.Block, src: LazySrcLoc, name: []const u8) !*Decl {
20462046 const mod = sema.mod;
2047 const decl = mod.lookupIdentifier(&sema.namespace.base, name) orelse {
2047 const decl = (try mod.lookupIdentifier(&sema.namespace.base, name)) orelse {
20482048 // TODO insert a "dependency on the non-existence of a decl" here to make this
20492049 // compile error go away when the decl is introduced. This data should be in a global
20502050 // sparse map since it is only relevant when a compile error occurs.
......@@ -4359,7 +4359,7 @@ fn zirHasDecl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError
43594359 "expected struct, enum, union, or opaque, found '{}'",
43604360 .{container_type},
43614361 );
4362 if (mod.lookupInNamespace(namespace, decl_name, true)) |decl| {
4362 if (try mod.lookupInNamespace(namespace, decl_name, true)) |decl| {
43634363 if (decl.is_pub or decl.namespace.file_scope == block.base.namespace().file_scope) {
43644364 return mod.constBool(arena, src, true);
43654365 }
......@@ -6279,7 +6279,7 @@ fn analyzeNamespaceLookup(
62796279) InnerError!?*Inst {
62806280 const mod = sema.mod;
62816281 const gpa = sema.gpa;
6282 if (mod.lookupInNamespace(namespace, decl_name, true)) |decl| {
6282 if (try mod.lookupInNamespace(namespace, decl_name, true)) |decl| {
62836283 if (!decl.is_pub and decl.namespace.file_scope != block.getFileScope()) {
62846284 const msg = msg: {
62856285 const msg = try mod.errMsg(&block.base, src, "'{s}' is not marked 'pub'", .{