-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
stage2: detect ambiguous decl references #8921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2355,6 +2355,7 @@ fn varDecl( | |
.name = ident_name, | ||
.ptr = init_scope.rl_ptr, | ||
.token_src = name_token, | ||
.is_comptime = true, | ||
}; | ||
return &sub_scope.base; | ||
}, | ||
|
@@ -2410,6 +2411,7 @@ fn varDecl( | |
.name = ident_name, | ||
.ptr = var_data.alloc, | ||
.token_src = name_token, | ||
.is_comptime = is_comptime, | ||
}; | ||
return &sub_scope.base; | ||
}, | ||
|
@@ -3354,7 +3356,7 @@ fn structDeclInner( | |
}; | ||
defer block_scope.instructions.deinit(gpa); | ||
|
||
var namespace: Scope.Namespace = .{ .parent = &gz.base }; | ||
var namespace: Scope.Namespace = .{ .parent = scope }; | ||
defer namespace.decls.deinit(gpa); | ||
|
||
var wip_decls: WipDecls = .{}; | ||
|
@@ -5347,6 +5349,7 @@ fn forExpr( | |
.name = index_name, | ||
.ptr = index_ptr, | ||
.token_src = index_token, | ||
.is_comptime = parent_gz.force_comptime, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Btw @andrewrk, @SpexGuy What should the correct behavior be for pub fn main() void {
comptime var i = 0;
inline while (i < 5) : (i+=1) {
const S = struct {
fn print() void {
std.debug.print("loop {d}\n", .{i});
}
};
S.print();
}
} Stage1 prints
but I think most people would assume it to print
|
||
}; | ||
break :blk &index_scope.base; | ||
}; | ||
|
@@ -6072,9 +6075,16 @@ fn identifier( | |
const name_str_index = try astgen.identAsString(ident_token); | ||
{ | ||
var s = scope; | ||
var found_already: ?ast.Node.Index = null; // we have found a decl with the same name already | ||
var hit_namespace = false; | ||
while (true) switch (s.tag) { | ||
.local_val => { | ||
const local_val = s.cast(Scope.LocalVal).?; | ||
if (hit_namespace) { | ||
// captures of non-locals need to be emitted as decl_val or decl_ref | ||
// This *might* be capturable depending on if it is comptime known | ||
break; | ||
} | ||
if (local_val.name == name_str_index) { | ||
return rvalue(gz, scope, rl, local_val.inst, ident); | ||
} | ||
|
@@ -6083,6 +6093,15 @@ fn identifier( | |
.local_ptr => { | ||
const local_ptr = s.cast(Scope.LocalPtr).?; | ||
if (local_ptr.name == name_str_index) { | ||
if (hit_namespace) { | ||
if (local_ptr.is_comptime) | ||
break | ||
else | ||
return astgen.failNodeNotes(ident, "'{s}' not accessible from inner function", .{ident_name}, &.{ | ||
try astgen.errNoteTok(local_ptr.token_src, "declared here", .{}), | ||
// TODO add crossed function definition here note. | ||
}); | ||
} | ||
switch (rl) { | ||
.ref, .none_or_ref => return local_ptr.ptr, | ||
else => { | ||
|
@@ -6095,7 +6114,22 @@ fn identifier( | |
}, | ||
.gen_zir => s = s.cast(GenZir).?.parent, | ||
.defer_normal, .defer_error => s = s.cast(Scope.Defer).?.parent, | ||
.namespace, .top => break, // TODO look for ambiguous references to decls | ||
// look for ambiguous references to decls | ||
.namespace => { | ||
g-w1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const ns = s.cast(Scope.Namespace).?; | ||
if (ns.decls.get(name_str_index)) |i| { | ||
if (found_already) |f| | ||
return astgen.failNodeNotes(ident, "ambiguous reference", .{}, &.{ | ||
try astgen.errNoteNode(i, "declared here", .{}), | ||
try astgen.errNoteNode(f, "also declared here", .{}), | ||
}) | ||
else | ||
found_already = i; | ||
} | ||
hit_namespace = true; | ||
s = ns.parent; | ||
}, | ||
.top => break, | ||
}; | ||
} | ||
|
||
|
@@ -8044,6 +8078,7 @@ const Scope = struct { | |
token_src: ast.TokenIndex, | ||
/// String table index. | ||
name: u32, | ||
is_comptime: bool, | ||
}; | ||
|
||
const Defer = struct { | ||
|
Uh oh!
There was an error while loading. Please reload this page.