From 46a474bbb8e72db6c8ddeea0f06b28da9d9317b9 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 28 Oct 2025 15:27:55 -0700 Subject: [PATCH 01/20] ci: add runners for vanilla LLVM 21 Ubuntu 25.10 has `llvm-21` packages that we can test with. The `Dockerfile` is otherwise the same as the `llvm-20` runners. --- .../host-x86_64/x86_64-gnu-llvm-21/Dockerfile | 66 +++++++++++++++++++ src/ci/github-actions/jobs.yml | 25 +++++++ 2 files changed, 91 insertions(+) create mode 100644 src/ci/docker/host-x86_64/x86_64-gnu-llvm-21/Dockerfile diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-llvm-21/Dockerfile b/src/ci/docker/host-x86_64/x86_64-gnu-llvm-21/Dockerfile new file mode 100644 index 0000000000000..f0314854411f2 --- /dev/null +++ b/src/ci/docker/host-x86_64/x86_64-gnu-llvm-21/Dockerfile @@ -0,0 +1,66 @@ +FROM ubuntu:25.10 + +ARG DEBIAN_FRONTEND=noninteractive + +RUN apt-get update && apt-get install -y --no-install-recommends \ + bzip2 \ + g++ \ + gcc-multilib \ + make \ + ninja-build \ + file \ + curl \ + ca-certificates \ + python3 \ + git \ + cmake \ + sudo \ + gdb \ + llvm-21-tools \ + llvm-21-dev \ + libedit-dev \ + libssl-dev \ + pkg-config \ + zlib1g-dev \ + xz-utils \ + nodejs \ + mingw-w64 \ + # libgccjit dependencies + flex \ + libmpfr-dev \ + libgmp-dev \ + libmpc3 \ + libmpc-dev \ + && rm -rf /var/lib/apt/lists/* + +# Install powershell (universal package) so we can test x.ps1 on Linux +# FIXME: need a "universal" version that supports libicu74, but for now it still works to ignore that dep. +RUN curl -sL "https://github.com/PowerShell/PowerShell/releases/download/v7.3.1/powershell_7.3.1-1.deb_amd64.deb" > powershell.deb && \ + dpkg --ignore-depends=libicu72 -i powershell.deb && \ + rm -f powershell.deb + +COPY scripts/sccache.sh /scripts/ +RUN sh /scripts/sccache.sh + +# We are disabling CI LLVM since this builder is intentionally using a host +# LLVM, rather than the typical src/llvm-project LLVM. +ENV NO_DOWNLOAD_CI_LLVM 1 +ENV EXTERNAL_LLVM 1 + +# Using llvm-link-shared due to libffi issues -- see #34486 +ENV RUST_CONFIGURE_ARGS \ + --build=x86_64-unknown-linux-gnu \ + --llvm-root=/usr/lib/llvm-21 \ + --enable-llvm-link-shared \ + --set rust.randomize-layout=true \ + --set rust.thin-lto-import-instr-limit=10 + +COPY scripts/shared.sh /scripts/ + +COPY scripts/x86_64-gnu-llvm.sh /scripts/ +COPY scripts/x86_64-gnu-llvm2.sh /scripts/ +COPY scripts/x86_64-gnu-llvm3.sh /scripts/ +COPY scripts/stage_2_test_set1.sh /scripts/ +COPY scripts/stage_2_test_set2.sh /scripts/ + +ENV SCRIPT "Must specify DOCKER_SCRIPT for this image" diff --git a/src/ci/github-actions/jobs.yml b/src/ci/github-actions/jobs.yml index eeef94483fda0..35cc43fa4f64a 100644 --- a/src/ci/github-actions/jobs.yml +++ b/src/ci/github-actions/jobs.yml @@ -404,6 +404,31 @@ auto: DOCKER_SCRIPT: x86_64-gnu-llvm3.sh <<: *job-linux-4c + # The x86_64-gnu-llvm-21 job is split into multiple jobs to run tests in parallel. + # x86_64-gnu-llvm-21-1 skips tests that run in x86_64-gnu-llvm-21-{2,3}. + - name: x86_64-gnu-llvm-21-1 + env: + RUST_BACKTRACE: 1 + IMAGE: x86_64-gnu-llvm-21 + DOCKER_SCRIPT: stage_2_test_set2.sh + <<: *job-linux-4c + + # Skip tests that run in x86_64-gnu-llvm-21-{1,3} + - name: x86_64-gnu-llvm-21-2 + env: + RUST_BACKTRACE: 1 + IMAGE: x86_64-gnu-llvm-21 + DOCKER_SCRIPT: x86_64-gnu-llvm2.sh + <<: *job-linux-4c + + # Skip tests that run in x86_64-gnu-llvm-21-{1,2} + - name: x86_64-gnu-llvm-21-3 + env: + RUST_BACKTRACE: 1 + IMAGE: x86_64-gnu-llvm-21 + DOCKER_SCRIPT: x86_64-gnu-llvm3.sh + <<: *job-linux-4c + - name: x86_64-gnu-nopt <<: *job-linux-4c From 3a02b357d36414dca2e388e761c41c0f611bd460 Mon Sep 17 00:00:00 2001 From: "Mark Z. Ding" Date: Wed, 29 Oct 2025 09:33:38 -0400 Subject: [PATCH 02/20] Add tests to demonstrate explicit tail call bug --- .../explicit-tail-calls/become-cast-return.rs | 22 +++++++++++++++++++ .../become-indirect-return.rs | 21 ++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 tests/ui/explicit-tail-calls/become-cast-return.rs create mode 100644 tests/ui/explicit-tail-calls/become-indirect-return.rs diff --git a/tests/ui/explicit-tail-calls/become-cast-return.rs b/tests/ui/explicit-tail-calls/become-cast-return.rs new file mode 100644 index 0000000000000..3052886d7b8f8 --- /dev/null +++ b/tests/ui/explicit-tail-calls/become-cast-return.rs @@ -0,0 +1,22 @@ +//@ check-pass +//@ ignore-backends: gcc +//@ known-bug: #148239 +//@ compile-flags: -Zno-codegen +#![expect(incomplete_features)] +#![feature(explicit_tail_calls)] + +#[inline(never)] +fn leaf(_: &Box) -> [u8; 1] { + [1] +} + +#[inline(never)] +fn dispatch(param: &Box) -> [u8; 1] { + become leaf(param) +} + +fn main() { + let data = Box::new(0); + let out = dispatch(&data); + assert_eq!(out, [1]); +} diff --git a/tests/ui/explicit-tail-calls/become-indirect-return.rs b/tests/ui/explicit-tail-calls/become-indirect-return.rs new file mode 100644 index 0000000000000..c94683d15f40b --- /dev/null +++ b/tests/ui/explicit-tail-calls/become-indirect-return.rs @@ -0,0 +1,21 @@ +//@ run-pass +//@ ignore-backends: gcc +//@ known-bug: #148239 +#![expect(incomplete_features)] +#![feature(explicit_tail_calls)] + +#[inline(never)] +fn op_dummy(_param: &Box) -> [u8; 24] { + [1; 24] +} + +#[inline(never)] +fn dispatch(param: &Box) -> [u8; 24] { + become op_dummy(param) +} + +fn main() { + let param = Box::new(0); + let result = dispatch(¶m); + assert_ne!(result, [1; 24]); // the data is not right! +} From 52959a85cca72bdacaae9793005ff7cc7af43762 Mon Sep 17 00:00:00 2001 From: "Mark Z. Ding" Date: Wed, 29 Oct 2025 09:38:56 -0400 Subject: [PATCH 03/20] Fix musttail returns for cast/indirect ABIs --- compiler/rustc_codegen_llvm/src/builder.rs | 6 +----- compiler/rustc_codegen_ssa/src/mir/block.rs | 12 +++++++++++- tests/ui/explicit-tail-calls/become-cast-return.rs | 4 +--- .../ui/explicit-tail-calls/become-indirect-return.rs | 3 +-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index c082a82306848..49d38e05c8b06 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -16,7 +16,6 @@ use rustc_codegen_ssa::mir::place::PlaceRef; use rustc_codegen_ssa::traits::*; use rustc_data_structures::small_c_str::SmallCStr; use rustc_hir::def_id::DefId; -use rustc_middle::bug; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs; use rustc_middle::ty::layout::{ FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers, @@ -1458,10 +1457,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { match &fn_abi.ret.mode { PassMode::Ignore | PassMode::Indirect { .. } => self.ret_void(), - PassMode::Direct(_) | PassMode::Pair { .. } => self.ret(call), - mode @ PassMode::Cast { .. } => { - bug!("Encountered `PassMode::{mode:?}` during codegen") - } + PassMode::Direct(_) | PassMode::Pair { .. } | PassMode::Cast { .. } => self.ret(call), } } diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index e2241a77d186c..0d1e27a34dc5b 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -1063,7 +1063,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let return_dest = self.make_return_dest(bx, destination, &fn_abi.ret, &mut llargs); target.map(|target| (return_dest, target)) } - CallKind::Tail => None, + CallKind::Tail => { + if fn_abi.ret.is_indirect() { + match self.make_return_dest(bx, destination, &fn_abi.ret, &mut llargs) { + ReturnDest::Nothing => {} + _ => bug!( + "tail calls to functions with indirect returns cannot store into a destination" + ), + } + } + None + } }; // Split the rust-call tupled arguments off. diff --git a/tests/ui/explicit-tail-calls/become-cast-return.rs b/tests/ui/explicit-tail-calls/become-cast-return.rs index 3052886d7b8f8..212a0ddcbca15 100644 --- a/tests/ui/explicit-tail-calls/become-cast-return.rs +++ b/tests/ui/explicit-tail-calls/become-cast-return.rs @@ -1,7 +1,5 @@ -//@ check-pass +//@ run-pass //@ ignore-backends: gcc -//@ known-bug: #148239 -//@ compile-flags: -Zno-codegen #![expect(incomplete_features)] #![feature(explicit_tail_calls)] diff --git a/tests/ui/explicit-tail-calls/become-indirect-return.rs b/tests/ui/explicit-tail-calls/become-indirect-return.rs index c94683d15f40b..7eec34f3b95c9 100644 --- a/tests/ui/explicit-tail-calls/become-indirect-return.rs +++ b/tests/ui/explicit-tail-calls/become-indirect-return.rs @@ -1,6 +1,5 @@ //@ run-pass //@ ignore-backends: gcc -//@ known-bug: #148239 #![expect(incomplete_features)] #![feature(explicit_tail_calls)] @@ -17,5 +16,5 @@ fn dispatch(param: &Box) -> [u8; 24] { fn main() { let param = Box::new(0); let result = dispatch(¶m); - assert_ne!(result, [1; 24]); // the data is not right! + assert_eq!(result, [1; 24]); } From 973ab7d08f6075b833aba0d8f96c28710b1cdfa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 1 Nov 2025 18:23:19 +0000 Subject: [PATCH 04/20] Make "add param to inner item" suggestion verbose ``` error[E0401]: can't use generic parameters from outer item --> $DIR/enum-definition-with-outer-generic-parameter-5997.rs:3:16 | LL | fn f() -> bool { | - type parameter from outer item LL | enum E { V(Z) } | ^ use of generic parameter from outer item | help: try introducing a local generic parameter here | LL | enum E { V(Z) } | +++ ``` --- compiler/rustc_resolve/src/errors.rs | 7 +++- .../early/const-param-from-outer-fn.stderr | 6 +++- tests/ui/delegation/target-expr.stderr | 9 +++-- tests/ui/error-codes/E0401.stderr | 17 +++++---- ...n-with-outer-generic-parameter-5997.stderr | 9 +++-- ...eneric-params-nested-fn-scope-error.stderr | 18 ++++++---- tests/ui/generics/issue-98432.stderr | 9 +++-- tests/ui/resolve/bad-type-env-capture.stderr | 9 +++-- ...m-in-const-item.generic_const_items.stderr | 24 ++++++++----- tests/ui/resolve/issue-3021-c.stderr | 18 ++++++---- tests/ui/resolve/issue-3214.stderr | 6 +++- ...resolve-type-param-in-item-in-trait.stderr | 36 ++++++++++++------- tests/ui/type/type-arg-out-of-scope.stderr | 18 ++++++---- 13 files changed, 126 insertions(+), 60 deletions(-) diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index f0ea97ba8a0c0..3465c7fb16473 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -47,7 +47,12 @@ pub(crate) enum GenericParamsFromOuterItemLabel { } #[derive(Subdiagnostic)] -#[suggestion(resolve_suggestion, code = "{snippet}", applicability = "maybe-incorrect")] +#[suggestion( + resolve_suggestion, + code = "{snippet}", + applicability = "maybe-incorrect", + style = "verbose" +)] pub(crate) struct GenericParamsFromOuterItemSugg { #[primary_span] pub(crate) span: Span, diff --git a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr index faba4ce10a1b4..0f7c7fbc6be01 100644 --- a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr +++ b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr @@ -4,9 +4,13 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - const parameter from outer item LL | fn bar() -> u32 { - | - help: try introducing a local generic parameter here: `` LL | X | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar() -> u32 { + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/delegation/target-expr.stderr b/tests/ui/delegation/target-expr.stderr index f5556bf9f4514..8bb25ad9e96e2 100644 --- a/tests/ui/delegation/target-expr.stderr +++ b/tests/ui/delegation/target-expr.stderr @@ -3,11 +3,14 @@ error[E0401]: can't use generic parameters from outer item | LL | fn bar(_: T) { | - type parameter from outer item -LL | reuse Trait::static_method { - | - help: try introducing a local generic parameter here: `T,` -LL | +... LL | let _ = T::Default(); | ^^^^^^^^^^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | reuse Trait::static_methodT, { + | ++ error[E0434]: can't capture dynamic environment in a fn item --> $DIR/target-expr.rs:26:17 diff --git a/tests/ui/error-codes/E0401.stderr b/tests/ui/error-codes/E0401.stderr index 5d6878620c89a..fedb064c6bea6 100644 --- a/tests/ui/error-codes/E0401.stderr +++ b/tests/ui/error-codes/E0401.stderr @@ -4,9 +4,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bfnr, W: Fn()>(y: T) { - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `T,` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bfnr, W: Fn()>(y: T) { + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/E0401.rs:9:16 @@ -14,11 +17,13 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item ... -LL | fn baz $DIR/E0401.rs:22:25 diff --git a/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr b/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr index aea0f049b07d3..cded8ce319616 100644 --- a/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr +++ b/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr @@ -4,9 +4,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn f() -> bool { | - type parameter from outer item LL | enum E { V(Z) } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | enum E { V(Z) } + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/generics/generic-params-nested-fn-scope-error.stderr b/tests/ui/generics/generic-params-nested-fn-scope-error.stderr index 7fd1069c651fa..8c3b65d1edea9 100644 --- a/tests/ui/generics/generic-params-nested-fn-scope-error.stderr +++ b/tests/ui/generics/generic-params-nested-fn-scope-error.stderr @@ -4,9 +4,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(v: Vec) -> U { | - type parameter from outer item LL | fn bar(w: [U]) -> U { - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar(w: [U]) -> U { + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/generic-params-nested-fn-scope-error.rs:5:23 @@ -14,9 +17,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(v: Vec) -> U { | - type parameter from outer item LL | fn bar(w: [U]) -> U { - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar(w: [U]) -> U { + | +++ error: aborting due to 2 previous errors diff --git a/tests/ui/generics/issue-98432.stderr b/tests/ui/generics/issue-98432.stderr index 2b09d43960f62..f20f779d884c0 100644 --- a/tests/ui/generics/issue-98432.stderr +++ b/tests/ui/generics/issue-98432.stderr @@ -5,9 +5,12 @@ LL | impl Struct { | - type parameter from outer item LL | const CONST: fn() = || { LL | struct _Obligation where T:; - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | struct _Obligation where T:; + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/resolve/bad-type-env-capture.stderr b/tests/ui/resolve/bad-type-env-capture.stderr index 3f9bc9149c24a..272672beb4fbb 100644 --- a/tests/ui/resolve/bad-type-env-capture.stderr +++ b/tests/ui/resolve/bad-type-env-capture.stderr @@ -4,9 +4,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | fn bar(b: T) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar(b: T) { } + | +++ error: aborting due to 1 previous error diff --git a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr index 60aa94038c3ad..3852b84fee2e0 100644 --- a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr +++ b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr @@ -4,11 +4,13 @@ error[E0401]: can't use generic parameters from outer item LL | fn outer() { // outer function | - type parameter from outer item LL | const K: u32 = T::C; - | - ^^^^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^^^^ use of generic parameter from outer item | = note: a `const` is a separate item from the item that contains it +help: try introducing a local generic parameter here + | +LL | const K: u32 = T::C; + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/generic-params-from-outer-item-in-const-item.rs:19:24 @@ -17,11 +19,13 @@ LL | impl Tr for T { // outer impl block | - type parameter from outer item LL | const C: u32 = { LL | const I: u32 = T::C; - | - ^^^^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^^^^ use of generic parameter from outer item | = note: a `const` is a separate item from the item that contains it +help: try introducing a local generic parameter here + | +LL | const I: u32 = T::C; + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/generic-params-from-outer-item-in-const-item.rs:27:20 @@ -29,11 +33,13 @@ error[E0401]: can't use generic parameters from outer item LL | struct S(U32<{ // outer struct | - type parameter from outer item LL | const _: u32 = T::C; - | - ^^^^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^^^^ use of generic parameter from outer item | = note: a `const` is a separate item from the item that contains it +help: try introducing a local generic parameter here + | +LL | const _: u32 = T::C; + | +++ error: aborting due to 3 previous errors diff --git a/tests/ui/resolve/issue-3021-c.stderr b/tests/ui/resolve/issue-3021-c.stderr index 537bbaf7b6a56..dcbaf2afd10c6 100644 --- a/tests/ui/resolve/issue-3021-c.stderr +++ b/tests/ui/resolve/issue-3021-c.stderr @@ -3,22 +3,28 @@ error[E0401]: can't use generic parameters from outer item | LL | fn siphash() { | - type parameter from outer item -LL | -LL | trait U { - | - help: try introducing a local generic parameter here: `` +... LL | fn g(&self, x: T) -> T; | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | trait U { + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/issue-3021-c.rs:4:30 | LL | fn siphash() { | - type parameter from outer item -LL | -LL | trait U { - | - help: try introducing a local generic parameter here: `` +... LL | fn g(&self, x: T) -> T; | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | trait U { + | +++ error: aborting due to 2 previous errors diff --git a/tests/ui/resolve/issue-3214.stderr b/tests/ui/resolve/issue-3214.stderr index 1c64fdc1711fd..5e5872f2a5fbd 100644 --- a/tests/ui/resolve/issue-3214.stderr +++ b/tests/ui/resolve/issue-3214.stderr @@ -4,9 +4,13 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | struct Foo { - | - help: try introducing a local generic parameter here: `` LL | x: T, | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | struct Foo { + | +++ error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied --> $DIR/issue-3214.rs:6:22 diff --git a/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr b/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr index 1ab56fdc50441..086418df7a253 100644 --- a/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr +++ b/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr @@ -3,11 +3,14 @@ error[E0401]: can't use generic parameters from outer item | LL | trait TraitA { | - type parameter from outer item -LL | fn outer(&self) { -LL | enum Foo { - | - help: try introducing a local generic parameter here: `A,` +... LL | Variance(A) | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | enum Foo { + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/resolve-type-param-in-item-in-trait.rs:16:23 @@ -16,9 +19,12 @@ LL | trait TraitB { | - type parameter from outer item LL | fn outer(&self) { LL | struct Foo(A); - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `A,` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | struct Foo(A); + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/resolve-type-param-in-item-in-trait.rs:23:28 @@ -27,9 +33,12 @@ LL | trait TraitC { | - type parameter from outer item LL | fn outer(&self) { LL | struct Foo { a: A } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `A,` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | struct Foo { a: A } + | ++ error[E0401]: can't use generic parameters from outer item --> $DIR/resolve-type-param-in-item-in-trait.rs:30:22 @@ -38,9 +47,12 @@ LL | trait TraitD { | - type parameter from outer item LL | fn outer(&self) { LL | fn foo(a: A) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `A,` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn foo(a: A) { } + | ++ error: aborting due to 4 previous errors diff --git a/tests/ui/type/type-arg-out-of-scope.stderr b/tests/ui/type/type-arg-out-of-scope.stderr index fcaaca1770f67..f4fbcdac53b66 100644 --- a/tests/ui/type/type-arg-out-of-scope.stderr +++ b/tests/ui/type/type-arg-out-of-scope.stderr @@ -4,9 +4,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bar(f: Box T>) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar(f: Box T>) { } + | +++ error[E0401]: can't use generic parameters from outer item --> $DIR/type-arg-out-of-scope.rs:2:35 @@ -14,9 +17,12 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bar(f: Box T>) { } - | - ^ use of generic parameter from outer item - | | - | help: try introducing a local generic parameter here: `` + | ^ use of generic parameter from outer item + | +help: try introducing a local generic parameter here + | +LL | fn bar(f: Box T>) { } + | +++ error: aborting due to 2 previous errors From f6938709c8aef58ade5577ae8ff77fbcb99d0845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 1 Nov 2025 18:29:46 +0000 Subject: [PATCH 05/20] Point at inner item when using outer item type param ``` error[E0401]: can't use generic parameters from outer item --> $DIR/E0401.rs:4:39 | LL | fn foo(x: T) { | - type parameter from outer item LL | fn bfnr, W: Fn()>(y: T) { | ---- ^ use of generic parameter from outer item | | | generic parameter used in this inner function | help: try introducing a local generic parameter here | LL | fn bfnr, W: Fn()>(y: T) { | ++ ``` --- compiler/rustc_resolve/messages.ftl | 5 ++++ compiler/rustc_resolve/src/diagnostics.rs | 7 ++++++ compiler/rustc_resolve/src/errors.rs | 10 ++++++++ compiler/rustc_resolve/src/ident.rs | 25 ++++++++++++++++++- compiler/rustc_resolve/src/late.rs | 15 +++++++---- compiler/rustc_resolve/src/lib.rs | 2 +- tests/ui/delegation/target-expr.stderr | 4 ++- tests/ui/error-codes/E0401.stderr | 16 ++++++++---- ...n-with-outer-generic-parameter-5997.stderr | 4 ++- ...eneric-params-nested-fn-scope-error.stderr | 8 ++++-- tests/ui/generics/issue-98432.stderr | 4 ++- tests/ui/resolve/bad-type-env-capture.stderr | 4 ++- ...om-outer-item-in-const-item.default.stderr | 12 ++++++--- ...m-in-const-item.generic_const_items.stderr | 12 ++++++--- tests/ui/resolve/issue-12796.stderr | 9 ++++--- tests/ui/resolve/issue-3021-c.stderr | 8 ++++-- tests/ui/resolve/issue-3214.stderr | 1 + ...65025-extern-static-parent-generics.stderr | 13 ++++++---- ...e-65035-static-with-parent-generics.stderr | 17 ++++++++----- ...resolve-type-param-in-item-in-trait.stderr | 16 +++++++++--- tests/ui/resolve/use-self-in-inner-fn.rs | 1 + tests/ui/resolve/use-self-in-inner-fn.stderr | 9 ++++--- .../static-generic-param-soundness.stderr | 4 ++- tests/ui/type/type-arg-out-of-scope.stderr | 8 ++++-- 24 files changed, 162 insertions(+), 52 deletions(-) diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 5bf90d2637df5..d462ff589f0da 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -180,6 +180,11 @@ resolve_generic_params_from_outer_item_const = a `const` is a separate item from resolve_generic_params_from_outer_item_const_param = const parameter from outer item +resolve_generic_params_from_outer_item_inner_item = {$is_self -> + [true] `Self` + *[false] generic parameter + } used in this inner {$descr} + resolve_generic_params_from_outer_item_self_ty_alias = `Self` type implicitly declared here, by this `impl` resolve_generic_params_from_outer_item_self_ty_param = can't use `Self` here diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 236ab1f09d35f..f302da356e1e1 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -557,6 +557,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { outer_res, has_generic_params, def_kind, + item, ) => { use errs::GenericParamsFromOuterItemLabel as Label; let static_or_const = match def_kind { @@ -575,6 +576,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { sugg: None, static_or_const, is_self, + item: item.map(|(span, descr)| errs::GenericParamsFromOuterItemInnerItem { + span, + descr, + }), }; let sm = self.tcx.sess.source_map(); @@ -2506,6 +2511,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, &ribs[ns_to_try], ignore_binding, + None, ) { // we found a locally-imported or available item/module Some(LexicalScopeBinding::Item(binding)) => Some(binding), @@ -2556,6 +2562,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, &ribs[ValueNS], ignore_binding, + None, ) } else { None diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 3465c7fb16473..5c5938a3260ed 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -24,6 +24,16 @@ pub(crate) struct GenericParamsFromOuterItem { #[subdiagnostic] pub(crate) static_or_const: Option, pub(crate) is_self: bool, + #[subdiagnostic] + pub(crate) item: Option, +} + +#[derive(Subdiagnostic)] +#[label(resolve_generic_params_from_outer_item_inner_item)] +pub(crate) struct GenericParamsFromOuterItemInnerItem { + #[primary_span] + pub(crate) span: Span, + pub(crate) descr: String, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 2a195c8068dac..4856b1e7cf970 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -12,7 +12,9 @@ use tracing::{debug, instrument}; use crate::errors::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst}; use crate::imports::{Import, NameResolution}; -use crate::late::{ConstantHasGenerics, NoConstantGenericsReason, PathSource, Rib, RibKind}; +use crate::late::{ + ConstantHasGenerics, DiagMetadata, NoConstantGenericsReason, PathSource, Rib, RibKind, +}; use crate::macros::{MacroRulesScope, sub_namespace_match}; use crate::{ AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, CmResolver, Determinacy, @@ -295,6 +297,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize: Option, ribs: &[Rib<'ra>], ignore_binding: Option>, + diag_metadata: Option<&DiagMetadata<'_>>, ) -> Option> { assert!(ns == TypeNS || ns == ValueNS); let orig_ident = ident; @@ -326,6 +329,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize.map(|finalize| finalize.path_span), *original_rib_ident_def, ribs, + diag_metadata, ))); } else if let RibKind::Block(Some(module)) = rib.kind && let Ok(binding) = self.cm().resolve_ident_in_module_unadjusted( @@ -1193,6 +1197,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize: Option, original_rib_ident_def: Ident, all_ribs: &[Rib<'ra>], + diag_metadata: Option<&DiagMetadata<'_>>, ) -> Res { debug!("validate_res_from_ribs({:?})", res); let ribs = &all_ribs[rib_index + 1..]; @@ -1391,12 +1396,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { }; if let Some(span) = finalize { + let item = if let Some(diag_metadata) = diag_metadata + && let Some(current_item) = diag_metadata.current_item + { + let span = current_item + .kind + .ident() + .map(|i| i.span) + .unwrap_or(current_item.span); + Some((span, current_item.kind.descr().to_string())) + } else { + None + }; self.report_error( span, ResolutionError::GenericParamsFromOuterItem( res, has_generic_params, def_kind, + item, ), ); } @@ -1472,6 +1490,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { res, has_generic_params, def_kind, + None, ), ); } @@ -1501,6 +1520,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, None, ignore_import, + None, ) } #[instrument(level = "debug", skip(self))] @@ -1522,6 +1542,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, ignore_binding, ignore_import, + None, ) } @@ -1535,6 +1556,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ribs: Option<&PerNS>>>, ignore_binding: Option>, ignore_import: Option>, + diag_metadata: Option<&DiagMetadata<'_>>, ) -> PathResult<'ra> { let mut module = None; let mut module_had_parse_errors = false; @@ -1675,6 +1697,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { finalize, &ribs[ns], ignore_binding, + diag_metadata, ) { // we found a locally-imported or available item/module Some(LexicalScopeBinding::Item(binding)) => Ok(binding), diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index e3051dc38eca2..198fef0cd630c 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -667,7 +667,7 @@ pub(crate) struct UnnecessaryQualification<'ra> { } #[derive(Default, Debug)] -struct DiagMetadata<'ast> { +pub(crate) struct DiagMetadata<'ast> { /// The current trait's associated items' ident, used for diagnostic suggestions. current_trait_assoc_items: Option<&'ast [Box]>, @@ -678,7 +678,7 @@ struct DiagMetadata<'ast> { current_self_item: Option, /// The current trait (used to suggest). - current_item: Option<&'ast Item>, + pub(crate) current_item: Option<&'ast Item>, /// When processing generic arguments and encountering an unresolved ident not found, /// suggest introducing a type or const param depending on the context. @@ -885,6 +885,7 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc TypeNS, Some(Finalize::new(ty.id, ty.span)), None, + None, ) .map_or(Res::Err, |d| d.res()); self.r.record_partial_res(ty.id, PartialRes::new(res)); @@ -1457,6 +1458,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { None, &self.ribs[ns], None, + None, ) } @@ -1466,6 +1468,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { ns: Namespace, finalize: Option, ignore_binding: Option>, + diag_metadata: Option<&crate::late::DiagMetadata<'_>>, ) -> Option> { self.r.resolve_ident_in_lexical_scope( ident, @@ -1474,6 +1477,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { finalize, &self.ribs[ns], ignore_binding, + diag_metadata, ) } @@ -1493,6 +1497,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { Some(&self.ribs), None, None, + Some(&self.diag_metadata), ) } @@ -2551,8 +2556,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { report_error(self, ns); } Some(LexicalScopeBinding::Item(binding)) => { - if let Some(LexicalScopeBinding::Res(..)) = - self.resolve_ident_in_lexical_scope(ident, ns, None, Some(binding)) + if let Some(LexicalScopeBinding::Res(..)) = self + .resolve_ident_in_lexical_scope(ident, ns, None, Some(binding), None) { report_error(self, ns); } @@ -5105,7 +5110,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { // use the type namespace let ns = if i + 1 == path.len() { ns } else { TypeNS }; let res = self.r.partial_res_map.get(&seg.id?)?.full_res()?; - let binding = self.resolve_ident_in_lexical_scope(seg.ident, ns, None, None)?; + let binding = self.resolve_ident_in_lexical_scope(seg.ident, ns, None, None, None)?; (res == binding.res()).then_some((seg, binding)) }); diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index f6a4f59cb339a..c7f5ec76c0751 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -241,7 +241,7 @@ struct BindingError { #[derive(Debug)] enum ResolutionError<'ra> { /// Error E0401: can't use type or const parameters from outer item. - GenericParamsFromOuterItem(Res, HasGenericParams, DefKind), + GenericParamsFromOuterItem(Res, HasGenericParams, DefKind, Option<(Span, String)>), /// Error E0403: the name is already used for a type or const parameter in this generic /// parameter list. NameAlreadyUsedInParameterList(Ident, Span), diff --git a/tests/ui/delegation/target-expr.stderr b/tests/ui/delegation/target-expr.stderr index 8bb25ad9e96e2..f2e6c331193f0 100644 --- a/tests/ui/delegation/target-expr.stderr +++ b/tests/ui/delegation/target-expr.stderr @@ -3,7 +3,9 @@ error[E0401]: can't use generic parameters from outer item | LL | fn bar(_: T) { | - type parameter from outer item -... +LL | reuse Trait::static_method { + | ------------- generic parameter used in this inner delegated function +LL | LL | let _ = T::Default(); | ^^^^^^^^^^ use of generic parameter from outer item | diff --git a/tests/ui/error-codes/E0401.stderr b/tests/ui/error-codes/E0401.stderr index fedb064c6bea6..8daaf09df1592 100644 --- a/tests/ui/error-codes/E0401.stderr +++ b/tests/ui/error-codes/E0401.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bfnr, W: Fn()>(y: T) { - | ^ use of generic parameter from outer item + | ---- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | @@ -17,6 +19,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item ... +LL | fn baz Iterator for A { | ---- `Self` type implicitly declared here, by this `impl` ... LL | fn helper(sel: &Self) -> u8 { - | ^^^^ - | | - | use of `Self` from outer item - | refer to the type directly here instead + | ------ ^^^^ + | | | + | | use of `Self` from outer item + | | refer to the type directly here instead + | `Self` used in this inner function error: aborting due to 3 previous errors diff --git a/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr b/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr index cded8ce319616..fb2d2f247b65b 100644 --- a/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr +++ b/tests/ui/generics/enum-definition-with-outer-generic-parameter-5997.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn f() -> bool { | - type parameter from outer item LL | enum E { V(Z) } - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner enum | help: try introducing a local generic parameter here | diff --git a/tests/ui/generics/generic-params-nested-fn-scope-error.stderr b/tests/ui/generics/generic-params-nested-fn-scope-error.stderr index 8c3b65d1edea9..f809740ed381f 100644 --- a/tests/ui/generics/generic-params-nested-fn-scope-error.stderr +++ b/tests/ui/generics/generic-params-nested-fn-scope-error.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(v: Vec) -> U { | - type parameter from outer item LL | fn bar(w: [U]) -> U { - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | @@ -17,7 +19,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(v: Vec) -> U { | - type parameter from outer item LL | fn bar(w: [U]) -> U { - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | diff --git a/tests/ui/generics/issue-98432.stderr b/tests/ui/generics/issue-98432.stderr index f20f779d884c0..a1efee78cb7df 100644 --- a/tests/ui/generics/issue-98432.stderr +++ b/tests/ui/generics/issue-98432.stderr @@ -5,7 +5,9 @@ LL | impl Struct { | - type parameter from outer item LL | const CONST: fn() = || { LL | struct _Obligation where T:; - | ^ use of generic parameter from outer item + | ----------- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner struct | help: try introducing a local generic parameter here | diff --git a/tests/ui/resolve/bad-type-env-capture.stderr b/tests/ui/resolve/bad-type-env-capture.stderr index 272672beb4fbb..c565997ca2a26 100644 --- a/tests/ui/resolve/bad-type-env-capture.stderr +++ b/tests/ui/resolve/bad-type-env-capture.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | fn bar(b: T) { } - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | diff --git a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr index fbb9ede8aa175..bc67e9dce4e3b 100644 --- a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr +++ b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.default.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn outer() { // outer function | - type parameter from outer item LL | const K: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it @@ -15,7 +17,9 @@ LL | impl Tr for T { // outer impl block | - type parameter from outer item LL | const C: u32 = { LL | const I: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it @@ -25,7 +29,9 @@ error[E0401]: can't use generic parameters from outer item LL | struct S(U32<{ // outer struct | - type parameter from outer item LL | const _: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it diff --git a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr index 3852b84fee2e0..3959d117c7c9a 100644 --- a/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr +++ b/tests/ui/resolve/generic-params-from-outer-item-in-const-item.generic_const_items.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn outer() { // outer function | - type parameter from outer item LL | const K: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it help: try introducing a local generic parameter here @@ -19,7 +21,9 @@ LL | impl Tr for T { // outer impl block | - type parameter from outer item LL | const C: u32 = { LL | const I: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it help: try introducing a local generic parameter here @@ -33,7 +37,9 @@ error[E0401]: can't use generic parameters from outer item LL | struct S(U32<{ // outer struct | - type parameter from outer item LL | const _: u32 = T::C; - | ^^^^ use of generic parameter from outer item + | - ^^^^ use of generic parameter from outer item + | | + | generic parameter used in this inner constant item | = note: a `const` is a separate item from the item that contains it help: try introducing a local generic parameter here diff --git a/tests/ui/resolve/issue-12796.stderr b/tests/ui/resolve/issue-12796.stderr index 2305971303a71..b5828c6f5fc86 100644 --- a/tests/ui/resolve/issue-12796.stderr +++ b/tests/ui/resolve/issue-12796.stderr @@ -2,10 +2,11 @@ error[E0401]: can't use `Self` from outer item --> $DIR/issue-12796.rs:3:22 | LL | fn inner(_: &Self) { - | ^^^^ - | | - | use of `Self` from outer item - | can't use `Self` here + | ----- ^^^^ + | | | + | | use of `Self` from outer item + | | can't use `Self` here + | `Self` used in this inner function error: aborting due to 1 previous error diff --git a/tests/ui/resolve/issue-3021-c.stderr b/tests/ui/resolve/issue-3021-c.stderr index dcbaf2afd10c6..8c554fd1b97dc 100644 --- a/tests/ui/resolve/issue-3021-c.stderr +++ b/tests/ui/resolve/issue-3021-c.stderr @@ -3,7 +3,9 @@ error[E0401]: can't use generic parameters from outer item | LL | fn siphash() { | - type parameter from outer item -... +LL | +LL | trait U { + | - generic parameter used in this inner trait LL | fn g(&self, x: T) -> T; | ^ use of generic parameter from outer item | @@ -17,7 +19,9 @@ error[E0401]: can't use generic parameters from outer item | LL | fn siphash() { | - type parameter from outer item -... +LL | +LL | trait U { + | - generic parameter used in this inner trait LL | fn g(&self, x: T) -> T; | ^ use of generic parameter from outer item | diff --git a/tests/ui/resolve/issue-3214.stderr b/tests/ui/resolve/issue-3214.stderr index 5e5872f2a5fbd..ab12676bdd805 100644 --- a/tests/ui/resolve/issue-3214.stderr +++ b/tests/ui/resolve/issue-3214.stderr @@ -4,6 +4,7 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | struct Foo { + | --- generic parameter used in this inner struct LL | x: T, | ^ use of generic parameter from outer item | diff --git a/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr b/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr index ca32147d19765..2d21ed0155a70 100644 --- a/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr +++ b/tests/ui/resolve/issue-65025-extern-static-parent-generics.stderr @@ -1,11 +1,14 @@ error[E0401]: can't use generic parameters from outer item --> $DIR/issue-65025-extern-static-parent-generics.rs:3:28 | -LL | unsafe fn foo() { - | - type parameter from outer item -LL | extern "C" { -LL | static baz: *const A; - | ^ use of generic parameter from outer item +LL | unsafe fn foo() { + | - type parameter from outer item +LL | / extern "C" { +LL | | static baz: *const A; + | | ^ use of generic parameter from outer item +LL | | +LL | | } + | |_____- generic parameter used in this inner extern block | = note: a `static` is a separate item from the item that contains it diff --git a/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr b/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr index 98ffb4567f164..869e1729cb853 100644 --- a/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr +++ b/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr @@ -1,11 +1,14 @@ error[E0401]: can't use generic parameters from outer item --> $DIR/issue-65035-static-with-parent-generics.rs:3:26 | -LL | fn f() { - | - type parameter from outer item -LL | extern "C" { -LL | static a: *const T; - | ^ use of generic parameter from outer item +LL | fn f() { + | - type parameter from outer item +LL | / extern "C" { +LL | | static a: *const T; + | | ^ use of generic parameter from outer item +LL | | +LL | | } + | |_____- generic parameter used in this inner extern block | = note: a `static` is a separate item from the item that contains it @@ -15,7 +18,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn g() { | - type parameter from outer item LL | static a: *const T = Default::default(); - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it diff --git a/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr b/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr index 086418df7a253..00aa645688e7f 100644 --- a/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr +++ b/tests/ui/resolve/resolve-type-param-in-item-in-trait.stderr @@ -3,7 +3,9 @@ error[E0401]: can't use generic parameters from outer item | LL | trait TraitA { | - type parameter from outer item -... +LL | fn outer(&self) { +LL | enum Foo { + | --- generic parameter used in this inner enum LL | Variance(A) | ^ use of generic parameter from outer item | @@ -19,7 +21,9 @@ LL | trait TraitB { | - type parameter from outer item LL | fn outer(&self) { LL | struct Foo(A); - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner struct | help: try introducing a local generic parameter here | @@ -33,7 +37,9 @@ LL | trait TraitC { | - type parameter from outer item LL | fn outer(&self) { LL | struct Foo { a: A } - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner struct | help: try introducing a local generic parameter here | @@ -47,7 +53,9 @@ LL | trait TraitD { | - type parameter from outer item LL | fn outer(&self) { LL | fn foo(a: A) { } - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | diff --git a/tests/ui/resolve/use-self-in-inner-fn.rs b/tests/ui/resolve/use-self-in-inner-fn.rs index 62f9dc5664ffe..ed64ee885271c 100644 --- a/tests/ui/resolve/use-self-in-inner-fn.rs +++ b/tests/ui/resolve/use-self-in-inner-fn.rs @@ -6,6 +6,7 @@ impl A { fn peach(this: &Self) { //~^ ERROR can't use `Self` from outer item //~| NOTE use of `Self` from outer item + //~| NOTE `Self` used in this inner function //~| NOTE refer to the type directly here instead } } diff --git a/tests/ui/resolve/use-self-in-inner-fn.stderr b/tests/ui/resolve/use-self-in-inner-fn.stderr index 9c388df8bc20c..645875f6e726d 100644 --- a/tests/ui/resolve/use-self-in-inner-fn.stderr +++ b/tests/ui/resolve/use-self-in-inner-fn.stderr @@ -5,10 +5,11 @@ LL | impl A { | ---- `Self` type implicitly declared here, by this `impl` ... LL | fn peach(this: &Self) { - | ^^^^ - | | - | use of `Self` from outer item - | refer to the type directly here instead + | ----- ^^^^ + | | | + | | use of `Self` from outer item + | | refer to the type directly here instead + | `Self` used in this inner function error: aborting due to 1 previous error diff --git a/tests/ui/statics/static-generic-param-soundness.stderr b/tests/ui/statics/static-generic-param-soundness.stderr index 47554c7fcb0f0..72f65e2bac7cb 100644 --- a/tests/ui/statics/static-generic-param-soundness.stderr +++ b/tests/ui/statics/static-generic-param-soundness.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - type parameter from outer item LL | static a: Bar = Bar::What; - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it diff --git a/tests/ui/type/type-arg-out-of-scope.stderr b/tests/ui/type/type-arg-out-of-scope.stderr index f4fbcdac53b66..3d8850ebccea5 100644 --- a/tests/ui/type/type-arg-out-of-scope.stderr +++ b/tests/ui/type/type-arg-out-of-scope.stderr @@ -4,7 +4,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bar(f: Box T>) { } - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | @@ -17,7 +19,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo(x: T) { | - type parameter from outer item LL | fn bar(f: Box T>) { } - | ^ use of generic parameter from outer item + | --- ^ use of generic parameter from outer item + | | + | generic parameter used in this inner function | help: try introducing a local generic parameter here | From 8fdd34713dc02907cd6c601cfc62315ff8d59406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 1 Nov 2025 19:09:56 +0000 Subject: [PATCH 06/20] Point at inner item on E0401 generated by hir_typeck ``` error[E0401]: can't reference `Self` constructor from outer item --> $DIR/do-not-ice-on-note_and_explain.rs:6:13 | LL | impl A { | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference LL | fn d() { LL | fn d() { | - `Self` used in this inner item LL | Self(1) | ^^^^ help: replace `Self` with the actual type: `A` ``` --- compiler/rustc_hir_typeck/messages.ftl | 2 ++ compiler/rustc_hir_typeck/src/errors.rs | 11 +++++++++++ compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs | 11 +++++++++++ .../malformed/do-not-ice-on-note_and_explain.stderr | 4 +++- tests/ui/self/self-ctor-nongeneric.stderr | 6 +++++- tests/ui/self/self-ctor.stderr | 6 +++++- .../ice-index-out-of-bounds-issue-117446.stderr | 3 +++ 7 files changed, 40 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_hir_typeck/messages.ftl b/compiler/rustc_hir_typeck/messages.ftl index 1ed0756fdd6a7..bb705dce6a4ba 100644 --- a/compiler/rustc_hir_typeck/messages.ftl +++ b/compiler/rustc_hir_typeck/messages.ftl @@ -262,6 +262,8 @@ hir_typeck_self_ctor_from_outer_item = can't reference `Self` constructor from o .label = the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference .suggestion = replace `Self` with the actual type +hir_typeck_self_ctor_from_outer_item_inner_item = `Self` used in this inner item + hir_typeck_slicing_suggestion = consider slicing here hir_typeck_struct_expr_non_exhaustive = diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index d15d092b7d3da..2ed3a542bae8e 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -978,6 +978,15 @@ pub(crate) struct SelfCtorFromOuterItem { pub impl_span: Span, #[subdiagnostic] pub sugg: Option, + #[subdiagnostic] + pub item: Option, +} + +#[derive(Subdiagnostic)] +#[label(hir_typeck_self_ctor_from_outer_item_inner_item)] +pub(crate) struct InnerItem { + #[primary_span] + pub span: Span, } #[derive(LintDiagnostic)] @@ -987,6 +996,8 @@ pub(crate) struct SelfCtorFromOuterItemLint { pub impl_span: Span, #[subdiagnostic] pub sugg: Option, + #[subdiagnostic] + pub item: Option, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index f4e65b42cd423..38586c844445b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -1094,11 +1094,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { span: path_span, name: self.tcx.item_name(def.did()).to_ident_string(), }); + let item = match self + .tcx + .hir_node_by_def_id(self.tcx.hir_get_parent_item(hir_id).def_id) + { + hir::Node::Item(item) => Some(errors::InnerItem { + span: item.kind.ident().map(|i| i.span).unwrap_or(item.span), + }), + _ => None, + }; if ty.raw.has_param() { let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem { span: path_span, impl_span: tcx.def_span(impl_def_id), sugg, + item, }); return (Ty::new_error(self.tcx, guar), res); } else { @@ -1109,6 +1119,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { errors::SelfCtorFromOuterItemLint { impl_span: tcx.def_span(impl_def_id), sugg, + item, }, ); } diff --git a/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr b/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr index 11a8c01e49094..63de7099797ca 100644 --- a/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr +++ b/tests/ui/malformed/do-not-ice-on-note_and_explain.stderr @@ -3,7 +3,9 @@ error[E0401]: can't reference `Self` constructor from outer item | LL | impl A { | ------------ the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference -... +LL | fn d() { +LL | fn d() { + | - `Self` used in this inner item LL | Self(1) | ^^^^ help: replace `Self` with the actual type: `A` diff --git a/tests/ui/self/self-ctor-nongeneric.stderr b/tests/ui/self/self-ctor-nongeneric.stderr index b53ecbe55b594..c3ae7df2ad25e 100644 --- a/tests/ui/self/self-ctor-nongeneric.stderr +++ b/tests/ui/self/self-ctor-nongeneric.stderr @@ -5,7 +5,9 @@ LL | impl S0 { | ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference LL | fn foo() { LL | const C: S0 = Self(0); - | ^^^^ help: replace `Self` with the actual type: `S0` + | - ^^^^ help: replace `Self` with the actual type: `S0` + | | + | `Self` used in this inner item | = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #124186 @@ -17,6 +19,8 @@ warning: can't reference `Self` constructor from outer item LL | impl S0 { | ------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference ... +LL | fn bar() -> S0 { + | --- `Self` used in this inner item LL | Self(0) | ^^^^ help: replace `Self` with the actual type: `S0` | diff --git a/tests/ui/self/self-ctor.stderr b/tests/ui/self/self-ctor.stderr index 0cb22baaa1a05..b8dfd324c3f36 100644 --- a/tests/ui/self/self-ctor.stderr +++ b/tests/ui/self/self-ctor.stderr @@ -5,7 +5,9 @@ LL | impl S0 { | ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference LL | fn foo() { LL | const C: S0 = Self(0); - | ^^^^ help: replace `Self` with the actual type: `S0` + | - ^^^^ help: replace `Self` with the actual type: `S0` + | | + | `Self` used in this inner item error[E0401]: can't reference `Self` constructor from outer item --> $DIR/self-ctor.rs:8:13 @@ -13,6 +15,8 @@ error[E0401]: can't reference `Self` constructor from outer item LL | impl S0 { | ------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference ... +LL | fn bar() -> S0 { + | --- `Self` used in this inner item LL | Self(0) | ^^^^ help: replace `Self` with the actual type: `S0` diff --git a/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr b/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr index ad33a70ed3bb6..2e75003c49b8a 100644 --- a/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr +++ b/tests/ui/traits/ice-index-out-of-bounds-issue-117446.stderr @@ -24,6 +24,9 @@ error[E0401]: can't reference `Self` constructor from outer item LL | impl<'a, T> Foo<'a> for Repeated { | ----------------------------------- the inner item doesn't inherit generics from this impl, so `Self` is invalid to reference ... +LL | fn inner(value: Option<()>) -> Repeated { + | ----- `Self` used in this inner item +LL | match value { LL | _ => Self(unimplemented!()), | ^^^^ help: replace `Self` with the actual type: `Repeated` From 44ece2e9cef484368cfef98f0e287e5a6d1011dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 1 Nov 2025 19:21:27 +0000 Subject: [PATCH 07/20] Point at inner item when using outer const param ``` error[E0401]: can't use generic parameters from outer item --> $DIR/const-param-from-outer-fn.rs:3:9 | LL | fn foo() { | - const parameter from outer item LL | fn bar() -> u32 { | --- generic parameter used in this inner function LL | X | ^ use of generic parameter from outer item | help: try introducing a local generic parameter here | LL | fn bar() -> u32 { | +++ ``` --- compiler/rustc_resolve/src/ident.rs | 14 ++++++++++++- .../early/const-param-from-outer-fn.stderr | 1 + ...e-65035-static-with-parent-generics.stderr | 21 ++++++++++++------- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 4856b1e7cf970..2abf129e94565 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1484,13 +1484,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { // This was an attempt to use a const parameter outside its scope. if let Some(span) = finalize { + let item = if let Some(diag_metadata) = diag_metadata + && let Some(current_item) = diag_metadata.current_item + { + let span = current_item + .kind + .ident() + .map(|i| i.span) + .unwrap_or(current_item.span); + Some((span, current_item.kind.descr().to_string())) + } else { + None + }; self.report_error( span, ResolutionError::GenericParamsFromOuterItem( res, has_generic_params, def_kind, - None, + item, ), ); } diff --git a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr index 0f7c7fbc6be01..3c25dff41aa22 100644 --- a/tests/ui/const-generics/early/const-param-from-outer-fn.stderr +++ b/tests/ui/const-generics/early/const-param-from-outer-fn.stderr @@ -4,6 +4,7 @@ error[E0401]: can't use generic parameters from outer item LL | fn foo() { | - const parameter from outer item LL | fn bar() -> u32 { + | --- generic parameter used in this inner function LL | X | ^ use of generic parameter from outer item | diff --git a/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr b/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr index 869e1729cb853..b22bfb719bd50 100644 --- a/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr +++ b/tests/ui/resolve/issue-65035-static-with-parent-generics.stderr @@ -27,11 +27,14 @@ LL | static a: *const T = Default::default(); error[E0401]: can't use generic parameters from outer item --> $DIR/issue-65035-static-with-parent-generics.rs:15:24 | -LL | fn h() { - | - const parameter from outer item -LL | extern "C" { -LL | static a: [u8; N]; - | ^ use of generic parameter from outer item +LL | fn h() { + | - const parameter from outer item +LL | / extern "C" { +LL | | static a: [u8; N]; + | | ^ use of generic parameter from outer item +LL | | +LL | | } + | |_____- generic parameter used in this inner extern block | = note: a `static` is a separate item from the item that contains it @@ -41,7 +44,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn i() { | - const parameter from outer item LL | static a: [u8; N] = [0; N]; - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it @@ -51,7 +56,9 @@ error[E0401]: can't use generic parameters from outer item LL | fn i() { | - const parameter from outer item LL | static a: [u8; N] = [0; N]; - | ^ use of generic parameter from outer item + | - ^ use of generic parameter from outer item + | | + | generic parameter used in this inner static item | = note: a `static` is a separate item from the item that contains it From 86d755618be25e3b03d16f0c13bdc51d4c2de2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 1 Nov 2025 19:55:35 +0000 Subject: [PATCH 08/20] Pass `DiagMetadata` through in more cases --- compiler/rustc_resolve/src/diagnostics.rs | 7 ++++--- compiler/rustc_resolve/src/ident.rs | 1 + compiler/rustc_resolve/src/late.rs | 12 +++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index f302da356e1e1..4a5d304f49bf9 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -40,7 +40,7 @@ use crate::errors::{ MaybeMissingMacroRulesName, }; use crate::imports::{Import, ImportKind}; -use crate::late::{PatternSource, Rib}; +use crate::late::{DiagMetadata, PatternSource, Rib}; use crate::{ AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, BindingKey, Finalize, ForwardGenericParamBanReason, HasGenericParams, LexicalScopeBinding, MacroRulesScope, Module, @@ -2406,6 +2406,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module: Option>, failed_segment_idx: usize, ident: Ident, + diag_metadata: Option<&DiagMetadata<'_>>, ) -> (String, Option) { let is_last = failed_segment_idx == path.len() - 1; let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS }; @@ -2511,7 +2512,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, &ribs[ns_to_try], ignore_binding, - None, + diag_metadata, ) { // we found a locally-imported or available item/module Some(LexicalScopeBinding::Item(binding)) => Some(binding), @@ -2562,7 +2563,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { None, &ribs[ValueNS], ignore_binding, - None, + diag_metadata, ) } else { None diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 2abf129e94565..2ab024df9a207 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1835,6 +1835,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { module, segment_idx, ident, + diag_metadata, ) }, ); diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 198fef0cd630c..3d79b94c25e79 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -885,7 +885,6 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc TypeNS, Some(Finalize::new(ty.id, ty.span)), None, - None, ) .map_or(Res::Err, |d| d.res()); self.r.record_partial_res(ty.id, PartialRes::new(res)); @@ -1458,7 +1457,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { None, &self.ribs[ns], None, - None, + Some(&self.diag_metadata), ) } @@ -1468,7 +1467,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { ns: Namespace, finalize: Option, ignore_binding: Option>, - diag_metadata: Option<&crate::late::DiagMetadata<'_>>, ) -> Option> { self.r.resolve_ident_in_lexical_scope( ident, @@ -1477,7 +1475,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { finalize, &self.ribs[ns], ignore_binding, - diag_metadata, + Some(&self.diag_metadata), ) } @@ -2556,8 +2554,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { report_error(self, ns); } Some(LexicalScopeBinding::Item(binding)) => { - if let Some(LexicalScopeBinding::Res(..)) = self - .resolve_ident_in_lexical_scope(ident, ns, None, Some(binding), None) + if let Some(LexicalScopeBinding::Res(..)) = + self.resolve_ident_in_lexical_scope(ident, ns, None, Some(binding)) { report_error(self, ns); } @@ -5110,7 +5108,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { // use the type namespace let ns = if i + 1 == path.len() { ns } else { TypeNS }; let res = self.r.partial_res_map.get(&seg.id?)?.full_res()?; - let binding = self.resolve_ident_in_lexical_scope(seg.ident, ns, None, None, None)?; + let binding = self.resolve_ident_in_lexical_scope(seg.ident, ns, None, None)?; (res == binding.res()).then_some((seg, binding)) }); From 72c9762f500ec24b017506d01427594f34bce250 Mon Sep 17 00:00:00 2001 From: "Mark Z. Ding" Date: Sun, 2 Nov 2025 03:45:10 -0500 Subject: [PATCH 09/20] Fix CI failure by ignoring wasm target and cross compile run --- tests/ui/explicit-tail-calls/become-cast-return.rs | 2 ++ tests/ui/explicit-tail-calls/become-indirect-return.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/ui/explicit-tail-calls/become-cast-return.rs b/tests/ui/explicit-tail-calls/become-cast-return.rs index 212a0ddcbca15..d4cdcbf3a0112 100644 --- a/tests/ui/explicit-tail-calls/become-cast-return.rs +++ b/tests/ui/explicit-tail-calls/become-cast-return.rs @@ -1,5 +1,7 @@ //@ run-pass //@ ignore-backends: gcc +//@ ignore-wasm 'tail-call' feature not enabled in target wasm32-wasip1 +//@ ignore-cross-compile #![expect(incomplete_features)] #![feature(explicit_tail_calls)] diff --git a/tests/ui/explicit-tail-calls/become-indirect-return.rs b/tests/ui/explicit-tail-calls/become-indirect-return.rs index 7eec34f3b95c9..b1e552bca0293 100644 --- a/tests/ui/explicit-tail-calls/become-indirect-return.rs +++ b/tests/ui/explicit-tail-calls/become-indirect-return.rs @@ -1,5 +1,7 @@ //@ run-pass //@ ignore-backends: gcc +//@ ignore-wasm 'tail-call' feature not enabled in target wasm32-wasip1 +//@ ignore-cross-compile #![expect(incomplete_features)] #![feature(explicit_tail_calls)] From 964d3af793eeebbe4aa1289ffdffeca6e3d56664 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 29 Oct 2025 12:58:55 +0000 Subject: [PATCH 10/20] Move a special case for rust_eh_personality from reachable_non_generics to cg_llvm This is a workaround for an LLVM bug, so put it in cg_llvm. --- compiler/rustc_codegen_llvm/src/back/lto.rs | 3 +++ compiler/rustc_codegen_ssa/src/back/symbol_export.rs | 7 +------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/back/lto.rs b/compiler/rustc_codegen_llvm/src/back/lto.rs index 5ac3a87c158e6..02b50fa8a6971 100644 --- a/compiler/rustc_codegen_llvm/src/back/lto.rs +++ b/compiler/rustc_codegen_llvm/src/back/lto.rs @@ -77,6 +77,9 @@ fn prepare_lto( // should have default visibility. symbols_below_threshold.push(c"__llvm_profile_counter_bias".to_owned()); + // LTO seems to discard this otherwise under certain circumstances. + symbols_below_threshold.push(c"rust_eh_personality".to_owned()); + // If we're performing LTO for the entire crate graph, then for each of our // upstream dependencies, find the corresponding rlib and load the bitcode // from the archive. diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 5bc18e2d7f8bb..8a1f20826a946 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -104,10 +104,6 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap, _: LocalCrate) -> DefIdMap Date: Wed, 29 Oct 2025 13:06:24 +0000 Subject: [PATCH 11/20] Remove special case for the panic runtime from reachable_non_generics The panic runtime uses #[rustc_std_internal_symbol] which has the same effect as this special case with respect to symbol visibility. --- .../rustc_codegen_ssa/src/back/symbol_export.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 8a1f20826a946..d9ba5017a422d 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -51,15 +51,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap = tcx .reachable_set(()) @@ -104,7 +96,12 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap Date: Sun, 2 Nov 2025 19:58:24 +0000 Subject: [PATCH 12/20] review comments --- compiler/rustc_resolve/src/diagnostics.rs | 10 +++++----- compiler/rustc_resolve/src/ident.rs | 20 ++++++++++---------- compiler/rustc_resolve/src/late.rs | 2 +- compiler/rustc_resolve/src/lib.rs | 7 ++++++- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 4a5d304f49bf9..bc41ffc63c3cd 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -553,12 +553,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { resolution_error: ResolutionError<'ra>, ) -> Diag<'_> { match resolution_error { - ResolutionError::GenericParamsFromOuterItem( + ResolutionError::GenericParamsFromOuterItem { outer_res, has_generic_params, def_kind, - item, - ) => { + inner_item, + } => { use errs::GenericParamsFromOuterItemLabel as Label; let static_or_const = match def_kind { DefKind::Static { .. } => { @@ -576,9 +576,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { sugg: None, static_or_const, is_self, - item: item.map(|(span, descr)| errs::GenericParamsFromOuterItemInnerItem { + item: inner_item.map(|(span, kind)| errs::GenericParamsFromOuterItemInnerItem { span, - descr, + descr: kind.descr().to_string(), }), }; diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index 2ab024df9a207..278c0fe35b7d8 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -1404,18 +1404,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { .ident() .map(|i| i.span) .unwrap_or(current_item.span); - Some((span, current_item.kind.descr().to_string())) + Some((span, current_item.kind.clone())) } else { None }; self.report_error( span, - ResolutionError::GenericParamsFromOuterItem( - res, + ResolutionError::GenericParamsFromOuterItem { + outer_res: res, has_generic_params, def_kind, - item, - ), + inner_item: item, + }, ); } return Res::Err; @@ -1492,18 +1492,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { .ident() .map(|i| i.span) .unwrap_or(current_item.span); - Some((span, current_item.kind.descr().to_string())) + Some((span, current_item.kind.clone())) } else { None }; self.report_error( span, - ResolutionError::GenericParamsFromOuterItem( - res, + ResolutionError::GenericParamsFromOuterItem { + outer_res: res, has_generic_params, def_kind, - item, - ), + inner_item: item, + }, ); } return Res::Err; diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 3d79b94c25e79..7bc5943123c1b 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -677,7 +677,7 @@ pub(crate) struct DiagMetadata<'ast> { /// The current self item if inside an ADT (used for better errors). current_self_item: Option, - /// The current trait (used to suggest). + /// The current item being evaluated (used for suggestions and more detail in errors). pub(crate) current_item: Option<&'ast Item>, /// When processing generic arguments and encountering an unresolved ident not found, diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index c7f5ec76c0751..6b671df433b3d 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -241,7 +241,12 @@ struct BindingError { #[derive(Debug)] enum ResolutionError<'ra> { /// Error E0401: can't use type or const parameters from outer item. - GenericParamsFromOuterItem(Res, HasGenericParams, DefKind, Option<(Span, String)>), + GenericParamsFromOuterItem { + outer_res: Res, + has_generic_params: HasGenericParams, + def_kind: DefKind, + inner_item: Option<(Span, ast::ItemKind)>, + }, /// Error E0403: the name is already used for a type or const parameter in this generic /// parameter list. NameAlreadyUsedInParameterList(Ident, Span), From f171c41a837120c4ba12c9e1567dd44ad46894ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 2 Nov 2025 20:05:48 +0000 Subject: [PATCH 13/20] Do not suggest adding type param to fn delegation --- compiler/rustc_resolve/src/diagnostics.rs | 12 ++++++++---- tests/ui/delegation/target-expr.stderr | 5 ----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index bc41ffc63c3cd..3020ecb6e7113 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -576,9 +576,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { sugg: None, static_or_const, is_self, - item: inner_item.map(|(span, kind)| errs::GenericParamsFromOuterItemInnerItem { - span, - descr: kind.descr().to_string(), + item: inner_item.as_ref().map(|(span, kind)| { + errs::GenericParamsFromOuterItemInnerItem { + span: *span, + descr: kind.descr().to_string(), + } }), }; @@ -613,7 +615,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } }; - if let HasGenericParams::Yes(span) = has_generic_params { + if let HasGenericParams::Yes(span) = has_generic_params + && !matches!(inner_item, Some((_, ItemKind::Delegation(..)))) + { let name = self.tcx.item_name(def_id); let (span, snippet) = if span.is_empty() { let snippet = format!("<{name}>"); diff --git a/tests/ui/delegation/target-expr.stderr b/tests/ui/delegation/target-expr.stderr index f2e6c331193f0..edd1a584eab26 100644 --- a/tests/ui/delegation/target-expr.stderr +++ b/tests/ui/delegation/target-expr.stderr @@ -8,11 +8,6 @@ LL | reuse Trait::static_method { LL | LL | let _ = T::Default(); | ^^^^^^^^^^ use of generic parameter from outer item - | -help: try introducing a local generic parameter here - | -LL | reuse Trait::static_methodT, { - | ++ error[E0434]: can't capture dynamic environment in a fn item --> $DIR/target-expr.rs:26:17 From 528137f97240dab08ed1da076d2634c114b39ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 18 Jul 2025 22:51:15 +0000 Subject: [PATCH 14/20] Point at non-const trait impl when encountering unmet `[const]` bound When encountering an unmet `Ty: [const] Trait` bound, if `Trait` is `#[const_trait]` and there's an `impl Trait for Ty` point at it. If local, suggest `impl const Trait for Ty`, otherwise just point at it. ``` error[E0277]: the trait bound `NonConstAdd: [const] Add` is not satisfied --> $DIR/assoc-type.rs:37:16 | LL | type Bar = NonConstAdd; | ^^^^^^^^^^^ | note: required by a bound in `Foo::Bar` --> $DIR/assoc-type.rs:33:15 | LL | type Bar: [const] Add; | ^^^^^^^^^^^ required by this bound in `Foo::Bar` help: make the `impl` of trait `Add` `const` | LL | impl const Add for NonConstAdd { | +++++ ``` ``` error[E0277]: the trait bound `T: [const] PartialEq` is not satisfied --> tests/ui/traits/const-traits/call-generic-method-fail.rs:5:5 | 5 | *t == *t | ^^^^^^^^ | note: trait `PartialEq` is implemented but not `const` --> /home/gh-estebank/rust/library/core/src/ptr/const_ptr.rs:1590:1 | 1590 | impl PartialEq for *const T { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: trait `PartialEq` is implemented but not `const` --> /home/gh-estebank/rust/library/core/src/ptr/mut_ptr.rs:2011:1 | 2011 | impl PartialEq for *mut T { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` --- .../traits/fulfillment_errors.rs | 28 +++++++++++++++++++ tests/ui/consts/const_cmp_type_id.stderr | 3 ++ .../const-traits/assoc-type.current.stderr | 4 +++ .../const-traits/assoc-type.next.stderr | 4 +++ .../const-traits/call-const-closure.stderr | 5 ++++ .../call-const-trait-method-fail.stderr | 5 ++++ .../call-generic-method-fail.stderr | 5 ++++ .../call-generic-method-nonconst.stderr | 4 +++ .../const-closure-trait-method-fail.stderr | 5 ++++ .../const-default-method-bodies.stderr | 5 ++++ .../const-drop-fail-2.precise.stderr | 4 +++ .../const-drop-fail-2.stock.stderr | 4 +++ .../const-traits/const-opaque.no.stderr | 9 ++++++ .../const-traits/cross-crate.gatednc.stderr | 6 ++++ ...-method-body-is-const-body-checking.stderr | 4 +++ ...-method-body-is-const-same-trait-ck.stderr | 5 ++++ .../item-bound-entailment-fails.stderr | 4 +++ .../const-traits/minicore-deref-fail.stderr | 5 ++++ .../const-traits/minicore-fn-fail.stderr | 4 +++ .../no-explicit-const-params.stderr | 5 ++++ .../specializing-constness-2.stderr | 5 ++++ .../const-traits/super-traits-fail.stderr | 5 ++++ 22 files changed, 128 insertions(+) diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs index 03d43977df965..d2f2c92dda080 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs @@ -839,6 +839,34 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { )) { diag.downgrade_to_delayed_bug(); } + for candidate in self.find_similar_impl_candidates(trait_ref) { + let CandidateSimilarity::Exact { .. } = candidate.similarity else { continue }; + let impl_did = candidate.impl_def_id; + let trait_did = candidate.trait_ref.def_id; + let impl_span = self.tcx.def_span(impl_did); + let trait_name = self.tcx.item_name(trait_did); + + if self.tcx.is_const_trait(trait_did) && !self.tcx.is_const_trait_impl(impl_did) { + if let Some(impl_did) = impl_did.as_local() + && let item = self.tcx.hir_expect_item(impl_did) + && let hir::ItemKind::Impl(item) = item.kind + && let Some(of_trait) = item.of_trait + { + // trait is const, impl is local and not const + diag.span_suggestion_verbose( + of_trait.trait_ref.path.span.shrink_to_lo(), + format!("make the `impl` of trait `{trait_name}` `const`"), + "const ".to_string(), + Applicability::MaybeIncorrect, + ); + } else { + diag.span_note( + impl_span, + format!("trait `{trait_name}` is implemented but not `const`"), + ); + } + } + } diag } diff --git a/tests/ui/consts/const_cmp_type_id.stderr b/tests/ui/consts/const_cmp_type_id.stderr index 62a1677c0d930..b991b6af08cc1 100644 --- a/tests/ui/consts/const_cmp_type_id.stderr +++ b/tests/ui/consts/const_cmp_type_id.stderr @@ -3,6 +3,9 @@ error[E0277]: the trait bound `TypeId: const PartialOrd` is not satisfied | LL | let _a = TypeId::of::() < TypeId::of::(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +note: trait `PartialOrd` is implemented but not `const` + --> $SRC_DIR/core/src/any.rs:LL:COL error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/assoc-type.current.stderr b/tests/ui/traits/const-traits/assoc-type.current.stderr index 1e58efeedeea1..7fe086550f1c2 100644 --- a/tests/ui/traits/const-traits/assoc-type.current.stderr +++ b/tests/ui/traits/const-traits/assoc-type.current.stderr @@ -9,6 +9,10 @@ note: required by a bound in `Foo::Bar` | LL | type Bar: [const] Add; | ^^^^^^^^^^^ required by this bound in `Foo::Bar` +help: make the `impl` of trait `Add` `const` + | +LL | impl const Add for NonConstAdd { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/assoc-type.next.stderr b/tests/ui/traits/const-traits/assoc-type.next.stderr index 1e58efeedeea1..7fe086550f1c2 100644 --- a/tests/ui/traits/const-traits/assoc-type.next.stderr +++ b/tests/ui/traits/const-traits/assoc-type.next.stderr @@ -9,6 +9,10 @@ note: required by a bound in `Foo::Bar` | LL | type Bar: [const] Add; | ^^^^^^^^^^^ required by this bound in `Foo::Bar` +help: make the `impl` of trait `Add` `const` + | +LL | impl const Add for NonConstAdd { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-const-closure.stderr b/tests/ui/traits/const-traits/call-const-closure.stderr index 4bb8b2e9777e6..9de22759c2003 100644 --- a/tests/ui/traits/const-traits/call-const-closure.stderr +++ b/tests/ui/traits/const-traits/call-const-closure.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `(): [const] Bar` is not satisfied | LL | (const || ().foo())(); | ^^^ + | +help: make the `impl` of trait `Bar` `const` + | +LL | impl const Bar for () { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-const-trait-method-fail.stderr b/tests/ui/traits/const-traits/call-const-trait-method-fail.stderr index 4aaf53344c90a..c6f93629379ac 100644 --- a/tests/ui/traits/const-traits/call-const-trait-method-fail.stderr +++ b/tests/ui/traits/const-traits/call-const-trait-method-fail.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `u32: [const] Plus` is not satisfied | LL | a.plus(b) | ^ + | +help: make the `impl` of trait `Plus` `const` + | +LL | impl const Plus for u32 { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-generic-method-fail.stderr b/tests/ui/traits/const-traits/call-generic-method-fail.stderr index a2fba141f7b89..6e75730b9b513 100644 --- a/tests/ui/traits/const-traits/call-generic-method-fail.stderr +++ b/tests/ui/traits/const-traits/call-generic-method-fail.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `T: [const] PartialEq` is not satisfied | LL | *t == *t | ^^^^^^^^ + | +note: trait `PartialEq` is implemented but not `const` + --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL +note: trait `PartialEq` is implemented but not `const` + --> $SRC_DIR/core/src/ptr/mut_ptr.rs:LL:COL error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-generic-method-nonconst.stderr b/tests/ui/traits/const-traits/call-generic-method-nonconst.stderr index 9c1e0fee9e711..b2ec41182a83b 100644 --- a/tests/ui/traits/const-traits/call-generic-method-nonconst.stderr +++ b/tests/ui/traits/const-traits/call-generic-method-nonconst.stderr @@ -11,6 +11,10 @@ note: required by a bound in `equals_self` | LL | const fn equals_self(t: &T) -> bool { | ^^^^^^^^^^^ required by this bound in `equals_self` +help: make the `impl` of trait `Foo` `const` + | +LL | impl const Foo for S { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const-closure-trait-method-fail.stderr b/tests/ui/traits/const-traits/const-closure-trait-method-fail.stderr index fddd8d10bccc4..faacc86512441 100644 --- a/tests/ui/traits/const-traits/const-closure-trait-method-fail.stderr +++ b/tests/ui/traits/const-traits/const-closure-trait-method-fail.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `(): const Tr` is not satisfied | LL | const _: () = assert!(need_const_closure(Tr::a) == 42); | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: make the `impl` of trait `Tr` `const` + | +LL | impl const Tr for () { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const-default-method-bodies.stderr b/tests/ui/traits/const-traits/const-default-method-bodies.stderr index 03ca6f1d51155..22b6a9b5613a8 100644 --- a/tests/ui/traits/const-traits/const-default-method-bodies.stderr +++ b/tests/ui/traits/const-traits/const-default-method-bodies.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `NonConstImpl: [const] ConstDefaultFn` is not sati | LL | NonConstImpl.a(); | ^ + | +help: make the `impl` of trait `ConstDefaultFn` `const` + | +LL | impl const ConstDefaultFn for NonConstImpl { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const-drop-fail-2.precise.stderr b/tests/ui/traits/const-traits/const-drop-fail-2.precise.stderr index c2309ea6e122e..48c8852a7b8d4 100644 --- a/tests/ui/traits/const-traits/const-drop-fail-2.precise.stderr +++ b/tests/ui/traits/const-traits/const-drop-fail-2.precise.stderr @@ -16,6 +16,10 @@ note: required by a bound in `check` | LL | const fn check(_: T) {} | ^^^^^^^^^^^^^^^^ required by this bound in `check` +help: make the `impl` of trait `A` `const` + | +LL | impl const A for NonTrivialDrop {} + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const-drop-fail-2.stock.stderr b/tests/ui/traits/const-traits/const-drop-fail-2.stock.stderr index c2309ea6e122e..48c8852a7b8d4 100644 --- a/tests/ui/traits/const-traits/const-drop-fail-2.stock.stderr +++ b/tests/ui/traits/const-traits/const-drop-fail-2.stock.stderr @@ -16,6 +16,10 @@ note: required by a bound in `check` | LL | const fn check(_: T) {} | ^^^^^^^^^^^^^^^^ required by this bound in `check` +help: make the `impl` of trait `A` `const` + | +LL | impl const A for NonTrivialDrop {} + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const-opaque.no.stderr b/tests/ui/traits/const-traits/const-opaque.no.stderr index acf19ba96ab39..591b81f976745 100644 --- a/tests/ui/traits/const-traits/const-opaque.no.stderr +++ b/tests/ui/traits/const-traits/const-opaque.no.stderr @@ -11,12 +11,21 @@ note: required by a bound in `bar` | LL | const fn bar(t: T) -> impl [const] Foo { | ^^^^^^^^^^^ required by this bound in `bar` +help: make the `impl` of trait `Foo` `const` + | +LL | impl const Foo for () { + | +++++ error[E0277]: the trait bound `(): const Foo` is not satisfied --> $DIR/const-opaque.rs:33:12 | LL | opaque.method(); | ^^^^^^ + | +help: make the `impl` of trait `Foo` `const` + | +LL | impl const Foo for () { + | +++++ error: aborting due to 2 previous errors diff --git a/tests/ui/traits/const-traits/cross-crate.gatednc.stderr b/tests/ui/traits/const-traits/cross-crate.gatednc.stderr index 1da519151182a..fe45ff188efb4 100644 --- a/tests/ui/traits/const-traits/cross-crate.gatednc.stderr +++ b/tests/ui/traits/const-traits/cross-crate.gatednc.stderr @@ -3,6 +3,12 @@ error[E0277]: the trait bound `cross_crate::NonConst: [const] cross_crate::MyTra | LL | NonConst.func(); | ^^^^ + | +note: trait `MyTrait` is implemented but not `const` + --> $DIR/auxiliary/cross-crate.rs:12:1 + | +LL | impl MyTrait for NonConst { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/default-method-body-is-const-body-checking.stderr b/tests/ui/traits/const-traits/default-method-body-is-const-body-checking.stderr index 2e236cecfb475..3ce646ec5027f 100644 --- a/tests/ui/traits/const-traits/default-method-body-is-const-body-checking.stderr +++ b/tests/ui/traits/const-traits/default-method-body-is-const-body-checking.stderr @@ -9,6 +9,10 @@ note: required by a bound in `foo` | LL | const fn foo() where T: [const] Tr {} | ^^^^^^^^^^ required by this bound in `foo` +help: make the `impl` of trait `Tr` `const` + | +LL | impl const Tr for () {} + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/default-method-body-is-const-same-trait-ck.stderr b/tests/ui/traits/const-traits/default-method-body-is-const-same-trait-ck.stderr index 2dc2d48461749..f473a687efcb0 100644 --- a/tests/ui/traits/const-traits/default-method-body-is-const-same-trait-ck.stderr +++ b/tests/ui/traits/const-traits/default-method-body-is-const-same-trait-ck.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `(): [const] Tr` is not satisfied | LL | ().a() | ^ + | +help: make the `impl` of trait `Tr` `const` + | +LL | impl const Tr for () {} + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/item-bound-entailment-fails.stderr b/tests/ui/traits/const-traits/item-bound-entailment-fails.stderr index 8e5894a32966f..3c150c77a6916 100644 --- a/tests/ui/traits/const-traits/item-bound-entailment-fails.stderr +++ b/tests/ui/traits/const-traits/item-bound-entailment-fails.stderr @@ -9,6 +9,10 @@ note: required by a bound in `Foo::Assoc` | LL | type Assoc: [const] Bar | ^^^^^^^^^^^ required by this bound in `Foo::Assoc` +help: make the `impl` of trait `Bar` `const` + | +LL | impl const Bar for N where T: Bar {} + | +++++ error[E0277]: the trait bound `T: [const] Bar` is not satisfied --> $DIR/item-bound-entailment-fails.rs:24:21 diff --git a/tests/ui/traits/const-traits/minicore-deref-fail.stderr b/tests/ui/traits/const-traits/minicore-deref-fail.stderr index 4329b235756be..e6c087cb9d822 100644 --- a/tests/ui/traits/const-traits/minicore-deref-fail.stderr +++ b/tests/ui/traits/const-traits/minicore-deref-fail.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `Ty: [const] minicore::Deref` is not satisfied | LL | *Ty; | ^^^ + | +help: make the `impl` of trait `Deref` `const` + | +LL | impl const Deref for Ty { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/minicore-fn-fail.stderr b/tests/ui/traits/const-traits/minicore-fn-fail.stderr index c02a067774b56..eb840d421f488 100644 --- a/tests/ui/traits/const-traits/minicore-fn-fail.stderr +++ b/tests/ui/traits/const-traits/minicore-fn-fail.stderr @@ -11,6 +11,10 @@ note: required by a bound in `call_indirect` | LL | const fn call_indirect(t: &T) { t() } | ^^^^^^^^^^^^ required by this bound in `call_indirect` +help: make the `impl` of trait `Foo` `const` + | +LL | impl const Foo for () {} + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/no-explicit-const-params.stderr b/tests/ui/traits/const-traits/no-explicit-const-params.stderr index 6955e9ab2985d..47fa65b2479be 100644 --- a/tests/ui/traits/const-traits/no-explicit-const-params.stderr +++ b/tests/ui/traits/const-traits/no-explicit-const-params.stderr @@ -59,6 +59,11 @@ error[E0277]: the trait bound `(): const Bar` is not satisfied | LL | <() as Bar>::bar(); | ^^ + | +help: make the `impl` of trait `Bar` `const` + | +LL | impl const Bar for () { + | +++++ error: aborting due to 5 previous errors diff --git a/tests/ui/traits/const-traits/specializing-constness-2.stderr b/tests/ui/traits/const-traits/specializing-constness-2.stderr index 850e6939daebc..2a34cd1c4f82d 100644 --- a/tests/ui/traits/const-traits/specializing-constness-2.stderr +++ b/tests/ui/traits/const-traits/specializing-constness-2.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `T: [const] A` is not satisfied | LL | ::a(); | ^ + | +help: make the `impl` of trait `A` `const` + | +LL | impl const A for T { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/super-traits-fail.stderr b/tests/ui/traits/const-traits/super-traits-fail.stderr index e19aa30cf95c3..3010a5df0c31f 100644 --- a/tests/ui/traits/const-traits/super-traits-fail.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail.stderr @@ -3,6 +3,11 @@ error[E0277]: the trait bound `S: [const] Foo` is not satisfied | LL | impl const Bar for S {} | ^ + | +help: make the `impl` of trait `Foo` `const` + | +LL | impl const Foo for S { + | +++++ error: aborting due to 1 previous error From a08bdffb21b0493488da82fd3ad7aa8a6a6128ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 19 Jul 2025 00:40:03 +0000 Subject: [PATCH 15/20] Point at non-const trait when using them in const context Point at trait and associated item when that associated item is used in a const context. Suggest making the trait `#[const_trait]`. ``` error[E0015]: cannot call non-const method `<() as Trait>::foo` in constant functions --> $DIR/inline-incorrect-early-bound-in-ctfe.rs:26:8 | LL | ().foo(); | ^^^^^ | note: method `foo` is not const because trait `Trait` is not const --> $DIR/inline-incorrect-early-bound-in-ctfe.rs:13:1 | LL | trait Trait { | ^^^^^^^^^^^ this trait is not const LL | fn foo(self); | ------------- this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Trait` const | LL + #[const_trait] LL | trait Trait { | ``` --- .../rustc_const_eval/src/check_consts/ops.rs | 62 ++++++++++++++++--- tests/ui/asm/non-const.stderr | 5 ++ tests/ui/borrowck/issue-64453.stderr | 2 + tests/ui/consts/const-call.stderr | 5 ++ tests/ui/consts/const-eval/format.stderr | 2 + .../const-extern-fn-call-extern-fn.stderr | 10 +++ .../consts/const-fn-not-safe-for-const.stderr | 5 ++ .../ui/consts/control-flow/issue-46843.stderr | 5 ++ tests/ui/consts/issue-16538.stderr | 5 ++ tests/ui/consts/issue-32829-2.stderr | 15 +++++ tests/ui/consts/issue-43105.stderr | 5 ++ tests/ui/consts/mir_check_nonconst.stderr | 5 ++ tests/ui/error-codes/E0015.stderr | 5 ++ tests/ui/explicit-tail-calls/constck.stderr | 10 +++ tests/ui/resolve/issue-39559-2.stderr | 24 +++++++ .../static-vec-repeat-not-constant.stderr | 5 ++ .../statics/check-values-constraints.stderr | 7 +++ .../const-check-fns-in-const-impl.stderr | 5 ++ ...nline-incorrect-early-bound-in-ctfe.stderr | 12 ++++ .../ui/traits/const-traits/issue-79450.stderr | 2 + .../ui/traits/const-traits/issue-88155.stderr | 12 ++++ .../super-traits-fail-2.nn.stderr | 12 ++++ .../super-traits-fail-2.ny.stderr | 12 ++++ .../super-traits-fail-3.nnn.stderr | 12 ++++ .../super-traits-fail-3.nny.stderr | 12 ++++ .../super-traits-fail-3.ynn.stderr | 12 ++++ .../super-traits-fail-3.yny.stderr | 12 ++++ .../typeck_type_placeholder_item.stderr | 19 ++++++ 28 files changed, 291 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index 79e32dcf105c9..f2eeaebdae6a9 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -1,9 +1,10 @@ //! Concrete error types for all operations which may be invalid in a certain const context. use hir::{ConstContext, LangItem}; -use rustc_errors::Diag; use rustc_errors::codes::*; +use rustc_errors::{Applicability, Diag, MultiSpan}; use rustc_hir as hir; +use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::traits::{ImplSource, Obligation, ObligationCause}; @@ -352,13 +353,58 @@ fn build_error_for_const_call<'tcx>( non_or_conditionally, }) } - _ => ccx.dcx().create_err(errors::NonConstFnCall { - span, - def_descr: ccx.tcx.def_descr(callee), - def_path_str: ccx.tcx.def_path_str_with_args(callee, args), - kind: ccx.const_kind(), - non_or_conditionally, - }), + _ => { + let def_descr = ccx.tcx.def_descr(callee); + let mut err = ccx.dcx().create_err(errors::NonConstFnCall { + span, + def_descr, + def_path_str: ccx.tcx.def_path_str_with_args(callee, args), + kind: ccx.const_kind(), + non_or_conditionally, + }); + let def_kind = ccx.tcx.def_kind(callee); + if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = def_kind { + let parent = ccx.tcx.parent(callee); + if let DefKind::Trait = ccx.tcx.def_kind(parent) + && !ccx.tcx.is_const_trait(parent) + { + let assoc_span = ccx.tcx.def_span(callee); + let assoc_name = ccx.tcx.item_name(callee); + let mut span: MultiSpan = ccx.tcx.def_span(parent).into(); + span.push_span_label(assoc_span, format!("this {def_descr} is not const")); + let trait_descr = ccx.tcx.def_descr(parent); + let trait_span = ccx.tcx.def_span(parent); + let trait_name = ccx.tcx.item_name(parent); + span.push_span_label(trait_span, format!("this {trait_descr} is not const")); + err.span_note( + span, + format!( + "{def_descr} `{assoc_name}` is not const because {trait_descr} \ + `{trait_name}` is not const", + ), + ); + let indentation = ccx + .tcx + .sess + .source_map() + .indentation_before(trait_span) + .unwrap_or_default(); + err.span_suggestion_verbose( + trait_span.shrink_to_lo(), + format!("consider making trait `{trait_name}` const"), + format!("#[const_trait]\n{indentation}"), + Applicability::MachineApplicable, + ); + } + } else if ccx.tcx.constness(callee) != hir::Constness::Const { + let name = ccx.tcx.item_name(callee); + err.span_note( + ccx.tcx.def_span(callee), + format!("{def_descr} `{name}` is not const"), + ); + } + err + } }; err.note(format!( diff --git a/tests/ui/asm/non-const.stderr b/tests/ui/asm/non-const.stderr index eac4fe841bffd..d7a901ba20ee5 100644 --- a/tests/ui/asm/non-const.stderr +++ b/tests/ui/asm/non-const.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `non_const_fn` in constants LL | global_asm!("/* {} */", const non_const_fn(0)); | ^^^^^^^^^^^^^^^ | +note: function `non_const_fn` is not const + --> $DIR/non-const.rs:8:1 + | +LL | fn non_const_fn(x: i32) -> i32 { x } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/issue-64453.stderr b/tests/ui/borrowck/issue-64453.stderr index 8ec9a10f09f4d..29a7363705cd7 100644 --- a/tests/ui/borrowck/issue-64453.stderr +++ b/tests/ui/borrowck/issue-64453.stderr @@ -4,6 +4,8 @@ error[E0015]: cannot call non-const function `format` in statics LL | static settings_dir: String = format!(""); | ^^^^^^^^^^^ | +note: function `format` is not const + --> $SRC_DIR/alloc/src/fmt.rs:LL:COL = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/consts/const-call.stderr b/tests/ui/consts/const-call.stderr index b9dcf5addb519..cc80f73d0bb61 100644 --- a/tests/ui/consts/const-call.stderr +++ b/tests/ui/consts/const-call.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `f` in constants LL | let _ = [0; f(2)]; | ^^^^ | +note: function `f` is not const + --> $DIR/const-call.rs:1:1 + | +LL | fn f(x: usize) -> usize { + | ^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/const-eval/format.stderr b/tests/ui/consts/const-eval/format.stderr index bd50ac0bf4116..ea191eb5c0989 100644 --- a/tests/ui/consts/const-eval/format.stderr +++ b/tests/ui/consts/const-eval/format.stderr @@ -21,6 +21,8 @@ error[E0015]: cannot call non-const function `_print` in constant functions LL | println!("{:?}", 0); | ^^^^^^^^^^^^^^^^^^^ | +note: function `_print` is not const + --> $SRC_DIR/std/src/io/stdio.rs:LL:COL = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr b/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr index 1fa881cf42b6a..ac0d344d8f480 100644 --- a/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr +++ b/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `regular_in_block` in constant func LL | regular_in_block(); | ^^^^^^^^^^^^^^^^^^ | +note: function `regular_in_block` is not const + --> $DIR/const-extern-fn-call-extern-fn.rs:2:5 + | +LL | fn regular_in_block(); + | ^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `regular` in constant functions @@ -12,6 +17,11 @@ error[E0015]: cannot call non-const function `regular` in constant functions LL | regular(); | ^^^^^^^^^ | +note: function `regular` is not const + --> $DIR/const-extern-fn-call-extern-fn.rs:12:1 + | +LL | extern "C" fn regular() {} + | ^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/consts/const-fn-not-safe-for-const.stderr b/tests/ui/consts/const-fn-not-safe-for-const.stderr index e8f0566e73dd8..8fd27df5367e0 100644 --- a/tests/ui/consts/const-fn-not-safe-for-const.stderr +++ b/tests/ui/consts/const-fn-not-safe-for-const.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `random` in constant functions LL | random() | ^^^^^^^^ | +note: function `random` is not const + --> $DIR/const-fn-not-safe-for-const.rs:5:1 + | +LL | fn random() -> u32 { + | ^^^^^^^^^^^^^^^^^^ = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/control-flow/issue-46843.stderr b/tests/ui/consts/control-flow/issue-46843.stderr index 42eb035647c0e..46b2a6bf1fd36 100644 --- a/tests/ui/consts/control-flow/issue-46843.stderr +++ b/tests/ui/consts/control-flow/issue-46843.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `non_const` in constants LL | pub const Q: i32 = match non_const() { | ^^^^^^^^^^^ | +note: function `non_const` is not const + --> $DIR/issue-46843.rs:6:1 + | +LL | fn non_const() -> Thing { + | ^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/issue-16538.stderr b/tests/ui/consts/issue-16538.stderr index 8bd11541a7dff..fd97b4360a468 100644 --- a/tests/ui/consts/issue-16538.stderr +++ b/tests/ui/consts/issue-16538.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `Y::foo` in statics LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | +note: function `foo` is not const + --> $DIR/issue-16538.rs:6:5 + | +LL | pub fn foo(value: *const X) -> *const X { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` diff --git a/tests/ui/consts/issue-32829-2.stderr b/tests/ui/consts/issue-32829-2.stderr index eedd9d34e550e..b201454866ed5 100644 --- a/tests/ui/consts/issue-32829-2.stderr +++ b/tests/ui/consts/issue-32829-2.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `invalid` in constants LL | invalid(); | ^^^^^^^^^ | +note: function `invalid` is not const + --> $DIR/issue-32829-2.rs:68:1 + | +LL | fn invalid() {} + | ^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `invalid` in statics @@ -12,6 +17,11 @@ error[E0015]: cannot call non-const function `invalid` in statics LL | invalid(); | ^^^^^^^^^ | +note: function `invalid` is not const + --> $DIR/issue-32829-2.rs:68:1 + | +LL | fn invalid() {} + | ^^^^^^^^^^^^ = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` @@ -21,6 +31,11 @@ error[E0015]: cannot call non-const function `invalid` in statics LL | invalid(); | ^^^^^^^^^ | +note: function `invalid` is not const + --> $DIR/issue-32829-2.rs:68:1 + | +LL | fn invalid() {} + | ^^^^^^^^^^^^ = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` diff --git a/tests/ui/consts/issue-43105.stderr b/tests/ui/consts/issue-43105.stderr index c030c0f5fcdec..7ec0a2b73afc5 100644 --- a/tests/ui/consts/issue-43105.stderr +++ b/tests/ui/consts/issue-43105.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `xyz` in constants LL | const NUM: u8 = xyz(); | ^^^^^ | +note: function `xyz` is not const + --> $DIR/issue-43105.rs:1:1 + | +LL | fn xyz() -> u8 { 42 } + | ^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/mir_check_nonconst.stderr b/tests/ui/consts/mir_check_nonconst.stderr index e930fa2103d5d..bb0940ee789b9 100644 --- a/tests/ui/consts/mir_check_nonconst.stderr +++ b/tests/ui/consts/mir_check_nonconst.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `bar` in statics LL | static foo: Foo = bar(); | ^^^^^ | +note: function `bar` is not const + --> $DIR/mir_check_nonconst.rs:4:1 + | +LL | fn bar() -> Foo { + | ^^^^^^^^^^^^^^^ = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` diff --git a/tests/ui/error-codes/E0015.stderr b/tests/ui/error-codes/E0015.stderr index 0c983d2843492..bc457aefaecb0 100644 --- a/tests/ui/error-codes/E0015.stderr +++ b/tests/ui/error-codes/E0015.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `create_some` in constants LL | const FOO: Option = create_some(); | ^^^^^^^^^^^^^ | +note: function `create_some` is not const + --> $DIR/E0015.rs:1:1 + | +LL | fn create_some() -> Option { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/explicit-tail-calls/constck.stderr b/tests/ui/explicit-tail-calls/constck.stderr index c223d273b383f..2fc2fb7f9338b 100644 --- a/tests/ui/explicit-tail-calls/constck.stderr +++ b/tests/ui/explicit-tail-calls/constck.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `not_const` in constant functions LL | become not_const(); | ^^^^^^^^^^^ | +note: function `not_const` is not const + --> $DIR/constck.rs:18:1 + | +LL | fn not_const() {} + | ^^^^^^^^^^^^^^ = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `not_const` in constant functions @@ -12,6 +17,11 @@ error[E0015]: cannot call non-const function `not_const` in constant functions LL | become yes_const(not_const()); | ^^^^^^^^^^^ | +note: function `not_const` is not const + --> $DIR/constck.rs:18:1 + | +LL | fn not_const() {} + | ^^^^^^^^^^^^^^ = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/resolve/issue-39559-2.stderr b/tests/ui/resolve/issue-39559-2.stderr index f6e6917d01e6d..8b8b68c7c8c3d 100644 --- a/tests/ui/resolve/issue-39559-2.stderr +++ b/tests/ui/resolve/issue-39559-2.stderr @@ -4,7 +4,19 @@ error[E0015]: cannot call non-const associated function `::dim` in LL | let array: [usize; Dim3::dim()] | ^^^^^^^^^^^ | +note: associated function `dim` is not const because trait `Dim` is not const + --> $DIR/issue-39559-2.rs:1:1 + | +LL | trait Dim { + | ^^^^^^^^^ this trait is not const +LL | fn dim() -> usize; + | ------------------ this associated function is not const = note: calls in constants are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Dim` const + | +LL + #[const_trait] +LL | trait Dim { + | error[E0015]: cannot call non-const associated function `::dim` in constants --> $DIR/issue-39559-2.rs:16:15 @@ -12,7 +24,19 @@ error[E0015]: cannot call non-const associated function `::dim` in LL | = [0; Dim3::dim()]; | ^^^^^^^^^^^ | +note: associated function `dim` is not const because trait `Dim` is not const + --> $DIR/issue-39559-2.rs:1:1 + | +LL | trait Dim { + | ^^^^^^^^^ this trait is not const +LL | fn dim() -> usize; + | ------------------ this associated function is not const = note: calls in constants are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Dim` const + | +LL + #[const_trait] +LL | trait Dim { + | error: aborting due to 2 previous errors diff --git a/tests/ui/static/static-vec-repeat-not-constant.stderr b/tests/ui/static/static-vec-repeat-not-constant.stderr index e6ff199ae0198..fa4033f6a4076 100644 --- a/tests/ui/static/static-vec-repeat-not-constant.stderr +++ b/tests/ui/static/static-vec-repeat-not-constant.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `foo` in statics LL | static a: [isize; 2] = [foo(); 2]; | ^^^^^ | +note: function `foo` is not const + --> $DIR/static-vec-repeat-not-constant.rs:1:1 + | +LL | fn foo() -> isize { 23 } + | ^^^^^^^^^^^^^^^^^ = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` diff --git a/tests/ui/statics/check-values-constraints.stderr b/tests/ui/statics/check-values-constraints.stderr index eb2d37d297e9f..c54f4830533ae 100644 --- a/tests/ui/statics/check-values-constraints.stderr +++ b/tests/ui/statics/check-values-constraints.stderr @@ -35,6 +35,13 @@ error[E0015]: cannot call non-const method `::to_string` in sta LL | field2: SafeEnum::Variant4("str".to_string()), | ^^^^^^^^^^^ | +note: method `to_string` is not const because trait `ToString` is not const + --> $SRC_DIR/alloc/src/string.rs:LL:COL + | + = note: this trait is not const + ::: $SRC_DIR/alloc/src/string.rs:LL:COL + | + = note: this method is not const = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` diff --git a/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr b/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr index 599a5503b0f33..1a7ec35f3ddba 100644 --- a/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr +++ b/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr @@ -4,6 +4,11 @@ error[E0015]: cannot call non-const function `non_const` in constant functions LL | fn foo() { non_const() } | ^^^^^^^^^^^ | +note: function `non_const` is not const + --> $DIR/const-check-fns-in-const-impl.rs:11:1 + | +LL | fn non_const() {} + | ^^^^^^^^^^^^^^ = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr b/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr index ad0829ff05fb0..ca73ae845550d 100644 --- a/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr +++ b/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr @@ -13,7 +13,19 @@ error[E0015]: cannot call non-const method `<() as Trait>::foo` in constant func LL | ().foo(); | ^^^^^ | +note: method `foo` is not const because trait `Trait` is not const + --> $DIR/inline-incorrect-early-bound-in-ctfe.rs:13:1 + | +LL | trait Trait { + | ^^^^^^^^^^^ this trait is not const +LL | fn foo(self); + | ------------- this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Trait` const + | +LL + #[const_trait] +LL | trait Trait { + | error: aborting due to 2 previous errors diff --git a/tests/ui/traits/const-traits/issue-79450.stderr b/tests/ui/traits/const-traits/issue-79450.stderr index 5bdebbbfb032b..d8a9e18980687 100644 --- a/tests/ui/traits/const-traits/issue-79450.stderr +++ b/tests/ui/traits/const-traits/issue-79450.stderr @@ -4,6 +4,8 @@ error[E0015]: cannot call non-const function `_print` in constant functions LL | println!("lul"); | ^^^^^^^^^^^^^^^ | +note: function `_print` is not const + --> $SRC_DIR/std/src/io/stdio.rs:LL:COL = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/traits/const-traits/issue-88155.stderr b/tests/ui/traits/const-traits/issue-88155.stderr index 96a3c4187f5c2..4a912cc8274a7 100644 --- a/tests/ui/traits/const-traits/issue-88155.stderr +++ b/tests/ui/traits/const-traits/issue-88155.stderr @@ -4,7 +4,19 @@ error[E0015]: cannot call non-const associated function `::assoc` in con LL | T::assoc() | ^^^^^^^^^^ | +note: associated function `assoc` is not const because trait `A` is not const + --> $DIR/issue-88155.rs:3:1 + | +LL | pub trait A { + | ^^^^^^^^^^^ this trait is not const +LL | fn assoc() -> bool; + | ------------------- this associated function is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `A` const + | +LL + #[const_trait] +LL | pub trait A { + | error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr b/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr index 0ecbad64bc853..c9dc239bef334 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr @@ -51,7 +51,19 @@ error[E0015]: cannot call non-const method `::a` in constant functions LL | x.a(); | ^^^ | +note: method `a` is not const because trait `Foo` is not const + --> $DIR/super-traits-fail-2.rs:6:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 5 previous errors diff --git a/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr b/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr index 0e5b697d1ddef..bfbf6980ab833 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr @@ -63,7 +63,19 @@ error[E0015]: cannot call non-const method `::a` in constant functions LL | x.a(); | ^^^ | +note: method `a` is not const because trait `Foo` is not const + --> $DIR/super-traits-fail-2.rs:6:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 6 previous errors diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr index a0ae60526ef3f..97e79f28515d0 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr @@ -94,7 +94,19 @@ error[E0015]: cannot call non-const method `::a` in constant functions LL | x.a(); | ^^^ | +note: method `a` is not const because trait `Foo` is not const + --> $DIR/super-traits-fail-3.rs:17:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 9 previous errors diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr index a0ae60526ef3f..97e79f28515d0 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr @@ -94,7 +94,19 @@ error[E0015]: cannot call non-const method `::a` in constant functions LL | x.a(); | ^^^ | +note: method `a` is not const because trait `Foo` is not const + --> $DIR/super-traits-fail-3.rs:17:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 9 previous errors diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr index c8ec77c2f0930..5951caebe7335 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr @@ -74,7 +74,19 @@ error[E0015]: cannot call non-const method `::a` in constant functions LL | x.a(); | ^^^ | +note: method `a` is not const because trait `Foo` is not const + --> $DIR/super-traits-fail-3.rs:17:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 7 previous errors diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr index a820239cde012..563495204ad7b 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr @@ -63,7 +63,19 @@ error[E0015]: cannot call non-const method `::a` in constant functions LL | x.a(); | ^^^ | +note: method `a` is not const because trait `Foo` is not const + --> $DIR/super-traits-fail-3.rs:17:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 6 previous errors diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr index 240dc1ae8ab9e..0b70ac97fd435 100644 --- a/tests/ui/typeck/typeck_type_placeholder_item.stderr +++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr @@ -671,6 +671,11 @@ error[E0015]: cannot call non-const function `map::` in constants LL | const _: Option<_> = map(value); | ^^^^^^^^^^ | +note: function `map` is not const + --> $DIR/typeck_type_placeholder_item.rs:222:1 + | +LL | fn map(_: fn() -> Option<&'static T>) -> Option { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method ` as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}>` in constants @@ -679,6 +684,13 @@ error[E0015]: cannot call non-const method ` as Iterator>:: LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); | ^^^^^^^^^^^^^^^^^^^^^^ | +note: method `filter` is not const because trait `Iterator` is not const + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: this trait is not const + ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: this method is not const = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method `, {closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}> as Iterator>::map::` in constants @@ -687,6 +699,13 @@ error[E0015]: cannot call non-const method `, {closu LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); | ^^^^^^^^^^^^^^ | +note: method `map` is not const because trait `Iterator` is not const + --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: this trait is not const + ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL + | + = note: this method is not const = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 83 previous errors From eb79e68c24ba0156dea9debabbddf7abca8eda6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 19 Jul 2025 01:03:00 +0000 Subject: [PATCH 16/20] Only suggest `#[const_trait]` only on local traits --- .../rustc_const_eval/src/check_consts/ops.rs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index f2eeaebdae6a9..594ef7a69cc6c 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -383,18 +383,20 @@ fn build_error_for_const_call<'tcx>( `{trait_name}` is not const", ), ); - let indentation = ccx - .tcx - .sess - .source_map() - .indentation_before(trait_span) - .unwrap_or_default(); - err.span_suggestion_verbose( - trait_span.shrink_to_lo(), - format!("consider making trait `{trait_name}` const"), - format!("#[const_trait]\n{indentation}"), - Applicability::MachineApplicable, - ); + if parent.is_local() { + let indentation = ccx + .tcx + .sess + .source_map() + .indentation_before(trait_span) + .unwrap_or_default(); + err.span_suggestion_verbose( + trait_span.shrink_to_lo(), + format!("consider making trait `{trait_name}` const"), + format!("#[const_trait]\n{indentation}"), + Applicability::MachineApplicable, + ); + } } } else if ccx.tcx.constness(callee) != hir::Constness::Const { let name = ccx.tcx.item_name(callee); From 9bc814cc8d7219187f1c36957c7efbf12ef8d56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 19 Jul 2025 01:23:52 +0000 Subject: [PATCH 17/20] Point at the enclosing const context ``` error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants --> $DIR/nested-type.rs:15:5 | LL | struct Foo; LL | | LL | | impl Foo { ... | LL | | Foo::<17>::value() | | ^^^^^^^^^^^^^^^^^^ LL | | LL | | }]>; | |_- calls in constants are limited to constant functions, tuple structs and tuple variants ``` --- .../rustc_const_eval/src/check_consts/ops.rs | 18 +++++++--- tests/ui/asm/non-const.stderr | 3 +- tests/ui/borrowck/issue-64453.stderr | 5 +-- .../ui/const-generics/nested-type.full.stderr | 15 ++++++--- .../ui/const-generics/nested-type.min.stderr | 15 ++++++--- tests/ui/consts/const-call.stderr | 3 +- tests/ui/consts/const-eval/format.stderr | 3 +- .../const-extern-fn-call-extern-fn.stderr | 8 +++-- .../consts/const-fn-not-safe-for-const.stderr | 3 +- .../ui/consts/control-flow/issue-46843.stderr | 5 +-- tests/ui/consts/issue-16538.stderr | 5 +-- tests/ui/consts/issue-32829-2.stderr | 12 +++++-- tests/ui/consts/issue-43105.stderr | 5 +-- .../min_const_fn/bad_const_fn_body_ice.stderr | 3 +- tests/ui/consts/mir_check_nonconst.stderr | 5 +-- tests/ui/error-codes/E0010-teach.stderr | 5 +-- tests/ui/error-codes/E0010.stderr | 5 +-- tests/ui/error-codes/E0015.stderr | 5 +-- tests/ui/explicit-tail-calls/constck.stderr | 8 +++-- tests/ui/resolve/issue-39559-2.stderr | 6 ++-- ...lobal-variable-promotion-error-7364.stderr | 5 +-- .../ui/static/static-mut-not-constant.stderr | 5 +-- .../static-vec-repeat-not-constant.stderr | 5 +-- .../statics/check-values-constraints.stderr | 33 ++++++++++++------- .../const-check-fns-in-const-impl.stderr | 5 +-- .../const_derives/derive-const-gate.stderr | 4 +-- .../derive-const-non-const-type.stderr | 4 +-- .../const-traits/cross-crate.stock.stderr | 4 ++- .../const-traits/cross-crate.stocknc.stderr | 9 +++-- ...nline-incorrect-early-bound-in-ctfe.stderr | 3 +- .../ui/traits/const-traits/issue-79450.stderr | 3 +- .../ui/traits/const-traits/issue-88155.stderr | 3 +- .../const-traits/staged-api-user-crate.stderr | 3 +- .../const-traits/std-impl-gate.stock.stderr | 3 +- .../super-traits-fail-2.nn.stderr | 3 +- .../super-traits-fail-2.ny.stderr | 3 +- .../super-traits-fail-3.nnn.stderr | 4 ++- .../super-traits-fail-3.nny.stderr | 4 ++- .../super-traits-fail-3.nyn.stderr | 4 ++- .../super-traits-fail-3.nyy.stderr | 4 ++- .../super-traits-fail-3.ynn.stderr | 4 ++- .../super-traits-fail-3.yny.stderr | 4 ++- .../typeck_type_placeholder_item.stderr | 15 +++++---- 43 files changed, 176 insertions(+), 95 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index 594ef7a69cc6c..55d29f9c21469 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -212,6 +212,7 @@ fn build_error_for_const_call<'tcx>( debug!(?call_kind); + let mut note = true; let mut err = match call_kind { CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => { macro_rules! error { @@ -362,6 +363,12 @@ fn build_error_for_const_call<'tcx>( kind: ccx.const_kind(), non_or_conditionally, }); + let context_span = ccx.tcx.def_span(ccx.def_id()); + err.span_label(context_span, format!( + "calls in {}s are limited to constant functions, tuple structs and tuple variants", + ccx.const_kind(), + )); + note = false; let def_kind = ccx.tcx.def_kind(callee); if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = def_kind { let parent = ccx.tcx.parent(callee); @@ -409,11 +416,12 @@ fn build_error_for_const_call<'tcx>( } }; - err.note(format!( - "calls in {}s are limited to constant functions, \ - tuple structs and tuple variants", - ccx.const_kind(), - )); + if note { + err.note(format!( + "calls in {}s are limited to constant functions, tuple structs and tuple variants", + ccx.const_kind(), + )); + } err } diff --git a/tests/ui/asm/non-const.stderr b/tests/ui/asm/non-const.stderr index d7a901ba20ee5..da881a12f120f 100644 --- a/tests/ui/asm/non-const.stderr +++ b/tests/ui/asm/non-const.stderr @@ -2,14 +2,13 @@ error[E0015]: cannot call non-const function `non_const_fn` in constants --> $DIR/non-const.rs:10:31 | LL | global_asm!("/* {} */", const non_const_fn(0)); - | ^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants | note: function `non_const_fn` is not const --> $DIR/non-const.rs:8:1 | LL | fn non_const_fn(x: i32) -> i32 { x } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/issue-64453.stderr b/tests/ui/borrowck/issue-64453.stderr index 29a7363705cd7..47d9082765555 100644 --- a/tests/ui/borrowck/issue-64453.stderr +++ b/tests/ui/borrowck/issue-64453.stderr @@ -2,11 +2,12 @@ error[E0015]: cannot call non-const function `format` in statics --> $DIR/issue-64453.rs:4:31 | LL | static settings_dir: String = format!(""); - | ^^^^^^^^^^^ + | --------------------------- ^^^^^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | note: function `format` is not const --> $SRC_DIR/alloc/src/fmt.rs:LL:COL - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/const-generics/nested-type.full.stderr b/tests/ui/const-generics/nested-type.full.stderr index e5a1f230380d3..587de095452d1 100644 --- a/tests/ui/const-generics/nested-type.full.stderr +++ b/tests/ui/const-generics/nested-type.full.stderr @@ -1,10 +1,17 @@ error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants --> $DIR/nested-type.rs:15:5 | -LL | Foo::<17>::value() - | ^^^^^^^^^^^^^^^^^^ - | - = note: calls in constants are limited to constant functions, tuple structs and tuple variants +LL | struct Foo; +LL | | +LL | | impl Foo { +... | +LL | | Foo::<17>::value() + | | ^^^^^^^^^^^^^^^^^^ +LL | | +LL | | }]>; + | |_- calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/const-generics/nested-type.min.stderr b/tests/ui/const-generics/nested-type.min.stderr index 8282acd4ea7b1..e7d21f8a98949 100644 --- a/tests/ui/const-generics/nested-type.min.stderr +++ b/tests/ui/const-generics/nested-type.min.stderr @@ -1,10 +1,17 @@ error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants --> $DIR/nested-type.rs:15:5 | -LL | Foo::<17>::value() - | ^^^^^^^^^^^^^^^^^^ - | - = note: calls in constants are limited to constant functions, tuple structs and tuple variants +LL | struct Foo; +LL | | +LL | | impl Foo { +... | +LL | | Foo::<17>::value() + | | ^^^^^^^^^^^^^^^^^^ +LL | | +LL | | }]>; + | |_- calls in constants are limited to constant functions, tuple structs and tuple variants error: `[u8; { struct Foo; diff --git a/tests/ui/consts/const-call.stderr b/tests/ui/consts/const-call.stderr index cc80f73d0bb61..db375f80b6c67 100644 --- a/tests/ui/consts/const-call.stderr +++ b/tests/ui/consts/const-call.stderr @@ -2,14 +2,13 @@ error[E0015]: cannot call non-const function `f` in constants --> $DIR/const-call.rs:6:17 | LL | let _ = [0; f(2)]; - | ^^^^ + | ^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants | note: function `f` is not const --> $DIR/const-call.rs:1:1 | LL | fn f(x: usize) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/const-eval/format.stderr b/tests/ui/consts/const-eval/format.stderr index ea191eb5c0989..97b4436a058ee 100644 --- a/tests/ui/consts/const-eval/format.stderr +++ b/tests/ui/consts/const-eval/format.stderr @@ -18,12 +18,13 @@ LL | println!("{:?}", 0); error[E0015]: cannot call non-const function `_print` in constant functions --> $DIR/format.rs:7:5 | +LL | const fn print() { + | ---------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | println!("{:?}", 0); | ^^^^^^^^^^^^^^^^^^^ | note: function `_print` is not const --> $SRC_DIR/std/src/io/stdio.rs:LL:COL - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0015]: cannot call non-const formatting macro in constant functions diff --git a/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr b/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr index ac0d344d8f480..8f07e038e1994 100644 --- a/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr +++ b/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr @@ -1,6 +1,9 @@ error[E0015]: cannot call non-const function `regular_in_block` in constant functions --> $DIR/const-extern-fn-call-extern-fn.rs:7:9 | +LL | const extern "C" fn bar() { + | ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +LL | unsafe { LL | regular_in_block(); | ^^^^^^^^^^^^^^^^^^ | @@ -9,11 +12,13 @@ note: function `regular_in_block` is not const | LL | fn regular_in_block(); | ^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `regular` in constant functions --> $DIR/const-extern-fn-call-extern-fn.rs:16:9 | +LL | const extern "C" fn foo() { + | ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +LL | unsafe { LL | regular(); | ^^^^^^^^^ | @@ -22,7 +27,6 @@ note: function `regular` is not const | LL | extern "C" fn regular() {} | ^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/consts/const-fn-not-safe-for-const.stderr b/tests/ui/consts/const-fn-not-safe-for-const.stderr index 8fd27df5367e0..068c06601c8b6 100644 --- a/tests/ui/consts/const-fn-not-safe-for-const.stderr +++ b/tests/ui/consts/const-fn-not-safe-for-const.stderr @@ -1,6 +1,8 @@ error[E0015]: cannot call non-const function `random` in constant functions --> $DIR/const-fn-not-safe-for-const.rs:14:5 | +LL | const fn sub1() -> u32 { + | ---------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | random() | ^^^^^^^^ | @@ -9,7 +11,6 @@ note: function `random` is not const | LL | fn random() -> u32 { | ^^^^^^^^^^^^^^^^^^ - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/control-flow/issue-46843.stderr b/tests/ui/consts/control-flow/issue-46843.stderr index 46b2a6bf1fd36..310c860f59b45 100644 --- a/tests/ui/consts/control-flow/issue-46843.stderr +++ b/tests/ui/consts/control-flow/issue-46843.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `non_const` in constants --> $DIR/issue-46843.rs:10:26 | LL | pub const Q: i32 = match non_const() { - | ^^^^^^^^^^^ + | ---------------- ^^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | note: function `non_const` is not const --> $DIR/issue-46843.rs:6:1 | LL | fn non_const() -> Thing { | ^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/issue-16538.stderr b/tests/ui/consts/issue-16538.stderr index fd97b4360a468..e78d95dcd186f 100644 --- a/tests/ui/consts/issue-16538.stderr +++ b/tests/ui/consts/issue-16538.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `Y::foo` in statics --> $DIR/issue-16538.rs:11:23 | LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | note: function `foo` is not const --> $DIR/issue-16538.rs:6:5 | LL | pub fn foo(value: *const X) -> *const X { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block diff --git a/tests/ui/consts/issue-32829-2.stderr b/tests/ui/consts/issue-32829-2.stderr index b201454866ed5..52398af41eeef 100644 --- a/tests/ui/consts/issue-32829-2.stderr +++ b/tests/ui/consts/issue-32829-2.stderr @@ -1,6 +1,9 @@ error[E0015]: cannot call non-const function `invalid` in constants --> $DIR/issue-32829-2.rs:10:9 | +LL | const bad_two : u32 = { + | ------------------- calls in constants are limited to constant functions, tuple structs and tuple variants +LL | { LL | invalid(); | ^^^^^^^^^ | @@ -9,11 +12,13 @@ note: function `invalid` is not const | LL | fn invalid() {} | ^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `invalid` in statics --> $DIR/issue-32829-2.rs:32:9 | +LL | static bad_five : u32 = { + | --------------------- calls in statics are limited to constant functions, tuple structs and tuple variants +LL | { LL | invalid(); | ^^^^^^^^^ | @@ -22,12 +27,14 @@ note: function `invalid` is not const | LL | fn invalid() {} | ^^^^^^^^^^^^ - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error[E0015]: cannot call non-const function `invalid` in statics --> $DIR/issue-32829-2.rs:54:9 | +LL | static mut bad_eight : u32 = { + | -------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants +LL | { LL | invalid(); | ^^^^^^^^^ | @@ -36,7 +43,6 @@ note: function `invalid` is not const | LL | fn invalid() {} | ^^^^^^^^^^^^ - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 3 previous errors diff --git a/tests/ui/consts/issue-43105.stderr b/tests/ui/consts/issue-43105.stderr index 7ec0a2b73afc5..442fbdd107fab 100644 --- a/tests/ui/consts/issue-43105.stderr +++ b/tests/ui/consts/issue-43105.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `xyz` in constants --> $DIR/issue-43105.rs:3:17 | LL | const NUM: u8 = xyz(); - | ^^^^^ + | ------------- ^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | note: function `xyz` is not const --> $DIR/issue-43105.rs:1:1 | LL | fn xyz() -> u8 { 42 } | ^^^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr index 8e52a7aa35e1e..c65abc530bc2b 100644 --- a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr +++ b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr @@ -9,10 +9,11 @@ LL | vec![1, 2, 3] error[E0015]: cannot call non-const method `slice::::into_vec::` in constant functions --> $DIR/bad_const_fn_body_ice.rs:2:5 | +LL | const fn foo(a: i32) -> Vec { + | -------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | vec![1, 2, 3] | ^^^^^^^^^^^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors diff --git a/tests/ui/consts/mir_check_nonconst.stderr b/tests/ui/consts/mir_check_nonconst.stderr index bb0940ee789b9..ad2056a737078 100644 --- a/tests/ui/consts/mir_check_nonconst.stderr +++ b/tests/ui/consts/mir_check_nonconst.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `bar` in statics --> $DIR/mir_check_nonconst.rs:8:19 | LL | static foo: Foo = bar(); - | ^^^^^ + | --------------- ^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | note: function `bar` is not const --> $DIR/mir_check_nonconst.rs:4:1 | LL | fn bar() -> Foo { | ^^^^^^^^^^^^^^^ - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0010-teach.stderr b/tests/ui/error-codes/E0010-teach.stderr index 82bbe01aef792..237efbefcf6ef 100644 --- a/tests/ui/error-codes/E0010-teach.stderr +++ b/tests/ui/error-codes/E0010-teach.stderr @@ -11,9 +11,10 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/E0010-teach.rs:5:23 | LL | const CON: Vec = vec![1, 2, 3]; - | ^^^^^^^^^^^^^ + | ------------------- ^^^^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | - = note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors diff --git a/tests/ui/error-codes/E0010.stderr b/tests/ui/error-codes/E0010.stderr index 87b722b5f6566..2bb87118d242d 100644 --- a/tests/ui/error-codes/E0010.stderr +++ b/tests/ui/error-codes/E0010.stderr @@ -10,9 +10,10 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/E0010.rs:3:23 | LL | const CON: Vec = vec![1, 2, 3]; - | ^^^^^^^^^^^^^ + | ------------------- ^^^^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | - = note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors diff --git a/tests/ui/error-codes/E0015.stderr b/tests/ui/error-codes/E0015.stderr index bc457aefaecb0..4331d7c34b3ca 100644 --- a/tests/ui/error-codes/E0015.stderr +++ b/tests/ui/error-codes/E0015.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `create_some` in constants --> $DIR/E0015.rs:5:25 | LL | const FOO: Option = create_some(); - | ^^^^^^^^^^^^^ + | --------------------- ^^^^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | note: function `create_some` is not const --> $DIR/E0015.rs:1:1 | LL | fn create_some() -> Option { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/explicit-tail-calls/constck.stderr b/tests/ui/explicit-tail-calls/constck.stderr index 2fc2fb7f9338b..84d7bc3a1bf13 100644 --- a/tests/ui/explicit-tail-calls/constck.stderr +++ b/tests/ui/explicit-tail-calls/constck.stderr @@ -1,6 +1,9 @@ error[E0015]: cannot call non-const function `not_const` in constant functions --> $DIR/constck.rs:6:16 | +LL | const fn f() { + | ------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants +LL | if false { LL | become not_const(); | ^^^^^^^^^^^ | @@ -9,11 +12,13 @@ note: function `not_const` is not const | LL | fn not_const() {} | ^^^^^^^^^^^^^^ - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `not_const` in constant functions --> $DIR/constck.rs:13:26 | +LL | const fn g((): ()) { + | ------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants +LL | if false { LL | become yes_const(not_const()); | ^^^^^^^^^^^ | @@ -22,7 +27,6 @@ note: function `not_const` is not const | LL | fn not_const() {} | ^^^^^^^^^^^^^^ - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/resolve/issue-39559-2.stderr b/tests/ui/resolve/issue-39559-2.stderr index 8b8b68c7c8c3d..8c19fb23d8e9a 100644 --- a/tests/ui/resolve/issue-39559-2.stderr +++ b/tests/ui/resolve/issue-39559-2.stderr @@ -2,7 +2,7 @@ error[E0015]: cannot call non-const associated function `::dim` in --> $DIR/issue-39559-2.rs:14:24 | LL | let array: [usize; Dim3::dim()] - | ^^^^^^^^^^^ + | ^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants | note: associated function `dim` is not const because trait `Dim` is not const --> $DIR/issue-39559-2.rs:1:1 @@ -11,7 +11,6 @@ LL | trait Dim { | ^^^^^^^^^ this trait is not const LL | fn dim() -> usize; | ------------------ this associated function is not const - = note: calls in constants are limited to constant functions, tuple structs and tuple variants help: consider making trait `Dim` const | LL + #[const_trait] @@ -22,7 +21,7 @@ error[E0015]: cannot call non-const associated function `::dim` in --> $DIR/issue-39559-2.rs:16:15 | LL | = [0; Dim3::dim()]; - | ^^^^^^^^^^^ + | ^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants | note: associated function `dim` is not const because trait `Dim` is not const --> $DIR/issue-39559-2.rs:1:1 @@ -31,7 +30,6 @@ LL | trait Dim { | ^^^^^^^^^ this trait is not const LL | fn dim() -> usize; | ------------------ this associated function is not const - = note: calls in constants are limited to constant functions, tuple structs and tuple variants help: consider making trait `Dim` const | LL + #[const_trait] diff --git a/tests/ui/static/global-variable-promotion-error-7364.stderr b/tests/ui/static/global-variable-promotion-error-7364.stderr index b9d75676bef89..bced8c1520202 100644 --- a/tests/ui/static/global-variable-promotion-error-7364.stderr +++ b/tests/ui/static/global-variable-promotion-error-7364.stderr @@ -15,9 +15,10 @@ error[E0015]: cannot call non-const associated function `Box::>:: --> $DIR/global-variable-promotion-error-7364.rs:5:37 | LL | static boxed: Box> = Box::new(RefCell::new(0)); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | --------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 2 previous errors diff --git a/tests/ui/static/static-mut-not-constant.stderr b/tests/ui/static/static-mut-not-constant.stderr index f28ea0b1689ab..7e64d36b26d17 100644 --- a/tests/ui/static/static-mut-not-constant.stderr +++ b/tests/ui/static/static-mut-not-constant.stderr @@ -2,9 +2,10 @@ error[E0015]: cannot call non-const associated function `Box::::new` in s --> $DIR/static-mut-not-constant.rs:1:28 | LL | static mut a: Box = Box::new(3); - | ^^^^^^^^^^^ + | ------------------------ ^^^^^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 1 previous error diff --git a/tests/ui/static/static-vec-repeat-not-constant.stderr b/tests/ui/static/static-vec-repeat-not-constant.stderr index fa4033f6a4076..24f0986ccc494 100644 --- a/tests/ui/static/static-vec-repeat-not-constant.stderr +++ b/tests/ui/static/static-vec-repeat-not-constant.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `foo` in statics --> $DIR/static-vec-repeat-not-constant.rs:3:25 | LL | static a: [isize; 2] = [foo(); 2]; - | ^^^^^ + | -------------------- ^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | note: function `foo` is not const --> $DIR/static-vec-repeat-not-constant.rs:1:1 | LL | fn foo() -> isize { 23 } | ^^^^^^^^^^^^^^^^^ - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 1 previous error diff --git a/tests/ui/statics/check-values-constraints.stderr b/tests/ui/statics/check-values-constraints.stderr index c54f4830533ae..dfbb1f6e985c3 100644 --- a/tests/ui/statics/check-values-constraints.stderr +++ b/tests/ui/statics/check-values-constraints.stderr @@ -23,15 +23,19 @@ error[E0015]: cannot call non-const method `slice::::into_vec::< --> $DIR/check-values-constraints.rs:81:33 | LL | static STATIC11: Vec = vec![MyOwned]; - | ^^^^^^^^^^^^^ + | ----------------------------- ^^^^^^^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0015]: cannot call non-const method `::to_string` in statics --> $DIR/check-values-constraints.rs:92:38 | +LL | static mut STATIC14: SafeStruct = SafeStruct { + | ------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants +LL | field1: SafeEnum::Variant1, LL | field2: SafeEnum::Variant4("str".to_string()), | ^^^^^^^^^^^ | @@ -42,7 +46,6 @@ note: method `to_string` is not const because trait `ToString` is not const ::: $SRC_DIR/alloc/src/string.rs:LL:COL | = note: this method is not const - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error[E0010]: allocations are not allowed in statics @@ -56,10 +59,11 @@ LL | vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:96:5 | +LL | static STATIC15: &'static [Vec] = &[ + | ---------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants LL | vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -74,10 +78,12 @@ LL | vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:98:5 | +LL | static STATIC15: &'static [Vec] = &[ + | ---------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants +... LL | vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -92,10 +98,11 @@ LL | &vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:103:6 | +LL | static STATIC16: (&'static Vec, &'static Vec) = ( + | --------------------------------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants LL | &vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -110,10 +117,12 @@ LL | &vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:105:6 | +LL | static STATIC16: (&'static Vec, &'static Vec) = ( + | --------------------------------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants +... LL | &vec![MyOwned], | ^^^^^^^^^^^^^ | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -129,9 +138,10 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/check-values-constraints.rs:111:31 | LL | static STATIC19: Vec = vec![3]; - | ^^^^^^^ + | --------------------------- ^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -147,9 +157,10 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/check-values-constraints.rs:117:32 | LL | static x: Vec = vec![3]; - | ^^^^^^^ + | -------------------- ^^^^^^^ + | | + | calls in statics are limited to constant functions, tuple structs and tuple variants | - = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr b/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr index 1a7ec35f3ddba..70704780ddce7 100644 --- a/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr +++ b/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr @@ -2,14 +2,15 @@ error[E0015]: cannot call non-const function `non_const` in constant functions --> $DIR/const-check-fns-in-const-impl.rs:14:16 | LL | fn foo() { non_const() } - | ^^^^^^^^^^^ + | -------- ^^^^^^^^^^^ + | | + | calls in constant functions are limited to constant functions, tuple structs and tuple variants | note: function `non_const` is not const --> $DIR/const-check-fns-in-const-impl.rs:11:1 | LL | fn non_const() {} | ^^^^^^^^^^^^^^ - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr b/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr index cbc62d602a473..ac393825ec720 100644 --- a/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr +++ b/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr @@ -21,9 +21,7 @@ error[E0015]: cannot call non-const method `Formatter::<'_>::write_str` in const --> $DIR/derive-const-gate.rs:1:16 | LL | #[derive_const(Debug)] - | ^^^^^ - | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants + | ^^^^^ calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 3 previous errors diff --git a/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr b/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr index 93638801895da..9f620fcf81b7c 100644 --- a/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr +++ b/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr @@ -11,9 +11,7 @@ error[E0015]: cannot call non-const method `Formatter::<'_>::debug_tuple_field1_ --> $DIR/derive-const-non-const-type.rs:12:16 | LL | #[derive_const(Debug)] - | ^^^^^ - | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants + | ^^^^^ calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/traits/const-traits/cross-crate.stock.stderr b/tests/ui/traits/const-traits/cross-crate.stock.stderr index 44a60c99ae9ea..89278ff56ad0c 100644 --- a/tests/ui/traits/const-traits/cross-crate.stock.stderr +++ b/tests/ui/traits/const-traits/cross-crate.stock.stderr @@ -1,10 +1,12 @@ error[E0658]: cannot call conditionally-const method `::func` in constant functions --> $DIR/cross-crate.rs:22:11 | +LL | const fn const_context() { + | ------------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | Const.func(); | ^^^^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/cross-crate.stocknc.stderr b/tests/ui/traits/const-traits/cross-crate.stocknc.stderr index 766c20aa8211f..6269e74fd1d2b 100644 --- a/tests/ui/traits/const-traits/cross-crate.stocknc.stderr +++ b/tests/ui/traits/const-traits/cross-crate.stocknc.stderr @@ -1,18 +1,21 @@ error[E0015]: cannot call non-const method `::func` in constant functions --> $DIR/cross-crate.rs:19:14 | +LL | const fn const_context() { + | ------------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants +LL | #[cfg(any(stocknc, gatednc))] LL | NonConst.func(); | ^^^^^^ - | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0658]: cannot call conditionally-const method `::func` in constant functions --> $DIR/cross-crate.rs:22:11 | +LL | const fn const_context() { + | ------------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | Const.func(); | ^^^^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr b/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr index ca73ae845550d..092a5ac05b823 100644 --- a/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr +++ b/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr @@ -10,6 +10,8 @@ LL | fn foo(self) { error[E0015]: cannot call non-const method `<() as Trait>::foo` in constant functions --> $DIR/inline-incorrect-early-bound-in-ctfe.rs:26:8 | +LL | const fn foo() { + | -------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | ().foo(); | ^^^^^ | @@ -20,7 +22,6 @@ LL | trait Trait { | ^^^^^^^^^^^ this trait is not const LL | fn foo(self); | ------------- this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Trait` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/issue-79450.stderr b/tests/ui/traits/const-traits/issue-79450.stderr index d8a9e18980687..dd78b464e7825 100644 --- a/tests/ui/traits/const-traits/issue-79450.stderr +++ b/tests/ui/traits/const-traits/issue-79450.stderr @@ -1,12 +1,13 @@ error[E0015]: cannot call non-const function `_print` in constant functions --> $DIR/issue-79450.rs:9:9 | +LL | fn prov(&self) { + | -------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | println!("lul"); | ^^^^^^^^^^^^^^^ | note: function `_print` is not const --> $SRC_DIR/std/src/io/stdio.rs:LL:COL - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/issue-88155.stderr b/tests/ui/traits/const-traits/issue-88155.stderr index 4a912cc8274a7..2df4a0004b6a6 100644 --- a/tests/ui/traits/const-traits/issue-88155.stderr +++ b/tests/ui/traits/const-traits/issue-88155.stderr @@ -1,6 +1,8 @@ error[E0015]: cannot call non-const associated function `::assoc` in constant functions --> $DIR/issue-88155.rs:8:5 | +LL | pub const fn foo() -> bool { + | -------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | T::assoc() | ^^^^^^^^^^ | @@ -11,7 +13,6 @@ LL | pub trait A { | ^^^^^^^^^^^ this trait is not const LL | fn assoc() -> bool; | ------------------- this associated function is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `A` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/staged-api-user-crate.stderr b/tests/ui/traits/const-traits/staged-api-user-crate.stderr index 81611da9e748c..1baa8b308284e 100644 --- a/tests/ui/traits/const-traits/staged-api-user-crate.stderr +++ b/tests/ui/traits/const-traits/staged-api-user-crate.stderr @@ -1,10 +1,11 @@ error[E0658]: cannot call conditionally-const associated function `::func` in constant functions --> $DIR/staged-api-user-crate.rs:12:5 | +LL | const fn stable_const_context() { + | ------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | Unstable::func(); | ^^^^^^^^^^^^^^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/std-impl-gate.stock.stderr b/tests/ui/traits/const-traits/std-impl-gate.stock.stderr index 261f68bebdb25..3f40f602eefe2 100644 --- a/tests/ui/traits/const-traits/std-impl-gate.stock.stderr +++ b/tests/ui/traits/const-traits/std-impl-gate.stock.stderr @@ -1,10 +1,11 @@ error[E0658]: cannot call conditionally-const associated function ` as Default>::default` in constant functions --> $DIR/std-impl-gate.rs:13:5 | +LL | const fn const_context() -> Vec { + | -------------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | Default::default() | ^^^^^^^^^^^^^^^^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr b/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr index c9dc239bef334..84fc743e4a2b7 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr @@ -48,6 +48,8 @@ LL | #[const_trait] trait Foo { error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-2.rs:20:7 | +LL | const fn foo(x: &T) { + | --------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | @@ -58,7 +60,6 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr b/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr index bfbf6980ab833..9d72e149aab28 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr @@ -60,6 +60,8 @@ LL | #[const_trait] trait Foo { error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-2.rs:20:7 | +LL | const fn foo(x: &T) { + | --------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | @@ -70,7 +72,6 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr index 97e79f28515d0..8a35dd966425a 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr @@ -91,6 +91,9 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | +LL | const fn foo(x: &T) { + | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | x.a(); | ^^^ | @@ -101,7 +104,6 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr index 97e79f28515d0..8a35dd966425a 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr @@ -91,6 +91,9 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | +LL | const fn foo(x: &T) { + | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | x.a(); | ^^^ | @@ -101,7 +104,6 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr index b00ad706a5fa1..155263656e368 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr @@ -41,10 +41,12 @@ LL | #[cfg_attr(any(yyy, yny, nyy, nyn), const_trait)] error[E0658]: cannot call conditionally-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | +LL | const fn foo(x: &T) { + | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | x.a(); | ^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr index b00ad706a5fa1..155263656e368 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr @@ -41,10 +41,12 @@ LL | #[cfg_attr(any(yyy, yny, nyy, nyn), const_trait)] error[E0658]: cannot call conditionally-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | +LL | const fn foo(x: &T) { + | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | x.a(); | ^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr index 5951caebe7335..4895b5b0cd08b 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr @@ -71,6 +71,9 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | +LL | const fn foo(x: &T) { + | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | x.a(); | ^^^ | @@ -81,7 +84,6 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr index 563495204ad7b..29bce84326cab 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr @@ -60,6 +60,9 @@ LL | #[const_trait] trait Foo { error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | +LL | const fn foo(x: &T) { + | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants +... LL | x.a(); | ^^^ | @@ -70,7 +73,6 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr index 0b70ac97fd435..0b0f9fbb9be11 100644 --- a/tests/ui/typeck/typeck_type_placeholder_item.stderr +++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr @@ -669,20 +669,23 @@ error[E0015]: cannot call non-const function `map::` in constants --> $DIR/typeck_type_placeholder_item.rs:231:22 | LL | const _: Option<_> = map(value); - | ^^^^^^^^^^ + | ------------------ ^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | note: function `map` is not const --> $DIR/typeck_type_placeholder_item.rs:222:1 | LL | fn map(_: fn() -> Option<&'static T>) -> Option { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method ` as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}>` in constants --> $DIR/typeck_type_placeholder_item.rs:240:22 | LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); - | ^^^^^^^^^^^^^^^^^^^^^^ + | ---------- ^^^^^^^^^^^^^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | note: method `filter` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL @@ -691,13 +694,14 @@ note: method `filter` is not const because trait `Iterator` is not const ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this method is not const - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method `, {closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}> as Iterator>::map::` in constants --> $DIR/typeck_type_placeholder_item.rs:240:45 | LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); - | ^^^^^^^^^^^^^^ + | ---------- ^^^^^^^^^^^^^^ + | | + | calls in constants are limited to constant functions, tuple structs and tuple variants | note: method `map` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL @@ -706,7 +710,6 @@ note: method `map` is not const because trait `Iterator` is not const ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this method is not const - = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 83 previous errors From bf45da7e517b248d47a2fc08fa3dc42855227a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 19 Jul 2025 21:42:15 +0000 Subject: [PATCH 18/20] Account for `#![feature(const_trait_impl)]` state --- .../rustc_const_eval/src/check_consts/ops.rs | 10 +++++++++- .../const-super-trait-nightly-disabled.stderr | 16 +++++++++++++++- .../const-super-trait-nightly-enabled.stderr | 15 ++++++++++++++- .../const-super-trait-stable-disabled.stderr | 11 ++++++++++- .../const-super-trait-stable-enabled.stderr | 11 ++++++++++- tests/ui/resolve/issue-39559-2.stderr | 2 ++ .../const-traits/super-traits-fail-3.nnn.stderr | 1 + .../const-traits/super-traits-fail-3.nny.stderr | 1 + 8 files changed, 62 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index 55d29f9c21469..c958a91f754bb 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -390,7 +390,13 @@ fn build_error_for_const_call<'tcx>( `{trait_name}` is not const", ), ); - if parent.is_local() { + if parent.is_local() && ccx.tcx.sess.is_nightly_build() { + if !ccx.tcx.features().const_trait_impl() { + err.help( + "add `#![feature(const_trait_impl)]` to the crate attributes to \ + enable `#[const_trait]`", + ); + } let indentation = ccx .tcx .sess @@ -403,6 +409,8 @@ fn build_error_for_const_call<'tcx>( format!("#[const_trait]\n{indentation}"), Applicability::MachineApplicable, ); + } else if !ccx.tcx.sess.is_nightly_build() { + err.help("const traits are not yet supported on stable Rust"); } } } else if ccx.tcx.constness(callee) != hir::Constness::Const { diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr index 895558f3b0fcb..47f6817b9b64d 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr @@ -55,10 +55,24 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | +LL | const fn foo(x: &T) { + | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +note: method `a` is not const because trait `Foo` is not const + --> const-super-trait.rs:3:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const + = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 6 previous errors diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr index 821ab6fa57cd0..49e2f98c371f3 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr @@ -35,10 +35,23 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | +LL | const fn foo(x: &T) { + | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +note: method `a` is not const because trait `Foo` is not const + --> const-super-trait.rs:3:1 + | +LL | trait Foo { + | ^^^^^^^^^ this trait is not const +LL | fn a(&self); + | ------------ this method is not const +help: consider making trait `Foo` const + | +LL + #[const_trait] +LL | trait Foo { + | error: aborting due to 4 previous errors diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr index b39be78e4384c..62fd9a38b7591 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr @@ -53,10 +53,19 @@ note: `Bar` can't be used with `[const]` because it isn't `const` error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | +9 | const fn foo(x: &T) { + | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants 10 | x.a(); | ^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +note: method `a` is not const because trait `Foo` is not const + --> const-super-trait.rs:3:1 + | +3 | trait Foo { + | ^^^^^^^^^ this trait is not const +4 | fn a(&self); + | ------------ this method is not const + = help: const traits are not yet supported on stable Rust error: aborting due to 6 previous errors diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr index 30ee5cf6f4b0c..fc1adbe441cef 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr @@ -43,10 +43,19 @@ note: `Bar` can't be used with `[const]` because it isn't `const` error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | +9 | const fn foo(x: &T) { + | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants 10 | x.a(); | ^^^ | - = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants +note: method `a` is not const because trait `Foo` is not const + --> const-super-trait.rs:3:1 + | +3 | trait Foo { + | ^^^^^^^^^ this trait is not const +4 | fn a(&self); + | ------------ this method is not const + = help: const traits are not yet supported on stable Rust error: aborting due to 5 previous errors diff --git a/tests/ui/resolve/issue-39559-2.stderr b/tests/ui/resolve/issue-39559-2.stderr index 8c19fb23d8e9a..fbbcaa663ae0d 100644 --- a/tests/ui/resolve/issue-39559-2.stderr +++ b/tests/ui/resolve/issue-39559-2.stderr @@ -11,6 +11,7 @@ LL | trait Dim { | ^^^^^^^^^ this trait is not const LL | fn dim() -> usize; | ------------------ this associated function is not const + = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` help: consider making trait `Dim` const | LL + #[const_trait] @@ -30,6 +31,7 @@ LL | trait Dim { | ^^^^^^^^^ this trait is not const LL | fn dim() -> usize; | ------------------ this associated function is not const + = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` help: consider making trait `Dim` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr index 8a35dd966425a..2625f94460616 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr @@ -104,6 +104,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr index 8a35dd966425a..2625f94460616 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr @@ -104,6 +104,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` help: consider making trait `Foo` const | LL + #[const_trait] From a2c3913007fa77728464b9d37d827e013f127f6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 23 Jul 2025 18:46:03 +0000 Subject: [PATCH 19/20] review comments --- .../rustc_const_eval/src/check_consts/ops.rs | 31 ++++++----------- .../const-super-trait-nightly-disabled.stderr | 3 +- .../const-super-trait-nightly-enabled.stderr | 3 +- .../const-super-trait-stable-disabled.stderr | 3 +- .../const-super-trait-stable-enabled.stderr | 3 +- tests/ui/asm/non-const.stderr | 3 +- tests/ui/borrowck/issue-64453.stderr | 5 ++- .../ui/const-generics/nested-type.full.stderr | 15 +++------ .../ui/const-generics/nested-type.min.stderr | 15 +++------ tests/ui/consts/const-call.stderr | 3 +- tests/ui/consts/const-eval/format.stderr | 3 +- .../const-extern-fn-call-extern-fn.stderr | 8 ++--- .../consts/const-fn-not-safe-for-const.stderr | 3 +- .../ui/consts/control-flow/issue-46843.stderr | 5 ++- tests/ui/consts/issue-16538.stderr | 5 ++- tests/ui/consts/issue-32829-2.stderr | 12 ++----- tests/ui/consts/issue-43105.stderr | 5 ++- .../min_const_fn/bad_const_fn_body_ice.stderr | 3 +- tests/ui/consts/mir_check_nonconst.stderr | 5 ++- tests/ui/error-codes/E0010-teach.stderr | 5 ++- tests/ui/error-codes/E0010.stderr | 5 ++- tests/ui/error-codes/E0015.stderr | 5 ++- tests/ui/explicit-tail-calls/constck.stderr | 8 ++--- tests/ui/resolve/issue-39559-2.stderr | 6 ++-- ...lobal-variable-promotion-error-7364.stderr | 5 ++- .../ui/static/static-mut-not-constant.stderr | 5 ++- .../static-vec-repeat-not-constant.stderr | 5 ++- .../statics/check-values-constraints.stderr | 33 +++++++------------ .../const-check-fns-in-const-impl.stderr | 5 ++- .../const_derives/derive-const-gate.stderr | 4 ++- .../derive-const-non-const-type.stderr | 4 ++- .../const-traits/cross-crate.stock.stderr | 4 +-- .../const-traits/cross-crate.stocknc.stderr | 9 ++--- ...nline-incorrect-early-bound-in-ctfe.stderr | 3 +- .../ui/traits/const-traits/issue-79450.stderr | 3 +- .../ui/traits/const-traits/issue-88155.stderr | 3 +- .../const-traits/staged-api-user-crate.stderr | 3 +- .../const-traits/std-impl-gate.stock.stderr | 3 +- .../super-traits-fail-2.nn.stderr | 3 +- .../super-traits-fail-2.ny.stderr | 3 +- .../super-traits-fail-3.nnn.stderr | 4 +-- .../super-traits-fail-3.nny.stderr | 4 +-- .../super-traits-fail-3.nyn.stderr | 4 +-- .../super-traits-fail-3.nyy.stderr | 4 +-- .../super-traits-fail-3.ynn.stderr | 4 +-- .../super-traits-fail-3.yny.stderr | 4 +-- .../typeck_type_placeholder_item.stderr | 15 ++++----- 47 files changed, 104 insertions(+), 192 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index c958a91f754bb..ecb58059600d0 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -4,7 +4,6 @@ use hir::{ConstContext, LangItem}; use rustc_errors::codes::*; use rustc_errors::{Applicability, Diag, MultiSpan}; use rustc_hir as hir; -use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::traits::{ImplSource, Obligation, ObligationCause}; @@ -12,8 +11,8 @@ use rustc_middle::mir::CallSource; use rustc_middle::span_bug; use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths}; use rustc_middle::ty::{ - self, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param, TraitRef, Ty, - suggest_constraining_type_param, + self, AssocItemContainer, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param, + TraitRef, Ty, suggest_constraining_type_param, }; use rustc_session::parse::add_feature_diagnostics; use rustc_span::{BytePos, Pos, Span, Symbol, sym}; @@ -212,7 +211,6 @@ fn build_error_for_const_call<'tcx>( debug!(?call_kind); - let mut note = true; let mut err = match call_kind { CallKind::Normal { desugaring: Some((kind, self_ty)), .. } => { macro_rules! error { @@ -363,16 +361,9 @@ fn build_error_for_const_call<'tcx>( kind: ccx.const_kind(), non_or_conditionally, }); - let context_span = ccx.tcx.def_span(ccx.def_id()); - err.span_label(context_span, format!( - "calls in {}s are limited to constant functions, tuple structs and tuple variants", - ccx.const_kind(), - )); - note = false; - let def_kind = ccx.tcx.def_kind(callee); - if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = def_kind { - let parent = ccx.tcx.parent(callee); - if let DefKind::Trait = ccx.tcx.def_kind(parent) + if let Some(item) = ccx.tcx.opt_associated_item(callee) { + if let AssocItemContainer::Trait = item.container + && let parent = item.container_id(ccx.tcx) && !ccx.tcx.is_const_trait(parent) { let assoc_span = ccx.tcx.def_span(callee); @@ -407,7 +398,7 @@ fn build_error_for_const_call<'tcx>( trait_span.shrink_to_lo(), format!("consider making trait `{trait_name}` const"), format!("#[const_trait]\n{indentation}"), - Applicability::MachineApplicable, + Applicability::MaybeIncorrect, ); } else if !ccx.tcx.sess.is_nightly_build() { err.help("const traits are not yet supported on stable Rust"); @@ -424,12 +415,10 @@ fn build_error_for_const_call<'tcx>( } }; - if note { - err.note(format!( - "calls in {}s are limited to constant functions, tuple structs and tuple variants", - ccx.const_kind(), - )); - } + err.note(format!( + "calls in {}s are limited to constant functions, tuple structs and tuple variants", + ccx.const_kind(), + )); err } diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr index 47f6817b9b64d..646688604674c 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-disabled.stderr @@ -55,8 +55,6 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | -LL | const fn foo(x: &T) { - | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | @@ -68,6 +66,7 @@ LL | trait Foo { LL | fn a(&self); | ------------ this method is not const = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr index 49e2f98c371f3..d0e7edf42cdd5 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-nightly-enabled.stderr @@ -35,8 +35,6 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | -LL | const fn foo(x: &T) { - | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | @@ -47,6 +45,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr index 62fd9a38b7591..e09539684953c 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr @@ -53,8 +53,6 @@ note: `Bar` can't be used with `[const]` because it isn't `const` error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | -9 | const fn foo(x: &T) { - | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants 10 | x.a(); | ^^^ | @@ -66,6 +64,7 @@ note: method `a` is not const because trait `Foo` is not const 4 | fn a(&self); | ------------ this method is not const = help: const traits are not yet supported on stable Rust + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 6 previous errors diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr index fc1adbe441cef..9da10eb81d833 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr @@ -43,8 +43,6 @@ note: `Bar` can't be used with `[const]` because it isn't `const` error[E0015]: cannot call non-const method `::a` in constant functions --> const-super-trait.rs:10:7 | -9 | const fn foo(x: &T) { - | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants 10 | x.a(); | ^^^ | @@ -56,6 +54,7 @@ note: method `a` is not const because trait `Foo` is not const 4 | fn a(&self); | ------------ this method is not const = help: const traits are not yet supported on stable Rust + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 5 previous errors diff --git a/tests/ui/asm/non-const.stderr b/tests/ui/asm/non-const.stderr index da881a12f120f..d7a901ba20ee5 100644 --- a/tests/ui/asm/non-const.stderr +++ b/tests/ui/asm/non-const.stderr @@ -2,13 +2,14 @@ error[E0015]: cannot call non-const function `non_const_fn` in constants --> $DIR/non-const.rs:10:31 | LL | global_asm!("/* {} */", const non_const_fn(0)); - | ^^^^^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^^^ | note: function `non_const_fn` is not const --> $DIR/non-const.rs:8:1 | LL | fn non_const_fn(x: i32) -> i32 { x } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/borrowck/issue-64453.stderr b/tests/ui/borrowck/issue-64453.stderr index 47d9082765555..29a7363705cd7 100644 --- a/tests/ui/borrowck/issue-64453.stderr +++ b/tests/ui/borrowck/issue-64453.stderr @@ -2,12 +2,11 @@ error[E0015]: cannot call non-const function `format` in statics --> $DIR/issue-64453.rs:4:31 | LL | static settings_dir: String = format!(""); - | --------------------------- ^^^^^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^ | note: function `format` is not const --> $SRC_DIR/alloc/src/fmt.rs:LL:COL + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/const-generics/nested-type.full.stderr b/tests/ui/const-generics/nested-type.full.stderr index 587de095452d1..e5a1f230380d3 100644 --- a/tests/ui/const-generics/nested-type.full.stderr +++ b/tests/ui/const-generics/nested-type.full.stderr @@ -1,17 +1,10 @@ error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants --> $DIR/nested-type.rs:15:5 | -LL | struct Foo; -LL | | -LL | | impl Foo { -... | -LL | | Foo::<17>::value() - | | ^^^^^^^^^^^^^^^^^^ -LL | | -LL | | }]>; - | |_- calls in constants are limited to constant functions, tuple structs and tuple variants +LL | Foo::<17>::value() + | ^^^^^^^^^^^^^^^^^^ + | + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/const-generics/nested-type.min.stderr b/tests/ui/const-generics/nested-type.min.stderr index e7d21f8a98949..8282acd4ea7b1 100644 --- a/tests/ui/const-generics/nested-type.min.stderr +++ b/tests/ui/const-generics/nested-type.min.stderr @@ -1,17 +1,10 @@ error[E0015]: cannot call non-const associated function `Foo::{constant#0}::Foo::<17>::value` in constants --> $DIR/nested-type.rs:15:5 | -LL | struct Foo; -LL | | -LL | | impl Foo { -... | -LL | | Foo::<17>::value() - | | ^^^^^^^^^^^^^^^^^^ -LL | | -LL | | }]>; - | |_- calls in constants are limited to constant functions, tuple structs and tuple variants +LL | Foo::<17>::value() + | ^^^^^^^^^^^^^^^^^^ + | + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: `[u8; { struct Foo; diff --git a/tests/ui/consts/const-call.stderr b/tests/ui/consts/const-call.stderr index db375f80b6c67..cc80f73d0bb61 100644 --- a/tests/ui/consts/const-call.stderr +++ b/tests/ui/consts/const-call.stderr @@ -2,13 +2,14 @@ error[E0015]: cannot call non-const function `f` in constants --> $DIR/const-call.rs:6:17 | LL | let _ = [0; f(2)]; - | ^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^ | note: function `f` is not const --> $DIR/const-call.rs:1:1 | LL | fn f(x: usize) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/const-eval/format.stderr b/tests/ui/consts/const-eval/format.stderr index 97b4436a058ee..ea191eb5c0989 100644 --- a/tests/ui/consts/const-eval/format.stderr +++ b/tests/ui/consts/const-eval/format.stderr @@ -18,13 +18,12 @@ LL | println!("{:?}", 0); error[E0015]: cannot call non-const function `_print` in constant functions --> $DIR/format.rs:7:5 | -LL | const fn print() { - | ---------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | println!("{:?}", 0); | ^^^^^^^^^^^^^^^^^^^ | note: function `_print` is not const --> $SRC_DIR/std/src/io/stdio.rs:LL:COL + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0015]: cannot call non-const formatting macro in constant functions diff --git a/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr b/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr index 8f07e038e1994..ac0d344d8f480 100644 --- a/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr +++ b/tests/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.stderr @@ -1,9 +1,6 @@ error[E0015]: cannot call non-const function `regular_in_block` in constant functions --> $DIR/const-extern-fn-call-extern-fn.rs:7:9 | -LL | const extern "C" fn bar() { - | ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -LL | unsafe { LL | regular_in_block(); | ^^^^^^^^^^^^^^^^^^ | @@ -12,13 +9,11 @@ note: function `regular_in_block` is not const | LL | fn regular_in_block(); | ^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `regular` in constant functions --> $DIR/const-extern-fn-call-extern-fn.rs:16:9 | -LL | const extern "C" fn foo() { - | ------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -LL | unsafe { LL | regular(); | ^^^^^^^^^ | @@ -27,6 +22,7 @@ note: function `regular` is not const | LL | extern "C" fn regular() {} | ^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/consts/const-fn-not-safe-for-const.stderr b/tests/ui/consts/const-fn-not-safe-for-const.stderr index 068c06601c8b6..8fd27df5367e0 100644 --- a/tests/ui/consts/const-fn-not-safe-for-const.stderr +++ b/tests/ui/consts/const-fn-not-safe-for-const.stderr @@ -1,8 +1,6 @@ error[E0015]: cannot call non-const function `random` in constant functions --> $DIR/const-fn-not-safe-for-const.rs:14:5 | -LL | const fn sub1() -> u32 { - | ---------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | random() | ^^^^^^^^ | @@ -11,6 +9,7 @@ note: function `random` is not const | LL | fn random() -> u32 { | ^^^^^^^^^^^^^^^^^^ + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/control-flow/issue-46843.stderr b/tests/ui/consts/control-flow/issue-46843.stderr index 310c860f59b45..46b2a6bf1fd36 100644 --- a/tests/ui/consts/control-flow/issue-46843.stderr +++ b/tests/ui/consts/control-flow/issue-46843.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `non_const` in constants --> $DIR/issue-46843.rs:10:26 | LL | pub const Q: i32 = match non_const() { - | ---------------- ^^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^ | note: function `non_const` is not const --> $DIR/issue-46843.rs:6:1 | LL | fn non_const() -> Thing { | ^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/issue-16538.stderr b/tests/ui/consts/issue-16538.stderr index e78d95dcd186f..fd97b4360a468 100644 --- a/tests/ui/consts/issue-16538.stderr +++ b/tests/ui/consts/issue-16538.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `Y::foo` in statics --> $DIR/issue-16538.rs:11:23 | LL | static foo: &Y::X = &*Y::foo(Y::x as *const Y::X); - | ----------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: function `foo` is not const --> $DIR/issue-16538.rs:6:5 | LL | pub fn foo(value: *const X) -> *const X { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error[E0133]: dereference of raw pointer is unsafe and requires unsafe function or block diff --git a/tests/ui/consts/issue-32829-2.stderr b/tests/ui/consts/issue-32829-2.stderr index 52398af41eeef..b201454866ed5 100644 --- a/tests/ui/consts/issue-32829-2.stderr +++ b/tests/ui/consts/issue-32829-2.stderr @@ -1,9 +1,6 @@ error[E0015]: cannot call non-const function `invalid` in constants --> $DIR/issue-32829-2.rs:10:9 | -LL | const bad_two : u32 = { - | ------------------- calls in constants are limited to constant functions, tuple structs and tuple variants -LL | { LL | invalid(); | ^^^^^^^^^ | @@ -12,13 +9,11 @@ note: function `invalid` is not const | LL | fn invalid() {} | ^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `invalid` in statics --> $DIR/issue-32829-2.rs:32:9 | -LL | static bad_five : u32 = { - | --------------------- calls in statics are limited to constant functions, tuple structs and tuple variants -LL | { LL | invalid(); | ^^^^^^^^^ | @@ -27,14 +22,12 @@ note: function `invalid` is not const | LL | fn invalid() {} | ^^^^^^^^^^^^ + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error[E0015]: cannot call non-const function `invalid` in statics --> $DIR/issue-32829-2.rs:54:9 | -LL | static mut bad_eight : u32 = { - | -------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants -LL | { LL | invalid(); | ^^^^^^^^^ | @@ -43,6 +36,7 @@ note: function `invalid` is not const | LL | fn invalid() {} | ^^^^^^^^^^^^ + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 3 previous errors diff --git a/tests/ui/consts/issue-43105.stderr b/tests/ui/consts/issue-43105.stderr index 442fbdd107fab..7ec0a2b73afc5 100644 --- a/tests/ui/consts/issue-43105.stderr +++ b/tests/ui/consts/issue-43105.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `xyz` in constants --> $DIR/issue-43105.rs:3:17 | LL | const NUM: u8 = xyz(); - | ------------- ^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^ | note: function `xyz` is not const --> $DIR/issue-43105.rs:1:1 | LL | fn xyz() -> u8 { 42 } | ^^^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr index c65abc530bc2b..8e52a7aa35e1e 100644 --- a/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr +++ b/tests/ui/consts/min_const_fn/bad_const_fn_body_ice.stderr @@ -9,11 +9,10 @@ LL | vec![1, 2, 3] error[E0015]: cannot call non-const method `slice::::into_vec::` in constant functions --> $DIR/bad_const_fn_body_ice.rs:2:5 | -LL | const fn foo(a: i32) -> Vec { - | -------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | vec![1, 2, 3] | ^^^^^^^^^^^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors diff --git a/tests/ui/consts/mir_check_nonconst.stderr b/tests/ui/consts/mir_check_nonconst.stderr index ad2056a737078..bb0940ee789b9 100644 --- a/tests/ui/consts/mir_check_nonconst.stderr +++ b/tests/ui/consts/mir_check_nonconst.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `bar` in statics --> $DIR/mir_check_nonconst.rs:8:19 | LL | static foo: Foo = bar(); - | --------------- ^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^ | note: function `bar` is not const --> $DIR/mir_check_nonconst.rs:4:1 | LL | fn bar() -> Foo { | ^^^^^^^^^^^^^^^ + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 1 previous error diff --git a/tests/ui/error-codes/E0010-teach.stderr b/tests/ui/error-codes/E0010-teach.stderr index 237efbefcf6ef..82bbe01aef792 100644 --- a/tests/ui/error-codes/E0010-teach.stderr +++ b/tests/ui/error-codes/E0010-teach.stderr @@ -11,10 +11,9 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/E0010-teach.rs:5:23 | LL | const CON: Vec = vec![1, 2, 3]; - | ------------------- ^^^^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^ | + = note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors diff --git a/tests/ui/error-codes/E0010.stderr b/tests/ui/error-codes/E0010.stderr index 2bb87118d242d..87b722b5f6566 100644 --- a/tests/ui/error-codes/E0010.stderr +++ b/tests/ui/error-codes/E0010.stderr @@ -10,10 +10,9 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/E0010.rs:3:23 | LL | const CON: Vec = vec![1, 2, 3]; - | ------------------- ^^^^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^ | + = note: calls in constants are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 2 previous errors diff --git a/tests/ui/error-codes/E0015.stderr b/tests/ui/error-codes/E0015.stderr index 4331d7c34b3ca..bc457aefaecb0 100644 --- a/tests/ui/error-codes/E0015.stderr +++ b/tests/ui/error-codes/E0015.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `create_some` in constants --> $DIR/E0015.rs:5:25 | LL | const FOO: Option = create_some(); - | --------------------- ^^^^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^ | note: function `create_some` is not const --> $DIR/E0015.rs:1:1 | LL | fn create_some() -> Option { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/explicit-tail-calls/constck.stderr b/tests/ui/explicit-tail-calls/constck.stderr index 84d7bc3a1bf13..2fc2fb7f9338b 100644 --- a/tests/ui/explicit-tail-calls/constck.stderr +++ b/tests/ui/explicit-tail-calls/constck.stderr @@ -1,9 +1,6 @@ error[E0015]: cannot call non-const function `not_const` in constant functions --> $DIR/constck.rs:6:16 | -LL | const fn f() { - | ------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants -LL | if false { LL | become not_const(); | ^^^^^^^^^^^ | @@ -12,13 +9,11 @@ note: function `not_const` is not const | LL | fn not_const() {} | ^^^^^^^^^^^^^^ + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const function `not_const` in constant functions --> $DIR/constck.rs:13:26 | -LL | const fn g((): ()) { - | ------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants -LL | if false { LL | become yes_const(not_const()); | ^^^^^^^^^^^ | @@ -27,6 +22,7 @@ note: function `not_const` is not const | LL | fn not_const() {} | ^^^^^^^^^^^^^^ + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/resolve/issue-39559-2.stderr b/tests/ui/resolve/issue-39559-2.stderr index fbbcaa663ae0d..4bfa1d1b1322f 100644 --- a/tests/ui/resolve/issue-39559-2.stderr +++ b/tests/ui/resolve/issue-39559-2.stderr @@ -2,7 +2,7 @@ error[E0015]: cannot call non-const associated function `::dim` in --> $DIR/issue-39559-2.rs:14:24 | LL | let array: [usize; Dim3::dim()] - | ^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^ | note: associated function `dim` is not const because trait `Dim` is not const --> $DIR/issue-39559-2.rs:1:1 @@ -12,6 +12,7 @@ LL | trait Dim { LL | fn dim() -> usize; | ------------------ this associated function is not const = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` + = note: calls in constants are limited to constant functions, tuple structs and tuple variants help: consider making trait `Dim` const | LL + #[const_trait] @@ -22,7 +23,7 @@ error[E0015]: cannot call non-const associated function `::dim` in --> $DIR/issue-39559-2.rs:16:15 | LL | = [0; Dim3::dim()]; - | ^^^^^^^^^^^ calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^ | note: associated function `dim` is not const because trait `Dim` is not const --> $DIR/issue-39559-2.rs:1:1 @@ -32,6 +33,7 @@ LL | trait Dim { LL | fn dim() -> usize; | ------------------ this associated function is not const = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` + = note: calls in constants are limited to constant functions, tuple structs and tuple variants help: consider making trait `Dim` const | LL + #[const_trait] diff --git a/tests/ui/static/global-variable-promotion-error-7364.stderr b/tests/ui/static/global-variable-promotion-error-7364.stderr index bced8c1520202..b9d75676bef89 100644 --- a/tests/ui/static/global-variable-promotion-error-7364.stderr +++ b/tests/ui/static/global-variable-promotion-error-7364.stderr @@ -15,10 +15,9 @@ error[E0015]: cannot call non-const associated function `Box::>:: --> $DIR/global-variable-promotion-error-7364.rs:5:37 | LL | static boxed: Box> = Box::new(RefCell::new(0)); - | --------------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^^^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 2 previous errors diff --git a/tests/ui/static/static-mut-not-constant.stderr b/tests/ui/static/static-mut-not-constant.stderr index 7e64d36b26d17..f28ea0b1689ab 100644 --- a/tests/ui/static/static-mut-not-constant.stderr +++ b/tests/ui/static/static-mut-not-constant.stderr @@ -2,10 +2,9 @@ error[E0015]: cannot call non-const associated function `Box::::new` in s --> $DIR/static-mut-not-constant.rs:1:28 | LL | static mut a: Box = Box::new(3); - | ------------------------ ^^^^^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 1 previous error diff --git a/tests/ui/static/static-vec-repeat-not-constant.stderr b/tests/ui/static/static-vec-repeat-not-constant.stderr index 24f0986ccc494..fa4033f6a4076 100644 --- a/tests/ui/static/static-vec-repeat-not-constant.stderr +++ b/tests/ui/static/static-vec-repeat-not-constant.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `foo` in statics --> $DIR/static-vec-repeat-not-constant.rs:3:25 | LL | static a: [isize; 2] = [foo(); 2]; - | -------------------- ^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^ | note: function `foo` is not const --> $DIR/static-vec-repeat-not-constant.rs:1:1 | LL | fn foo() -> isize { 23 } | ^^^^^^^^^^^^^^^^^ + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error: aborting due to 1 previous error diff --git a/tests/ui/statics/check-values-constraints.stderr b/tests/ui/statics/check-values-constraints.stderr index dfbb1f6e985c3..c54f4830533ae 100644 --- a/tests/ui/statics/check-values-constraints.stderr +++ b/tests/ui/statics/check-values-constraints.stderr @@ -23,19 +23,15 @@ error[E0015]: cannot call non-const method `slice::::into_vec::< --> $DIR/check-values-constraints.rs:81:33 | LL | static STATIC11: Vec = vec![MyOwned]; - | ----------------------------- ^^^^^^^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0015]: cannot call non-const method `::to_string` in statics --> $DIR/check-values-constraints.rs:92:38 | -LL | static mut STATIC14: SafeStruct = SafeStruct { - | ------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants -LL | field1: SafeEnum::Variant1, LL | field2: SafeEnum::Variant4("str".to_string()), | ^^^^^^^^^^^ | @@ -46,6 +42,7 @@ note: method `to_string` is not const because trait `ToString` is not const ::: $SRC_DIR/alloc/src/string.rs:LL:COL | = note: this method is not const + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` error[E0010]: allocations are not allowed in statics @@ -59,11 +56,10 @@ LL | vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:96:5 | -LL | static STATIC15: &'static [Vec] = &[ - | ---------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants LL | vec![MyOwned], | ^^^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -78,12 +74,10 @@ LL | vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:98:5 | -LL | static STATIC15: &'static [Vec] = &[ - | ---------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants -... LL | vec![MyOwned], | ^^^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -98,11 +92,10 @@ LL | &vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:103:6 | -LL | static STATIC16: (&'static Vec, &'static Vec) = ( - | --------------------------------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants LL | &vec![MyOwned], | ^^^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -117,12 +110,10 @@ LL | &vec![MyOwned], error[E0015]: cannot call non-const method `slice::::into_vec::` in statics --> $DIR/check-values-constraints.rs:105:6 | -LL | static STATIC16: (&'static Vec, &'static Vec) = ( - | --------------------------------------------------------------- calls in statics are limited to constant functions, tuple structs and tuple variants -... LL | &vec![MyOwned], | ^^^^^^^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -138,10 +129,9 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/check-values-constraints.rs:111:31 | LL | static STATIC19: Vec = vec![3]; - | --------------------------- ^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -157,10 +147,9 @@ error[E0015]: cannot call non-const method `slice::::into_vec:: $DIR/check-values-constraints.rs:117:32 | LL | static x: Vec = vec![3]; - | -------------------- ^^^^^^^ - | | - | calls in statics are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^ | + = note: calls in statics are limited to constant functions, tuple structs and tuple variants = note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)` = note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr b/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr index 70704780ddce7..1a7ec35f3ddba 100644 --- a/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr +++ b/tests/ui/traits/const-traits/const-check-fns-in-const-impl.stderr @@ -2,15 +2,14 @@ error[E0015]: cannot call non-const function `non_const` in constant functions --> $DIR/const-check-fns-in-const-impl.rs:14:16 | LL | fn foo() { non_const() } - | -------- ^^^^^^^^^^^ - | | - | calls in constant functions are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^ | note: function `non_const` is not const --> $DIR/const-check-fns-in-const-impl.rs:11:1 | LL | fn non_const() {} | ^^^^^^^^^^^^^^ + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr b/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr index ac393825ec720..cbc62d602a473 100644 --- a/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr +++ b/tests/ui/traits/const-traits/const_derives/derive-const-gate.stderr @@ -21,7 +21,9 @@ error[E0015]: cannot call non-const method `Formatter::<'_>::write_str` in const --> $DIR/derive-const-gate.rs:1:16 | LL | #[derive_const(Debug)] - | ^^^^^ calls in constant functions are limited to constant functions, tuple structs and tuple variants + | ^^^^^ + | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 3 previous errors diff --git a/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr b/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr index 9f620fcf81b7c..93638801895da 100644 --- a/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr +++ b/tests/ui/traits/const-traits/const_derives/derive-const-non-const-type.stderr @@ -11,7 +11,9 @@ error[E0015]: cannot call non-const method `Formatter::<'_>::debug_tuple_field1_ --> $DIR/derive-const-non-const-type.rs:12:16 | LL | #[derive_const(Debug)] - | ^^^^^ calls in constant functions are limited to constant functions, tuple structs and tuple variants + | ^^^^^ + | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error: aborting due to 2 previous errors diff --git a/tests/ui/traits/const-traits/cross-crate.stock.stderr b/tests/ui/traits/const-traits/cross-crate.stock.stderr index 89278ff56ad0c..44a60c99ae9ea 100644 --- a/tests/ui/traits/const-traits/cross-crate.stock.stderr +++ b/tests/ui/traits/const-traits/cross-crate.stock.stderr @@ -1,12 +1,10 @@ error[E0658]: cannot call conditionally-const method `::func` in constant functions --> $DIR/cross-crate.rs:22:11 | -LL | const fn const_context() { - | ------------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | Const.func(); | ^^^^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/cross-crate.stocknc.stderr b/tests/ui/traits/const-traits/cross-crate.stocknc.stderr index 6269e74fd1d2b..766c20aa8211f 100644 --- a/tests/ui/traits/const-traits/cross-crate.stocknc.stderr +++ b/tests/ui/traits/const-traits/cross-crate.stocknc.stderr @@ -1,21 +1,18 @@ error[E0015]: cannot call non-const method `::func` in constant functions --> $DIR/cross-crate.rs:19:14 | -LL | const fn const_context() { - | ------------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants -LL | #[cfg(any(stocknc, gatednc))] LL | NonConst.func(); | ^^^^^^ + | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants error[E0658]: cannot call conditionally-const method `::func` in constant functions --> $DIR/cross-crate.rs:22:11 | -LL | const fn const_context() { - | ------------------------ calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | Const.func(); | ^^^^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr b/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr index 092a5ac05b823..ca73ae845550d 100644 --- a/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr +++ b/tests/ui/traits/const-traits/inline-incorrect-early-bound-in-ctfe.stderr @@ -10,8 +10,6 @@ LL | fn foo(self) { error[E0015]: cannot call non-const method `<() as Trait>::foo` in constant functions --> $DIR/inline-incorrect-early-bound-in-ctfe.rs:26:8 | -LL | const fn foo() { - | -------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | ().foo(); | ^^^^^ | @@ -22,6 +20,7 @@ LL | trait Trait { | ^^^^^^^^^^^ this trait is not const LL | fn foo(self); | ------------- this method is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Trait` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/issue-79450.stderr b/tests/ui/traits/const-traits/issue-79450.stderr index dd78b464e7825..d8a9e18980687 100644 --- a/tests/ui/traits/const-traits/issue-79450.stderr +++ b/tests/ui/traits/const-traits/issue-79450.stderr @@ -1,13 +1,12 @@ error[E0015]: cannot call non-const function `_print` in constant functions --> $DIR/issue-79450.rs:9:9 | -LL | fn prov(&self) { - | -------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | println!("lul"); | ^^^^^^^^^^^^^^^ | note: function `_print` is not const --> $SRC_DIR/std/src/io/stdio.rs:LL:COL + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/issue-88155.stderr b/tests/ui/traits/const-traits/issue-88155.stderr index 2df4a0004b6a6..4a912cc8274a7 100644 --- a/tests/ui/traits/const-traits/issue-88155.stderr +++ b/tests/ui/traits/const-traits/issue-88155.stderr @@ -1,8 +1,6 @@ error[E0015]: cannot call non-const associated function `::assoc` in constant functions --> $DIR/issue-88155.rs:8:5 | -LL | pub const fn foo() -> bool { - | -------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | T::assoc() | ^^^^^^^^^^ | @@ -13,6 +11,7 @@ LL | pub trait A { | ^^^^^^^^^^^ this trait is not const LL | fn assoc() -> bool; | ------------------- this associated function is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `A` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/staged-api-user-crate.stderr b/tests/ui/traits/const-traits/staged-api-user-crate.stderr index 1baa8b308284e..81611da9e748c 100644 --- a/tests/ui/traits/const-traits/staged-api-user-crate.stderr +++ b/tests/ui/traits/const-traits/staged-api-user-crate.stderr @@ -1,11 +1,10 @@ error[E0658]: cannot call conditionally-const associated function `::func` in constant functions --> $DIR/staged-api-user-crate.rs:12:5 | -LL | const fn stable_const_context() { - | ------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | Unstable::func(); | ^^^^^^^^^^^^^^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/std-impl-gate.stock.stderr b/tests/ui/traits/const-traits/std-impl-gate.stock.stderr index 3f40f602eefe2..261f68bebdb25 100644 --- a/tests/ui/traits/const-traits/std-impl-gate.stock.stderr +++ b/tests/ui/traits/const-traits/std-impl-gate.stock.stderr @@ -1,11 +1,10 @@ error[E0658]: cannot call conditionally-const associated function ` as Default>::default` in constant functions --> $DIR/std-impl-gate.rs:13:5 | -LL | const fn const_context() -> Vec { - | -------------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | Default::default() | ^^^^^^^^^^^^^^^^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr b/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr index 84fc743e4a2b7..c9dc239bef334 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-2.nn.stderr @@ -48,8 +48,6 @@ LL | #[const_trait] trait Foo { error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-2.rs:20:7 | -LL | const fn foo(x: &T) { - | --------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | @@ -60,6 +58,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr b/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr index 9d72e149aab28..bfbf6980ab833 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-2.ny.stderr @@ -60,8 +60,6 @@ LL | #[const_trait] trait Foo { error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-2.rs:20:7 | -LL | const fn foo(x: &T) { - | --------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants LL | x.a(); | ^^^ | @@ -72,6 +70,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr index 2625f94460616..ea487cbd563f2 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nnn.stderr @@ -91,9 +91,6 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | -LL | const fn foo(x: &T) { - | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | x.a(); | ^^^ | @@ -105,6 +102,7 @@ LL | trait Foo { LL | fn a(&self); | ------------ this method is not const = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr index 2625f94460616..ea487cbd563f2 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nny.stderr @@ -91,9 +91,6 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | -LL | const fn foo(x: &T) { - | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | x.a(); | ^^^ | @@ -105,6 +102,7 @@ LL | trait Foo { LL | fn a(&self); | ------------ this method is not const = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable `#[const_trait]` + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr index 155263656e368..b00ad706a5fa1 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nyn.stderr @@ -41,12 +41,10 @@ LL | #[cfg_attr(any(yyy, yny, nyy, nyn), const_trait)] error[E0658]: cannot call conditionally-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | -LL | const fn foo(x: &T) { - | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | x.a(); | ^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr index 155263656e368..b00ad706a5fa1 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.nyy.stderr @@ -41,12 +41,10 @@ LL | #[cfg_attr(any(yyy, yny, nyy, nyn), const_trait)] error[E0658]: cannot call conditionally-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | -LL | const fn foo(x: &T) { - | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | x.a(); | ^^^ | + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: see issue #143874 for more information = help: add `#![feature(const_trait_impl)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr index 4895b5b0cd08b..5951caebe7335 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.ynn.stderr @@ -71,9 +71,6 @@ LL | #[const_trait] trait Bar: [const] Foo {} error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | -LL | const fn foo(x: &T) { - | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | x.a(); | ^^^ | @@ -84,6 +81,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr b/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr index 29bce84326cab..563495204ad7b 100644 --- a/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail-3.yny.stderr @@ -60,9 +60,6 @@ LL | #[const_trait] trait Foo { error[E0015]: cannot call non-const method `::a` in constant functions --> $DIR/super-traits-fail-3.rs:36:7 | -LL | const fn foo(x: &T) { - | ----------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants -... LL | x.a(); | ^^^ | @@ -73,6 +70,7 @@ LL | trait Foo { | ^^^^^^^^^ this trait is not const LL | fn a(&self); | ------------ this method is not const + = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants help: consider making trait `Foo` const | LL + #[const_trait] diff --git a/tests/ui/typeck/typeck_type_placeholder_item.stderr b/tests/ui/typeck/typeck_type_placeholder_item.stderr index 0b0f9fbb9be11..0b70ac97fd435 100644 --- a/tests/ui/typeck/typeck_type_placeholder_item.stderr +++ b/tests/ui/typeck/typeck_type_placeholder_item.stderr @@ -669,23 +669,20 @@ error[E0015]: cannot call non-const function `map::` in constants --> $DIR/typeck_type_placeholder_item.rs:231:22 | LL | const _: Option<_> = map(value); - | ------------------ ^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^ | note: function `map` is not const --> $DIR/typeck_type_placeholder_item.rs:222:1 | LL | fn map(_: fn() -> Option<&'static T>) -> Option { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method ` as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}>` in constants --> $DIR/typeck_type_placeholder_item.rs:240:22 | LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); - | ---------- ^^^^^^^^^^^^^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^^^^^^^^^^ | note: method `filter` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL @@ -694,14 +691,13 @@ note: method `filter` is not const because trait `Iterator` is not const ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this method is not const + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error[E0015]: cannot call non-const method `, {closure@$DIR/typeck_type_placeholder_item.rs:240:29: 240:32}> as Iterator>::map::` in constants --> $DIR/typeck_type_placeholder_item.rs:240:45 | LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x); - | ---------- ^^^^^^^^^^^^^^ - | | - | calls in constants are limited to constant functions, tuple structs and tuple variants + | ^^^^^^^^^^^^^^ | note: method `map` is not const because trait `Iterator` is not const --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL @@ -710,6 +706,7 @@ note: method `map` is not const because trait `Iterator` is not const ::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL | = note: this method is not const + = note: calls in constants are limited to constant functions, tuple structs and tuple variants error: aborting due to 83 previous errors From 74b7592ce936a8e4cf57d3e73cded0a9f7a93f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 31 Oct 2025 21:23:59 +0000 Subject: [PATCH 20/20] fix rebase --- compiler/rustc_const_eval/src/check_consts/ops.rs | 6 +++--- .../const-super-trait-stable-disabled.stderr | 4 ++-- .../const-super-trait-stable-enabled.stderr | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_const_eval/src/check_consts/ops.rs b/compiler/rustc_const_eval/src/check_consts/ops.rs index ecb58059600d0..d81b31b704b3d 100644 --- a/compiler/rustc_const_eval/src/check_consts/ops.rs +++ b/compiler/rustc_const_eval/src/check_consts/ops.rs @@ -11,8 +11,8 @@ use rustc_middle::mir::CallSource; use rustc_middle::span_bug; use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths}; use rustc_middle::ty::{ - self, AssocItemContainer, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param, - TraitRef, Ty, suggest_constraining_type_param, + self, AssocContainer, Closure, FnDef, FnPtr, GenericArgKind, GenericArgsRef, Param, TraitRef, + Ty, suggest_constraining_type_param, }; use rustc_session::parse::add_feature_diagnostics; use rustc_span::{BytePos, Pos, Span, Symbol, sym}; @@ -362,7 +362,7 @@ fn build_error_for_const_call<'tcx>( non_or_conditionally, }); if let Some(item) = ccx.tcx.opt_associated_item(callee) { - if let AssocItemContainer::Trait = item.container + if let AssocContainer::Trait = item.container && let parent = item.container_id(ccx.tcx) && !ccx.tcx.is_const_trait(parent) { diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr index e09539684953c..e8b5c09699202 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-disabled.stderr @@ -59,9 +59,9 @@ error[E0015]: cannot call non-const method `::a` in constant functions note: method `a` is not const because trait `Foo` is not const --> const-super-trait.rs:3:1 | -3 | trait Foo { + 3 | trait Foo { | ^^^^^^^^^ this trait is not const -4 | fn a(&self); + 4 | fn a(&self); | ------------ this method is not const = help: const traits are not yet supported on stable Rust = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants diff --git a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr index 9da10eb81d833..263532f3a9400 100644 --- a/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr +++ b/tests/run-make/const-trait-stable-toolchain/const-super-trait-stable-enabled.stderr @@ -49,9 +49,9 @@ error[E0015]: cannot call non-const method `::a` in constant functions note: method `a` is not const because trait `Foo` is not const --> const-super-trait.rs:3:1 | -3 | trait Foo { + 3 | trait Foo { | ^^^^^^^^^ this trait is not const -4 | fn a(&self); + 4 | fn a(&self); | ------------ this method is not const = help: const traits are not yet supported on stable Rust = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants