Handle references to global lets and enclosing locals. (#8061)

When building an expression referencing the result of name lookup, if
the lookup result is non-constant then check we can access it from the
current scope.

* If it is a global, then use a new `GlobalRef` instruction to get the
value of that global. In lowering, create global variables to hold the
values of global `let` bindings so that `GlobalRef` can access those
values.
* If it is a local, walk the lexical scopes between it and the point of
use and ensure it is usable through from all of them. Error if there's
an intervening declarative scope such as a class, add a TODO to handle
captures for intervening functions, and allow `typeof(...)` to name
things it can't read the value of since it's unevaluated.

Global `let`s (which includes `let`s at class scope) now also have their
initializers collected into the global inits block. If the initializer
is non-constant, the wrapper binding also lives in the global inits
block; otherwise it continues to live in the file block and the variable
emitted for it is a global constant.

Assisted-by: Gemini and Claude via Antigravity
This commit is contained in:
Richard Smith authored and GitHub committed 2026-11-06 02:53:03 +00:00
1 parent bb6486b553
commit d50576ca16
94 files changed
+3394 -1293

No files matched your search

+17 -25
View File
@@ -6,6 +6,7 @@
#include "toolchain/check/context.h"
#include "toolchain/check/convert.h"
#include "toolchain/check/eval.h"
#include "toolchain/check/full_pattern_stack.h"
#include "toolchain/check/handle.h"
#include "toolchain/check/inst.h"
#include "toolchain/check/pattern.h"
@@ -115,19 +116,19 @@ auto HandleParseNode(Context& context, Parse::TuplePatternId node_id) -> bool {
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::TuplePatternStart>();
if (InNonStaticFieldDecl(context)) {
CARBON_DIAGNOSTIC(FieldWithTuplePattern, Error,
"tuple pattern in field declaration");
context.emitter().Emit(node_id, FieldWithTuplePattern);
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
BeginExprRegionForPattern(context);
return true;
}
const auto& inst_block = context.inst_blocks().Get(refs_id);
llvm::SmallVector<SemIR::InstId> type_inst_ids;
type_inst_ids.reserve(inst_block.size());
for (auto inst : inst_block) {
if (InNonStaticFieldDecl(context)) {
CARBON_DIAGNOSTIC(FieldWithTuplePattern, Error,
"found tuple pattern in class `var` decl");
context.emitter().Emit(LocIdForDiagnostics::TokenOnly(node_id),
FieldWithTuplePattern);
return false;
}
auto type_id = ExtractScrutineeType(context.sem_ir(),
context.insts().Get(inst).type_id());
type_inst_ids.push_back(context.types().GetTypeInstId(type_id));
@@ -153,20 +154,6 @@ auto HandleParseNode(Context& context, Parse::StructPatternId node_id) -> bool {
EndExprRegionForPattern(context, context.node_stack());
}
// TODO: Reject StructPatterns in classes during Parse, or treat patterns in a
// class scope as patterns and generate the FieldDecl insts in a later
// pattern-matching step.
if (context.scope_stack().TryGetCurrentScopeAs<SemIR::ClassDecl>()) {
bool is_var = context.full_pattern_stack().IsCurrentKindClassScopeVarDecl();
CARBON_DIAGNOSTIC(
FieldWithStructPattern, Error,
"found struct pattern in class member {0:var|let} declaration",
Diagnostics::BoolAsSelect);
context.emitter().Emit(node_id, FieldWithStructPattern, is_var);
return false;
}
if (!context.node_stack().PeekIs(Parse::NodeCategory::MemberName)) {
// Remove the last parameter from the node stack before collecting names.
context.param_and_arg_refs_stack().EndNoPop(
@@ -184,8 +171,13 @@ auto HandleParseNode(Context& context, Parse::StructPatternId node_id) -> bool {
context.node_stack()
.PopAndDiscardSoloNodeId<Parse::NodeKind::StructPatternStart>();
if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
StructKind::StructPattern)) {
if (InNonStaticFieldDecl(context)) {
CARBON_DIAGNOSTIC(FieldWithStructPattern, Error,
"struct pattern in field declaration");
context.emitter().Emit(node_id, FieldWithStructPattern);
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
} else if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
StructKind::StructPattern)) {
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
} else {
auto type_id = GetPatternType(