Handle block scopes in compile-time binding check (#4379)

This fixes a fuzzer-found crash when a compile-time binding occurs
inside a block (which is represented by an invalid inst ID).
This commit is contained in:
Geoff Romer
2024-10-07 22:53:22 +00:00
committed by GitHub
parent 284b14981d
commit 6439f9065d
2 changed files with 170 additions and 7 deletions
+14 -6
View File
@@ -219,12 +219,20 @@ auto HandleParseNode(Context& context,
bool is_generic = true;
if (context.decl_introducer_state_stack().innermost().kind ==
Lex::TokenKind::Let) {
auto scope_inst = context.insts().Get(context.scope_stack().PeekInstId());
if (!scope_inst.Is<SemIR::InterfaceDecl>() &&
!scope_inst.Is<SemIR::FunctionDecl>()) {
context.TODO(node_id,
"`let` compile time binding outside function or interface");
is_generic = false;
// Disallow `let` outside of function and interface definitions.
// TODO: find a less brittle way of doing this. An invalid scope_inst_id
// can represent a block scope, but is also used for other kinds of scopes
// that aren't necessarily part of an interface or function decl.
auto scope_inst_id = context.scope_stack().PeekInstId();
if (scope_inst_id.is_valid()) {
auto scope_inst = context.insts().Get(scope_inst_id);
if (!scope_inst.Is<SemIR::InterfaceDecl>() &&
!scope_inst.Is<SemIR::FunctionDecl>()) {
context.TODO(
node_id,
"`let` compile time binding outside function or interface");
is_generic = false;
}
}
}