Skip to content

Conversation

@estebank
Copy link
Contributor

@estebank estebank commented Jul 19, 2025

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<T: PointeeSized> 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<T: PointeeSized> PartialEq for *mut T {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2025

r? @oli-obk

rustbot has assigned @oli-obk.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 19, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 19, 2025

Some changes occurred to constck

cc @fee1-dead

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

This PR modifies run-make tests.

cc @jieyouxu

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it pointing at the raw ptr impls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that they are incorrectly detected as CandidateSimilarity::Exact, which is incorrect. I can skip them explicitly on this PR by adding some other check, or look into it at another time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because of this:

let strip_references = |mut t: Ty<'tcx>| -> Ty<'tcx> {
loop {
match t.kind() {
ty::Ref(_, inner, _) | ty::RawPtr(inner, _) => t = *inner,
_ => break t,
}
}
};
if !ignoring_lifetimes {
a = strip_references(a);
b = strip_references(b);
}

trait_span.shrink_to_lo(),
format!("consider making trait `{trait_name}` const"),
format!("#[const_trait]\n{indentation}"),
Applicability::MachineApplicable,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not MachineApplicable, since it doesn't fix the bug completely.

));
note = false;
let def_kind = ccx.tcx.def_kind(callee);
if let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = def_kind {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callees are always AssocFns, why did you add DefKind::AssocTy | DefKind::AssocConst too?

Comment on lines 376 to 378
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole check could be simplified if you looked at opt_associated_item and checked both the AssocKind and the AssocItemContainer, rather than using manual parenting logic.

non_or_conditionally,
});
let context_span = ccx.tcx.def_span(ccx.def_id());
err.span_label(context_span, format!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this to a label only for this case, and keep it as a note for the rest? Having special logic changing it between a note and a label seems too special cased. I'd prefer if you reverted it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is pretty long for a label too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to point at the constant context in the output in some way, as it isn't always trivially obvious why a constant function was expected. As we already had a note talking about the const context I moved that same message over from the note to the label so we wouldn't need both.

It was

error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
  --> const-super-trait.rs:10:7
   |
LL | const fn foo<T: ~const Bar>(x: &T) {
   | ---------------------------------- calls in constant functions are limited to constant functions, tuple structs and tuple variants
LL |     x.a();
   |       ^^^

vs

error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
  --> const-super-trait.rs:10:7
   |
LL |     x.a();
   |       ^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

We could instead have

error[E0015]: cannot call non-const method `<T as Foo>::a` in constant functions
  --> const-super-trait.rs:10:7
   |
LL | const fn foo<T: ~const Bar>(x: &T) {
   | ---------------------------------- required by this constant function
LL |     x.a();
   |       ^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

I was just trying to keep the verbosity increase to a minimum.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to point at the constant context in the output in some way, as it isn't always trivially obvious why a constant function was expected.

I don't think the users need this to be a label. There's a tradeoff here with verbosity and I think this both complicates the implementation by having that note boolean and also splits the presentation of the diagnostic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed label in the last commit.

@bors
Copy link
Collaborator

bors commented Sep 12, 2025

☔ The latest upstream changes (presumably #144847) made this pull request unmergeable. Please resolve the merge conflicts.

@wesleywiser
Copy link
Member

Hi @oli-obk, it looks like Esteban has resolved all of the existing review feedback so this is ready for another round of review whenever you get a chance. Thanks!

@wesleywiser
Copy link
Member

r? rust-lang/compiler

@rustbot rustbot assigned davidtwco and unassigned oli-obk Oct 23, 2025
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit then r=me

View changes since this review

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<T: PointeeSized> 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<T: PointeeSized> PartialEq for *mut T {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
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 {
   |
```
```
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<const N: [u8; {
   |  __________________________-
LL | |     struct Foo<const N: usize>;
LL | |
LL | |     impl<const N: usize> Foo<N> {
...  |
LL | |     Foo::<17>::value()
   | |     ^^^^^^^^^^^^^^^^^^
LL | |
LL | | }]>;
   | |_- calls in constants are limited to constant functions, tuple structs and tuple variants
```
@rustbot rustbot added the F-explicit_tail_calls `#![feature(explicit_tail_calls)]` label Oct 31, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 31, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-gcc failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
---- [run-make] tests/run-make/const-trait-stable-toolchain stdout ----

error: rmake recipe failed to complete
status: exit status: 101
command: cd "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-make/const-trait-stable-toolchain/rmake_out" && env -u RUSTFLAGS -u __RUSTC_DEBUG_ASSERTIONS_ENABLED -u __STD_DEBUG_ASSERTIONS_ENABLED AR="ar" BUILD_ROOT="/checkout/obj/build/x86_64-unknown-linux-gnu" CC="cc" CC_DEFAULT_FLAGS="-ffunction-sections -fdata-sections -fPIC -m64" CXX="c++" CXX_DEFAULT_FLAGS="-ffunction-sections -fdata-sections -fPIC -m64" HOST_RUSTC_DYLIB_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib" LD_LIBRARY_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/bootstrap-tools/x86_64-unknown-linux-gnu/release/deps:/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/lib/rustlib/x86_64-unknown-linux-gnu/lib" LD_LIB_PATH_ENVVAR="LD_LIBRARY_PATH" LLVM_BIN_DIR="/checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm/bin" LLVM_COMPONENTS="aarch64 aarch64asmparser aarch64codegen aarch64desc aarch64disassembler aarch64info aarch64utils aggressiveinstcombine all all-targets amdgpu amdgpuasmparser amdgpucodegen amdgpudesc amdgpudisassembler amdgpuinfo amdgputargetmca amdgpuutils analysis arm armasmparser armcodegen armdesc armdisassembler arminfo armutils asmparser asmprinter avr avrasmparser avrcodegen avrdesc avrdisassembler avrinfo binaryformat bitreader bitstreamreader bitwriter bpf bpfasmparser bpfcodegen bpfdesc bpfdisassembler bpfinfo cfguard cgdata codegen codegentypes core coroutines coverage csky cskyasmparser cskycodegen cskydesc cskydisassembler cskyinfo debuginfobtf debuginfocodeview debuginfodwarf debuginfodwarflowlevel debuginfogsym debuginfologicalview debuginfomsf debuginfopdb demangle dlltooldriver dwarfcfichecker dwarflinker dwarflinkerclassic dwarflinkerparallel dwp engine executionengine extensions filecheck frontendatomic frontenddirective frontenddriver frontendhlsl frontendoffloading frontendopenacc frontendopenmp fuzzercli fuzzmutate globalisel hexagon hexagonasmparser hexagoncodegen hexagondesc hexagondisassembler hexagoninfo hipstdpar instcombine instrumentation interfacestub interpreter ipo irprinter irreader jitlink libdriver lineeditor linker loongarch loongarchasmparser loongarchcodegen loongarchdesc loongarchdisassembler loongarchinfo lto m68k m68kasmparser m68kcodegen m68kdesc m68kdisassembler m68kinfo mc mca mcdisassembler mcjit mcparser mips mipsasmparser mipscodegen mipsdesc mipsdisassembler mipsinfo mirparser msp430 msp430asmparser msp430codegen msp430desc msp430disassembler msp430info native nativecodegen nvptx nvptxcodegen nvptxdesc nvptxinfo objcarcopts objcopy object objectyaml option orcdebugging orcjit orcshared orctargetprocess passes powerpc powerpcasmparser powerpccodegen powerpcdesc powerpcdisassembler powerpcinfo profiledata remarks riscv riscvasmparser riscvcodegen riscvdesc riscvdisassembler riscvinfo riscvtargetmca runtimedyld sandboxir scalaropts selectiondag sparc sparcasmparser sparccodegen sparcdesc sparcdisassembler sparcinfo support symbolize systemz systemzasmparser systemzcodegen systemzdesc systemzdisassembler systemzinfo tablegen target targetparser telemetry textapi textapibinaryreader transformutils vectorize webassembly webassemblyasmparser webassemblycodegen webassemblydesc webassemblydisassembler webassemblyinfo webassemblyutils windowsdriver windowsmanifest x86 x86asmparser x86codegen x86desc x86disassembler x86info x86targetmca xray xtensa xtensaasmparser xtensacodegen xtensadesc xtensadisassembler xtensainfo" LLVM_FILECHECK="/checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm/bin/FileCheck" PYTHON="/usr/bin/python3" RUSTC="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" RUSTDOC="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustdoc" SOURCE_ROOT="/checkout" TARGET="x86_64-unknown-linux-gnu" TARGET_EXE_DYLIB_PATH="/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-make/const-trait-stable-toolchain/rmake"
stdout: none
--- stderr -------------------------------

thread 'main' (37427) panicked at /checkout/tests/run-make/const-trait-stable-toolchain/rmake.rs:23:10:
test failed: `const-super-trait-stable-enabled.stderr` is different from `(rustc)`
---
   0: __rustc::rust_begin_unwind
             at /rustc/3b4dd9bf1410f8da6329baa36ce5e37673cbbd1f/library/std/src/panicking.rs:698:5
   1: core::panicking::panic_fmt
             at /rustc/3b4dd9bf1410f8da6329baa36ce5e37673cbbd1f/library/core/src/panicking.rs:80:14
   2: <run_make_support::diff::Diff>::run
   3: rmake::main
   4: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
------------------------------------------

---- [run-make] tests/run-make/const-trait-stable-toolchain stdout end ----

For more information how to resolve CI failures of this job, visit this link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs F-explicit_tail_calls `#![feature(explicit_tail_calls)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants