Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,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.path.span.shrink_to_lo(),
format!("make the `impl` of trait `{trait_name}` `const`"),
"const ".to_string(),
Applicability::MachineApplicable,
);
} else {
diag.span_note(
impl_span,
format!("trait `{trait_name}` is implemented but not `const`"),
);
}
}
}
diag
}

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/traits/const-traits/assoc-type.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/traits/const-traits/assoc-type.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/call-const-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/call-generic-method-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
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);
}


error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ note: required by a bound in `equals_self`
|
LL | const fn equals_self<T: [const] Foo>(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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/traits/const-traits/const-drop-fail-2.precise.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ note: required by a bound in `check`
|
LL | const fn check<T: [const] Destruct>(_: 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

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/traits/const-traits/const-drop-fail-2.stock.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ note: required by a bound in `check`
|
LL | const fn check<T: [const] Destruct>(_: 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

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/traits/const-traits/const-impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LL | assert!(cmp(&()));
| |
| required by a bound introduced by this call
|
note: trait `PartialEq` is implemented but not `const`
--> $SRC_DIR/core/src/cmp.rs:LL:COL
note: required by a bound in `cmp`
--> $DIR/const-impl-trait.rs:11:23
|
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/traits/const-traits/const-opaque.no.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ note: required by a bound in `bar`
|
LL | const fn bar<T: [const] Foo>(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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ LL | #[derive_const(Default, PartialEq)]
| --------- in this derive macro expansion
LL | pub struct S((), A);
| ^^
|
note: trait `PartialEq` is implemented but not `const`
--> $SRC_DIR/core/src/cmp.rs:LL:COL

error: aborting due to 1 previous error

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/traits/const-traits/cross-crate.gatednc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ note: required by a bound in `foo`
|
LL | const fn foo<T>() 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ note: required by a bound in `Foo::Assoc`
|
LL | type Assoc<T>: [const] Bar
| ^^^^^^^^^^^ required by this bound in `Foo::Assoc`
help: make the `impl` of trait `Bar` `const`
|
LL | impl<T> const Bar for N<T> where T: Bar {}
| +++++

error[E0277]: the trait bound `T: [const] Bar` is not satisfied
--> $DIR/item-bound-entailment-fails.rs:24:21
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/minicore-deref-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions tests/ui/traits/const-traits/minicore-fn-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ note: required by a bound in `call_indirect`
|
LL | const fn call_indirect<T: [const] Fn()>(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

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/no-explicit-const-params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ error[E0277]: the trait bound `(): const Bar` is not satisfied
|
LL | <() as Bar<false>>::bar();
| ^^
|
help: make the `impl` of trait `Bar` `const`
|
LL | impl const Bar for () {
| +++++

error: aborting due to 5 previous errors

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/specializing-constness-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ error[E0277]: the trait bound `T: [const] A` is not satisfied
|
LL | <T as A>::a();
| ^
|
help: make the `impl` of trait `A` `const`
|
LL | impl<T: Default> const A for T {
| +++++

error: aborting due to 1 previous error

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/traits/const-traits/super-traits-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down