mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Implements proposal #7016: `self` moves from the deduced implicit list (`fn F[self: Self]()`) to the front of the explicit list. Its type may be written explicitly (`fn F(self: Self)`) or omitted, in which case it defaults to `Self` (`fn F(self)`, `fn F(ref self)`); `self` in the implicit list is rejected. Throughout checking, `self` is modeled as the first explicit parameter. Because a method is just a function whose first parameter is `self`, it can also be called as an ordinary function with the receiver passed explicitly (`Type.M(obj, ...)`), not only as `obj.M(...)`. A new `SemIR::CallArgParamPatterns` helper chooses the parameters matched against the explicit arguments, excluding a leading `self` only when it is supplied as a method-call receiver; arity checking, conversion, and generic deduction use it. The resulting SemIR and lowering are unchanged: `self` is still `call_param0`, and witnesses, thunks, and vtables are unaffected. An omitted `self` type is parsed as a `SelfBindingPattern` node with no type expression; checking synthesizes the `Self` type so it behaves exactly like `self: Self`. However, the exact spelling used must match between a forward declaration and a definition, following #3763's rules around declaration matching. Generated functions, thunks, and C++ interop import/export build `self` as the first explicit parameter, and the `self`-type override (e.g. Derived->Base for a virtual override) applies to the explicit `self`. Placement is validated by new diagnostics: `SelfInImplicitParamList`, `SelfNotFirstParam`, and `SelfOutsideParamList`. The benchmark source generator and the documentation adopt the `(self)` shorthand; the prelude, the examples, and the test data are migrated in the following commits. Assisted-by: Claude Code with Claude Opus 4.7 --------- Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
192 lines
7.2 KiB
Plaintext
192 lines
7.2 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/convert.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/lookup/specialization_poison.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/specialization_poison.carbon
|
|
|
|
// --- fail_final_poisoned_concrete_query_in_specific.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {
|
|
let T:! type;
|
|
fn F(self) -> T;
|
|
}
|
|
|
|
impl forall [U:! type] U as I where .T = () {
|
|
fn F(unused self) -> () { return (); }
|
|
}
|
|
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_in_specific.carbon:[[@LINE+3]]:25: error: found `impl` that would change the result of an earlier use of `C* as I` [PoisonedImplLookupConcreteResult]
|
|
// CHECK:STDERR: fn H[W:! type](v: W) -> W.(I.T) {
|
|
// CHECK:STDERR: ^~~~~~~
|
|
fn H[W:! type](v: W) -> W.(I.T) {
|
|
return v.(I.F)();
|
|
}
|
|
|
|
class C { adapt (); }
|
|
|
|
fn G(p: C*) -> () {
|
|
// This concrete impl lookup query poisons any further specializations.
|
|
return H(p);
|
|
}
|
|
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_in_specific.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
|
|
// CHECK:STDERR: impl C* as I where .T = C {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_in_specific.carbon:[[@LINE-21]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
|
|
// CHECK:STDERR: impl forall [U:! type] U as I where .T = () {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl C* as I where .T = C {
|
|
fn F(self) -> C { return *self; }
|
|
}
|
|
|
|
// --- fail_final_poisoned_concrete_query_in_generic.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {
|
|
let T:! type;
|
|
}
|
|
|
|
impl forall [U:! type] U as I where .T = () {}
|
|
|
|
class C {}
|
|
|
|
fn H[W:! type](unused v: W) {
|
|
// This concrete impl lookup query poisons any further specializations.
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_in_generic.carbon:[[@LINE+3]]:17: error: found `impl` that would change the result of an earlier use of `C as I` [PoisonedImplLookupConcreteResult]
|
|
// CHECK:STDERR: let unused a: C.(I.T) = ();
|
|
// CHECK:STDERR: ^~~~~~~
|
|
let unused a: C.(I.T) = ();
|
|
}
|
|
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_in_generic.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
|
|
// CHECK:STDERR: impl C as I where .T = C {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_in_generic.carbon:[[@LINE-15]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
|
|
// CHECK:STDERR: impl forall [U:! type] U as I where .T = () {}
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl C as I where .T = C {}
|
|
|
|
// --- fail_final_poisoned_concrete_query_nested_type_in_self.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {
|
|
let T:! type;
|
|
fn F(self) -> T;
|
|
}
|
|
|
|
class C(U:! type) {}
|
|
|
|
impl forall [U:! type] C(U) as I where .T = () {
|
|
fn F(unused self) -> () { return (); }
|
|
}
|
|
|
|
fn G(c: C(())) -> () {
|
|
// This concrete impl lookup query poisons any further specializations.
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_self.carbon:[[@LINE+3]]:10: error: found `impl` that would change the result of an earlier use of `C(()) as I` [PoisonedImplLookupConcreteResult]
|
|
// CHECK:STDERR: return c.(I.F)();
|
|
// CHECK:STDERR: ^~~~~~~
|
|
return c.(I.F)();
|
|
}
|
|
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_self.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
|
|
// CHECK:STDERR: impl C(()) as I where .T = {} {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_self.carbon:[[@LINE-15]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
|
|
// CHECK:STDERR: impl forall [U:! type] C(U) as I where .T = () {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl C(()) as I where .T = {} {
|
|
fn F(unused self) -> {} { return {}; }
|
|
}
|
|
|
|
// --- fail_final_poisoned_concrete_query_nested_type_in_interface.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I(U:! type) {
|
|
let T:! type;
|
|
fn F(self) -> T;
|
|
}
|
|
|
|
impl forall [U:! type] U as I(U) where .T = () {
|
|
fn F(unused self) -> () { return (); }
|
|
}
|
|
|
|
class C {}
|
|
|
|
fn G(c: C) -> () {
|
|
// This concrete impl lookup query poisons any further specializations.
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_interface.carbon:[[@LINE+3]]:10: error: found `impl` that would change the result of an earlier use of `C as I(C)` [PoisonedImplLookupConcreteResult]
|
|
// CHECK:STDERR: return c.(I(C).F)();
|
|
// CHECK:STDERR: ^~~~~~~~~~
|
|
return c.(I(C).F)();
|
|
}
|
|
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_interface.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
|
|
// CHECK:STDERR: impl C as I(C) where .T = {} {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_interface.carbon:[[@LINE-17]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
|
|
// CHECK:STDERR: impl forall [U:! type] U as I(U) where .T = () {
|
|
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// CHECK:STDERR:
|
|
impl C as I(C) where .T = {} {
|
|
fn F(unused self) -> {} { return {}; }
|
|
}
|
|
|
|
// --- todo_fail_final_poisoned_by_generic_query.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
interface I {
|
|
let T:! type;
|
|
fn F(self) -> T;
|
|
}
|
|
|
|
impl forall [U:! type] U as I where .T = () {
|
|
fn F(unused self) -> () { return (); }
|
|
}
|
|
|
|
fn H[W:! type](v: W) -> W.(I.T) {
|
|
return v.(I.F)();
|
|
}
|
|
|
|
// This function could return a concrete `X` if it saw the `final` impl below.
|
|
fn G[X:! type](p: X*) -> (X*).(I.T) {
|
|
return H(p);
|
|
}
|
|
|
|
// TODO: This should be diagnosed as poisoned, as H() uses an associated
|
|
// constant from the `impl` which was treated as a symbolic, but this would
|
|
// change it to be a concrete type.
|
|
final impl forall [V:! Core.Copy] V* as I where .T = V {
|
|
fn F(self) -> V { return *self; }
|
|
}
|
|
|
|
// --- fail_todo_poisoned_concrete_lookup_from_eval_fn.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
class D {}
|
|
|
|
// CHECK:STDERR: fail_todo_poisoned_concrete_lookup_from_eval_fn.carbon:[[@LINE+4]]:22: error: semantics TODO: `HandleAutoTypeLiteral` [SemanticsTodo]
|
|
// CHECK:STDERR: musteval fn G() -> auto {
|
|
// CHECK:STDERR: ^~~~
|
|
// CHECK:STDERR:
|
|
musteval fn G() -> auto {
|
|
class C(T:! type) {}
|
|
impl forall [T:! type] C(T) as Z {}
|
|
C(D) as Z;
|
|
return C(D);
|
|
}
|
|
|
|
// TODO: error: found `impl` that would change the result of an earlier use of `C(D) as Z`.
|
|
impl G() as Z {}
|
|
}
|