Files
carbon-lang/toolchain/check/testdata/impl/impl_inside_interface.carbon
T
Dana Jansens 6776e2b804 Detect impl redecls in non-declarative scopes (#7170)
Avoid crashing on the fact that block scopes have no related InstId. So
we can't push the InstId of the current scope in the ImplIntroducer
node, as it can be None. Instead we have to find the parent InstId when
we're building the ImplDecl, because the ImplDecl has a node at the top
of the scope stack for the DeclNameStack.

Impls are allowed in sequential (non-declarative) scopes (functions,
blocks), but [redeclarations are not
allowed](https://github.com/carbon-language/carbon-lang/blob/db24042fe56d22275aa801696e2f8f5c4171e35b/proposals/p3763.md?plain=1#L279).
We now diagnose these redecls as invalid.
2026-05-07 13:44:40 +00:00

57 lines
2.6 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/impl_inside_interface.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_inside_interface.carbon
// --- fail_todo_impl_in_interface_definition.carbon
library "[[@TEST_NAME]]";
// This test uses many unsupported features, and is expected to change.
interface I {
// CHECK:STDERR: fail_todo_impl_in_interface_definition.carbon:[[@LINE+4]]:3: error: semantics TODO: `interface modifier` [SemanticsTodo]
// CHECK:STDERR: default fn F() {
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
default fn F() {
class C {}
// CHECK:STDERR: fail_todo_impl_in_interface_definition.carbon:[[@LINE+7]]:5: error: missing implementation of F in impl of interface I [ImplMissingFunction]
// CHECK:STDERR: impl C as I {}
// CHECK:STDERR: ^~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_impl_in_interface_definition.carbon:[[@LINE-5]]:3: note: associated function F declared here [AssociatedFunctionHere]
// CHECK:STDERR: default fn F() {
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl C as I {}
}
}
// --- fail_todo_impl_in_interface_definition_with_associated.carbon
library "[[@TEST_NAME]]";
// This test uses many unsupported features, and is expected to change.
interface I {
let U:! type;
// CHECK:STDERR: fail_todo_impl_in_interface_definition_with_associated.carbon:[[@LINE+4]]:3: error: semantics TODO: `interface modifier` [SemanticsTodo]
// CHECK:STDERR: default fn F() {
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
default fn F() {
class C {}
// CHECK:STDERR: fail_todo_impl_in_interface_definition_with_associated.carbon:[[@LINE+7]]:5: error: missing implementation of F in impl of interface I [ImplMissingFunction]
// CHECK:STDERR: impl C as I where .U = C {}
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_impl_in_interface_definition_with_associated.carbon:[[@LINE-5]]:3: note: associated function F declared here [AssociatedFunctionHere]
// CHECK:STDERR: default fn F() {
// CHECK:STDERR: ^~~~~~~~~~~~~~~~
// CHECK:STDERR:
impl C as I where .U = C {}
}
}