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 8af061168a865..5f456edb8d7f6 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 @@ -949,12 +949,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { } } else 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 + && let hir::ItemKind::Impl(impl_) = item.kind + && impl_.of_trait.is_some() { // trait is const, impl is local and not const diag.span_suggestion_verbose( - of_trait.trait_ref.path.span.shrink_to_lo(), + item.span.shrink_to_lo(), format!("make the `impl` of trait `{trait_name}` `const`"), "const ".to_string(), Applicability::MaybeIncorrect, diff --git a/tests/ui/comptime/comptime_impl.stderr b/tests/ui/comptime/comptime_impl.stderr index 4231df305cfc1..4c55736c0bb63 100644 --- a/tests/ui/comptime/comptime_impl.stderr +++ b/tests/ui/comptime/comptime_impl.stderr @@ -14,8 +14,8 @@ LL | Bar.foo(); | help: make the `impl` of trait `Foo` `const` | -LL | impl const Foo for Bar { - | +++++ +LL | const impl Foo for Bar { + | +++++ error[E0277]: the trait bound `Bar: const Foo` is not satisfied --> $DIR/comptime_impl.rs:31:9 @@ -25,8 +25,8 @@ LL | Bar.bar(); | help: make the `impl` of trait `Foo` `const` | -LL | impl const Foo for Bar { - | +++++ +LL | const impl Foo for Bar { + | +++++ error: aborting due to 3 previous errors diff --git a/tests/ui/traits/const-traits/assoc-type.current.stderr b/tests/ui/traits/const-traits/assoc-type.current.stderr index b730fd8ad3662..f66e9e03b0754 100644 --- a/tests/ui/traits/const-traits/assoc-type.current.stderr +++ b/tests/ui/traits/const-traits/assoc-type.current.stderr @@ -11,8 +11,8 @@ 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 { - | +++++ +LL | const impl 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 b730fd8ad3662..f66e9e03b0754 100644 --- a/tests/ui/traits/const-traits/assoc-type.next.stderr +++ b/tests/ui/traits/const-traits/assoc-type.next.stderr @@ -11,8 +11,8 @@ 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 { - | +++++ +LL | const impl Add for NonConstAdd { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-const-closure.next.stderr b/tests/ui/traits/const-traits/call-const-closure.next.stderr index 9a851a97f186a..bdea41585c666 100644 --- a/tests/ui/traits/const-traits/call-const-closure.next.stderr +++ b/tests/ui/traits/const-traits/call-const-closure.next.stderr @@ -6,8 +6,8 @@ LL | (const || ().foo())(); | help: make the `impl` of trait `Bar` `const` | -LL | impl const Bar for () { - | +++++ +LL | const impl Bar for () { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-const-closure.old.stderr b/tests/ui/traits/const-traits/call-const-closure.old.stderr index 9a851a97f186a..bdea41585c666 100644 --- a/tests/ui/traits/const-traits/call-const-closure.old.stderr +++ b/tests/ui/traits/const-traits/call-const-closure.old.stderr @@ -6,8 +6,8 @@ LL | (const || ().foo())(); | help: make the `impl` of trait `Bar` `const` | -LL | impl const Bar for () { - | +++++ +LL | const impl Bar for () { + | +++++ error: aborting due to 1 previous error diff --git a/tests/ui/traits/const-traits/call-const-trait-method-fail.rs b/tests/ui/traits/const-traits/call-const-trait-method-fail.rs index dd8e3dfde4a3d..4b2165b110481 100644 --- a/tests/ui/traits/const-traits/call-const-trait-method-fail.rs +++ b/tests/ui/traits/const-traits/call-const-trait-method-fail.rs @@ -5,6 +5,11 @@ pub const trait Plus { fn plus(self, rhs: Self) -> Self; } + +pub const unsafe trait Minus { + fn minus(self, rhs: Self) -> Self; +} + const impl Plus for i32 { fn plus(self, rhs: Self) -> Self { self + rhs @@ -17,6 +22,20 @@ impl Plus for u32 { } } + +const unsafe impl Minus for i32 { + fn minus(self, rhs: Self) -> Self { + self - rhs + } +} + +unsafe impl Minus for u32 { + fn minus(self, rhs: Self) -> Self { + self - rhs + } +} + + pub const fn add_i32(a: i32, b: i32) -> i32 { a.plus(b) // ok } @@ -26,4 +45,15 @@ pub const fn add_u32(a: u32, b: u32) -> u32 { //~^ ERROR the trait bound `u32: [const] Plus` } + +pub const unsafe fn sub_i32(a: i32, b: i32) -> i32 { + a.minus(b) // ok +} + +pub const unsafe fn sub_u32(a: u32, b: u32) -> u32 { + a.minus(b) + //~^ ERROR the trait bound `u32: [const] Minus` +} + + fn main() {} 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 6e235d9cb6f0c..7ee8a0e935555 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 @@ -1,14 +1,25 @@ error[E0277]: the trait bound `u32: [const] Plus` is not satisfied - --> $DIR/call-const-trait-method-fail.rs:25:5 + --> $DIR/call-const-trait-method-fail.rs:44:5 | LL | a.plus(b) | ^ | help: make the `impl` of trait `Plus` `const` | -LL | impl const Plus for u32 { - | +++++ +LL | const impl Plus for u32 { + | +++++ -error: aborting due to 1 previous error +error[E0277]: the trait bound `u32: [const] Minus` is not satisfied + --> $DIR/call-const-trait-method-fail.rs:54:5 + | +LL | a.minus(b) + | ^ + | +help: make the `impl` of trait `Minus` `const` + | +LL | const unsafe impl Minus for u32 { + | +++++ + +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0277`. 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 91d3918372601..d641e22a9a905 100644 --- a/tests/ui/traits/const-traits/call-generic-method-nonconst.stderr +++ b/tests/ui/traits/const-traits/call-generic-method-nonconst.stderr @@ -13,8 +13,8 @@ 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 { - | +++++ +LL | const impl 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 93563dd12f952..30fbeb72fab9a 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 @@ -6,8 +6,8 @@ LL | const _: () = assert!(need_const_closure(Tr::a) == 42); | help: make the `impl` of trait `Tr` `const` | -LL | impl const Tr for () { - | +++++ +LL | const impl 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 55a12f95a6d19..c5a0c8743fa13 100644 --- a/tests/ui/traits/const-traits/const-default-method-bodies.stderr +++ b/tests/ui/traits/const-traits/const-default-method-bodies.stderr @@ -6,8 +6,8 @@ LL | NonConstImpl.a(); | help: make the `impl` of trait `ConstDefaultFn` `const` | -LL | impl const ConstDefaultFn for NonConstImpl { - | +++++ +LL | const impl 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 c6d71bde6a368..b43a67667967b 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 @@ -18,8 +18,8 @@ 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 {} - | +++++ +LL | const impl 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 c6d71bde6a368..b43a67667967b 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 @@ -18,8 +18,8 @@ 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 {} - | +++++ +LL | const impl 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 9f17b0d4354d7..d2b83ac2f3636 100644 --- a/tests/ui/traits/const-traits/const-opaque.no.stderr +++ b/tests/ui/traits/const-traits/const-opaque.no.stderr @@ -13,8 +13,8 @@ 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 () { - | +++++ +LL | const impl Foo for () { + | +++++ error[E0277]: the trait bound `(): const Foo` is not satisfied --> $DIR/const-opaque.rs:32:12 @@ -24,8 +24,8 @@ LL | opaque.method(); | help: make the `impl` of trait `Foo` `const` | -LL | impl const Foo for () { - | +++++ +LL | const impl Foo for () { + | +++++ error: aborting due to 2 previous errors 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 a436221d75412..7bd1118022647 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 @@ -11,8 +11,8 @@ 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 () {} - | +++++ +LL | const impl 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 f93e57d5fd6b2..d92297fa7b69f 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 @@ -6,8 +6,8 @@ LL | ().a() | help: make the `impl` of trait `Tr` `const` | -LL | impl const Tr for () {} - | +++++ +LL | const impl 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 ffa9679e86ce6..375cb58580490 100644 --- a/tests/ui/traits/const-traits/item-bound-entailment-fails.stderr +++ b/tests/ui/traits/const-traits/item-bound-entailment-fails.stderr @@ -11,8 +11,8 @@ 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 {} - | +++++ +LL | const impl 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 e6c087cb9d822..c64dce93cc562 100644 --- a/tests/ui/traits/const-traits/minicore-deref-fail.stderr +++ b/tests/ui/traits/const-traits/minicore-deref-fail.stderr @@ -6,8 +6,8 @@ LL | *Ty; | help: make the `impl` of trait `Deref` `const` | -LL | impl const Deref for Ty { - | +++++ +LL | const impl 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 0c260f7f33a77..949a1270b60e1 100644 --- a/tests/ui/traits/const-traits/minicore-fn-fail.stderr +++ b/tests/ui/traits/const-traits/minicore-fn-fail.stderr @@ -13,8 +13,8 @@ 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 () {} - | +++++ +LL | const impl 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 efd90482f7742..72c64120a3d77 100644 --- a/tests/ui/traits/const-traits/no-explicit-const-params.stderr +++ b/tests/ui/traits/const-traits/no-explicit-const-params.stderr @@ -62,8 +62,8 @@ LL | <() as Bar>::bar(); | help: make the `impl` of trait `Bar` `const` | -LL | impl const Bar for () { - | +++++ +LL | const impl Bar for () { + | +++++ error: aborting due to 5 previous errors diff --git a/tests/ui/traits/const-traits/specialization/specializing-constness-2.stderr b/tests/ui/traits/const-traits/specialization/specializing-constness-2.stderr index bb1c9ac785314..aed032714b6df 100644 --- a/tests/ui/traits/const-traits/specialization/specializing-constness-2.stderr +++ b/tests/ui/traits/const-traits/specialization/specializing-constness-2.stderr @@ -6,8 +6,8 @@ LL | ::a(); | help: make the `impl` of trait `A` `const` | -LL | impl const A for T { - | +++++ +LL | const impl 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 bb3bc51c44a2c..8b8b84c850f33 100644 --- a/tests/ui/traits/const-traits/super-traits-fail.stderr +++ b/tests/ui/traits/const-traits/super-traits-fail.stderr @@ -6,8 +6,8 @@ LL | const impl Bar for S {} | help: make the `impl` of trait `Foo` `const` | -LL | impl const Foo for S { - | +++++ +LL | const impl Foo for S { + | +++++ error: aborting due to 1 previous error