From e2984d9fc3ac36250080fc327e67d9ec406a4975 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Tue, 29 Apr 2025 11:25:27 -0400 Subject: [PATCH] Avoid ToTokenOnly for the AddrSelfIsNonRef diagnostic (#5371) The location of the diagnostic is an instruction id, not a parse node. Converting to a parse node to call ToTokenOnly will drop the descendents of the instruction being diagnosed, but the diagnostic is about the whole instruction not just the root parse node of whatever instruction it happens to be. --- toolchain/check/pattern_match.cpp | 4 +--- .../check/testdata/class/fail_memaccess_category.carbon | 8 ++++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/toolchain/check/pattern_match.cpp b/toolchain/check/pattern_match.cpp index 5a62983a410f..f97840d1c336 100644 --- a/toolchain/check/pattern_match.cpp +++ b/toolchain/check/pattern_match.cpp @@ -261,9 +261,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context, default: CARBON_DIAGNOSTIC(AddrSelfIsNonRef, Error, "`addr self` method cannot be invoked on a value"); - context.emitter().Emit( - context.insts().GetCanonicalLocId(entry.scrutinee_id).ToTokenOnly(), - AddrSelfIsNonRef); + context.emitter().Emit(entry.scrutinee_id, AddrSelfIsNonRef); // Add fake reference expression to preserve invariants. auto scrutinee = context.insts().GetWithLocId(entry.scrutinee_id); scrutinee_ref_id = AddInstWithCleanup( diff --git a/toolchain/check/testdata/class/fail_memaccess_category.carbon b/toolchain/check/testdata/class/fail_memaccess_category.carbon index 6908607d453a..6b6cd6176ef3 100644 --- a/toolchain/check/testdata/class/fail_memaccess_category.carbon +++ b/toolchain/check/testdata/class/fail_memaccess_category.carbon @@ -18,9 +18,9 @@ class B { fn F(s: {.a: A}, b: B) { // `s` has only a value representation, so this must be invalid. - // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE+7]]:4: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef] + // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE+7]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef] // CHECK:STDERR: s.a.F(); - // CHECK:STDERR: ^ + // CHECK:STDERR: ^~~ // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE-12]]:8: note: initializing function parameter [InCallToFunctionParam] // CHECK:STDERR: fn F[addr self: A*](); // CHECK:STDERR: ^~~~~~~~~~~~~ @@ -29,9 +29,9 @@ fn F(s: {.a: A}, b: B) { // `b` has an object representation for `A`, but this is still invalid for // consistency. - // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE+7]]:4: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef] + // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE+7]]:3: error: `addr self` method cannot be invoked on a value [AddrSelfIsNonRef] // CHECK:STDERR: b.a.F(); - // CHECK:STDERR: ^ + // CHECK:STDERR: ^~~ // CHECK:STDERR: fail_memaccess_category.carbon:[[@LINE-23]]:8: note: initializing function parameter [InCallToFunctionParam] // CHECK:STDERR: fn F[addr self: A*](); // CHECK:STDERR: ^~~~~~~~~~~~~