Implement unused pattern bindings, continued (#6518)

Implementation of unused pattern bindings #2022, continued.

Whereas previous PR #6460 took care of parsing, and PR #6479 prepared
the stage by using _ in some test cases, this PR has the the actual
implementation, using a simple dataflow analysis.

---------

Co-authored-by: Burak Emir <bqe@google.com>
Co-authored-by: jonmeow <jperkins@google.com>
This commit is contained in:
Burak Emir
2026-02-19 23:33:36 +00:00
committed by GitHub
co-authored by Burak Emir jonmeow
parent bea24a8bee
commit fdb188ccfd
366 changed files with 8453 additions and 7996 deletions
+48 -48
View File
@@ -35,19 +35,19 @@ import library "interface";
class A { adapt {}; }
impl A as Runtime {
fn F[self: Self]() -> type { return A; }
fn F[unused self: Self]() -> type { return A; }
}
impl A as Eval {
// TODO: Consider rejecting this, as compile-time evaluation would always
// fail.
fn F[self: Self]() -> type { return A; }
fn F[unused self: Self]() -> type { return A; }
}
impl A as MustEval {
// TODO: Consider rejecting this, as compile-time evaluation would always
// fail.
fn F[self: Self]() -> type { return A; }
fn F[unused self: Self]() -> type { return A; }
}
fn Call() {
@@ -65,33 +65,33 @@ import library "interface";
class A { adapt {}; }
impl A as Runtime {
fn F[self: Self]() -> type { return A; }
fn F[unused self: Self]() -> type { return A; }
}
impl A as Eval {
fn F[self: Self]() -> type { return A; }
fn F[unused self: Self]() -> type { return A; }
}
impl A as MustEval {
fn F[self: Self]() -> type { return A; }
fn F[unused self: Self]() -> type { return A; }
}
fn Call() {
// CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let t: ({} as A).(Runtime.F)() = {} as A;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:17: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let unused t: ({} as A).(Runtime.F)() = {} as A;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let t: ({} as A).(Runtime.F)() = {} as A;
// CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let u: ({} as A).(Eval.F)() = {} as A;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
let unused t: ({} as A).(Runtime.F)() = {} as A;
// CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:17: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let unused u: ({} as A).(Eval.F)() = {} as A;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let u: ({} as A).(Eval.F)() = {} as A;
// CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let v: ({} as A).(MustEval.F)() = {} as A;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
let unused u: ({} as A).(Eval.F)() = {} as A;
// CHECK:STDERR: fail_impl_runtime_eval.carbon:[[@LINE+4]]:17: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let unused v: ({} as A).(MustEval.F)() = {} as A;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let v: ({} as A).(MustEval.F)() = {} as A;
let unused v: ({} as A).(MustEval.F)() = {} as A;
}
// --- impl_eval.carbon
@@ -103,23 +103,23 @@ import library "interface";
class A { adapt {}; }
impl A as Runtime {
eval fn F[self: Self]() -> type { return A; }
eval fn F[unused self: Self]() -> type { return A; }
}
impl A as Eval {
eval fn F[self: Self]() -> type { return A; }
eval fn F[unused self: Self]() -> type { return A; }
}
impl A as MustEval {
eval fn F[self: Self]() -> type { return A; }
eval fn F[unused self: Self]() -> type { return A; }
}
fn Call() {
// TODO: Should we allow calling this at compile time? This is not allowed if
// we generate a thunk; see the next split for an example.
let t: ({} as A).(Runtime.F)() = {} as A;
let u: ({} as A).(Eval.F)() = {} as A;
let v: ({} as A).(MustEval.F)() = {} as A;
let unused t: ({} as A).(Runtime.F)() = {} as A;
let unused u: ({} as A).(Eval.F)() = {} as A;
let unused v: ({} as A).(MustEval.F)() = {} as A;
}
// --- fail_todo_impl_eval_from_runtime_thunk.carbon
@@ -132,21 +132,21 @@ class B {}
class BView {}
impl B as Core.ImplicitAs(BView) {
fn Convert[self: Self]() -> BView { return {}; }
fn Convert[unused self: Self]() -> BView { return {}; }
}
impl B as Runtime {
eval fn F[self: BView]() -> type { return B; }
eval fn F[unused self: BView]() -> type { return B; }
}
fn Call() {
// TODO: Should we allow calling this at compile time? This is allowed if we
// don't generate a thunk.
// CHECK:STDERR: fail_todo_impl_eval_from_runtime_thunk.carbon:[[@LINE+4]]:10: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let t: ({} as B).(Runtime.F)() = {} as B;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_impl_eval_from_runtime_thunk.carbon:[[@LINE+4]]:17: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR: let unused t: ({} as B).(Runtime.F)() = {} as B;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let t: ({} as B).(Runtime.F)() = {} as B;
let unused t: ({} as B).(Runtime.F)() = {} as B;
}
// --- fail_impl_musteval.carbon
@@ -159,32 +159,32 @@ class B { adapt {}; }
impl B as Runtime {
// CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+11]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: musteval fn F[unused self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+8]]:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: musteval fn F[unused self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE-11]]:1: in import [InImport]
// CHECK:STDERR: interface.carbon:5:3: note: while building thunk to match the signature of this function [ThunkSignature]
// CHECK:STDERR: fn F[self: Self]() -> type;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
musteval fn F[self: Self]() -> type { return B; }
musteval fn F[unused self: Self]() -> type { return B; }
}
impl B as Eval {
// CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+11]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: musteval fn F[unused self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE+8]]:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR: musteval fn F[self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: musteval fn F[unused self: Self]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_impl_musteval.carbon:[[@LINE-26]]:1: in import [InImport]
// CHECK:STDERR: interface.carbon:9:3: note: while building thunk to match the signature of this function [ThunkSignature]
// CHECK:STDERR: eval fn F[self: Self]() -> type;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
musteval fn F[self: Self]() -> type { return B; }
musteval fn F[unused self: Self]() -> type { return B; }
}
// --- impl_musteval_from_musteval.carbon
@@ -196,11 +196,11 @@ import library "interface";
class B { adapt {}; }
impl B as MustEval {
musteval fn F[self: Self]() -> type { return B; }
musteval fn F[unused self: Self]() -> type { return B; }
}
fn Call() {
let t: ({} as B).(MustEval.F)() = {} as B;
let unused t: ({} as B).(MustEval.F)() = {} as B;
}
// --- fail_todo_impl_musteval_from_musteval_thunk.carbon
@@ -213,20 +213,20 @@ class B {}
class BView {}
impl B as Core.ImplicitAs(BView) {
fn Convert[self: Self]() -> BView { return {}; }
fn Convert[unused self: Self]() -> BView { return {}; }
}
impl B as MustEval {
// CHECK:STDERR: fail_todo_impl_musteval_from_musteval_thunk.carbon:[[@LINE+11]]:3: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: musteval fn F[self: BView]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: musteval fn F[unused self: BView]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_impl_musteval_from_musteval_thunk.carbon:[[@LINE+8]]:3: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR: musteval fn F[self: BView]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: musteval fn F[unused self: BView]() -> type { return B; }
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR: fail_todo_impl_musteval_from_musteval_thunk.carbon:[[@LINE-16]]:1: in import [InImport]
// CHECK:STDERR: interface.carbon:13:3: note: while building thunk to match the signature of this function [ThunkSignature]
// CHECK:STDERR: musteval fn F[self: Self]() -> type;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
musteval fn F[self: BView]() -> type { return B; }
musteval fn F[unused self: BView]() -> type { return B; }
}