Skip to content

Don't wrap an ErrorInst as a subpattern of another pattern #5542

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

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions toolchain/check/handle_let_and_var.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "toolchain/diagnostics/format_providers.h"
#include "toolchain/lex/token_kind.h"
#include "toolchain/parse/node_kind.h"
#include "toolchain/parse/typed_nodes.h"
#include "toolchain/sem_ir/ids.h"
#include "toolchain/sem_ir/inst.h"
#include "toolchain/sem_ir/name_scope.h"
Expand Down Expand Up @@ -143,6 +144,11 @@ auto HandleParseNode(Context& context, Parse::VariablePatternId node_id)
auto subpattern_id = context.node_stack().PopPattern();
auto type_id = context.insts().Get(subpattern_id).type_id();

if (subpattern_id == SemIR::ErrorInst::InstId) {
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
return true;
}

// In a parameter list, a `var` pattern is always a single `Call` parameter,
// even if it contains multiple binding patterns.
switch (context.full_pattern_stack().CurrentKind()) {
Expand Down Expand Up @@ -369,7 +375,8 @@ auto HandleParseNode(Context& context, Parse::LetDeclId node_id) -> bool {
return true;
}

auto HandleParseNode(Context& context, Parse::VariableDeclId node_id) -> bool {
auto HandleParseNode(Context& context, Parse::VariableDeclId /*node_id*/)
-> bool {
auto decl_info =
HandleDecl<Lex::TokenKind::Var, Parse::NodeKind::VariableIntroducer,
Parse::NodeKind::VariableInitializer>(context);
Expand All @@ -378,13 +385,6 @@ auto HandleParseNode(Context& context, Parse::VariableDeclId node_id) -> bool {
context, decl_info.introducer,
KeywordModifierSet::Access | KeywordModifierSet::Returned);

if (context.scope_stack().GetCurrentScopeAs<SemIR::InterfaceDecl>()) {
CARBON_DIAGNOSTIC(VarInInterfaceDecl, Error,
"`var` declaration in interface");
context.emitter().Emit(node_id, VarInInterfaceDecl);
return true;
}

LocalPatternMatch(context, decl_info.pattern_id, decl_info.init_id);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ fn G() {
// CHECK:STDOUT: constants {
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @F(%.param: <error>);
// CHECK:STDOUT: fn @F();
// CHECK:STDOUT:
// CHECK:STDOUT: --- var_self.carbon
// CHECK:STDOUT:
Expand Down
55 changes: 26 additions & 29 deletions toolchain/check/testdata/var/no_prelude/fail_in_interface.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,40 @@
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// EXTRA-ARGS: --dump-sem-ir-ranges=only
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/var/no_prelude/fail_in_interface.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/var/no_prelude/fail_in_interface.carbon

interface I {
// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+8]]:7: error: found runtime binding pattern in associated constant declaration; expected a `:!` binding [ExpectedSymbolicBindingInAssociatedConstant]
// CHECK:STDERR: var v: ();
// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+4]]:11: error: found runtime binding pattern in associated constant declaration; expected a `:!` binding [ExpectedSymbolicBindingInAssociatedConstant]
// CHECK:STDERR: let var a: ();
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
let var a: ();

// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+4]]:7: error: found runtime binding pattern in associated constant declaration; expected a `:!` binding [ExpectedSymbolicBindingInAssociatedConstant]
// CHECK:STDERR: var b: ();
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+4]]:3: error: `var` declaration in interface [VarInInterfaceDecl]
// CHECK:STDERR: var v: ();
// CHECK:STDERR: ^~~~~~~~~~
var b: ();

// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+8]]:11: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo]
// CHECK:STDERR: let var c:! ();
// CHECK:STDERR: ^~~~~~
// CHECK:STDERR:
// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+4]]:17: error: `var` pattern cannot declare a compile-time binding [CompileTimeBindingInVarDecl]
// CHECK:STDERR: let var c:! ();
// CHECK:STDERR: ^
// CHECK:STDERR:
var v: ();
}
let var c:! ();

// CHECK:STDOUT: --- fail_in_interface.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
// CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .I = %I.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: interface @I {
// CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self
// CHECK:STDOUT: witness = ()
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDERR: fail_in_interface.carbon:[[@LINE+4]]:13: error: `var` pattern cannot declare a compile-time binding [CompileTimeBindingInVarDecl]
// CHECK:STDERR: var d:! ();
// CHECK:STDERR: ^
// CHECK:STDERR:
var d:! ();
}
1 change: 0 additions & 1 deletion toolchain/diagnostics/diagnostic_kind.def
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,6 @@ CARBON_DIAGNOSTIC_KIND(ExportPrevious)
// Interface checking.
CARBON_DIAGNOSTIC_KIND(InterfaceForwardDeclaredHere)
CARBON_DIAGNOSTIC_KIND(InterfaceIncompleteWithinDefinition)
CARBON_DIAGNOSTIC_KIND(VarInInterfaceDecl)

// Impl checking.
CARBON_DIAGNOSTIC_KIND(AssociatedConstantHere)
Expand Down
Loading