Skip to content

Commit 2f3f895

Browse files
authored
Unrolled build for #159968
Rollup merge of #159968 - Randl:fix_sugg, r=oli-obk Fix the const impl suggestion r? @oli-obk
2 parents 4fefe36 + 87a3711 commit 2f3f895

22 files changed

Lines changed: 90 additions & 49 deletions

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,12 +949,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
949949
}
950950
} else if let Some(impl_did) = impl_did.as_local()
951951
&& let item = self.tcx.hir_expect_item(impl_did)
952-
&& let hir::ItemKind::Impl(item) = item.kind
953-
&& let Some(of_trait) = item.of_trait
952+
&& let hir::ItemKind::Impl(impl_) = item.kind
953+
&& impl_.of_trait.is_some()
954954
{
955955
// trait is const, impl is local and not const
956956
diag.span_suggestion_verbose(
957-
of_trait.trait_ref.path.span.shrink_to_lo(),
957+
item.span.shrink_to_lo(),
958958
format!("make the `impl` of trait `{trait_name}` `const`"),
959959
"const ".to_string(),
960960
Applicability::MaybeIncorrect,

tests/ui/comptime/comptime_impl.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ LL | Bar.foo();
1414
|
1515
help: make the `impl` of trait `Foo` `const`
1616
|
17-
LL | impl const Foo for Bar {
18-
| +++++
17+
LL | const impl Foo for Bar {
18+
| +++++
1919

2020
error[E0277]: the trait bound `Bar: const Foo` is not satisfied
2121
--> $DIR/comptime_impl.rs:31:9
@@ -25,8 +25,8 @@ LL | Bar.bar();
2525
|
2626
help: make the `impl` of trait `Foo` `const`
2727
|
28-
LL | impl const Foo for Bar {
29-
| +++++
28+
LL | const impl Foo for Bar {
29+
| +++++
3030

3131
error: aborting due to 3 previous errors
3232

tests/ui/traits/const-traits/assoc-type.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | type Bar: [const] Add;
1111
| ^^^^^^^^^^^ required by this bound in `Foo::Bar`
1212
help: make the `impl` of trait `Add` `const`
1313
|
14-
LL | impl const Add for NonConstAdd {
15-
| +++++
14+
LL | const impl Add for NonConstAdd {
15+
| +++++
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/traits/const-traits/assoc-type.next.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ LL | type Bar: [const] Add;
1111
| ^^^^^^^^^^^ required by this bound in `Foo::Bar`
1212
help: make the `impl` of trait `Add` `const`
1313
|
14-
LL | impl const Add for NonConstAdd {
15-
| +++++
14+
LL | const impl Add for NonConstAdd {
15+
| +++++
1616

1717
error: aborting due to 1 previous error
1818

tests/ui/traits/const-traits/call-const-closure.next.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | (const || ().foo())();
66
|
77
help: make the `impl` of trait `Bar` `const`
88
|
9-
LL | impl const Bar for () {
10-
| +++++
9+
LL | const impl Bar for () {
10+
| +++++
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/traits/const-traits/call-const-closure.old.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | (const || ().foo())();
66
|
77
help: make the `impl` of trait `Bar` `const`
88
|
9-
LL | impl const Bar for () {
10-
| +++++
9+
LL | const impl Bar for () {
10+
| +++++
1111

1212
error: aborting due to 1 previous error
1313

tests/ui/traits/const-traits/call-const-trait-method-fail.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ pub const trait Plus {
55
fn plus(self, rhs: Self) -> Self;
66
}
77

8+
9+
pub const unsafe trait Minus {
10+
fn minus(self, rhs: Self) -> Self;
11+
}
12+
813
const impl Plus for i32 {
914
fn plus(self, rhs: Self) -> Self {
1015
self + rhs
@@ -17,6 +22,20 @@ impl Plus for u32 {
1722
}
1823
}
1924

25+
26+
const unsafe impl Minus for i32 {
27+
fn minus(self, rhs: Self) -> Self {
28+
self - rhs
29+
}
30+
}
31+
32+
unsafe impl Minus for u32 {
33+
fn minus(self, rhs: Self) -> Self {
34+
self - rhs
35+
}
36+
}
37+
38+
2039
pub const fn add_i32(a: i32, b: i32) -> i32 {
2140
a.plus(b) // ok
2241
}
@@ -26,4 +45,15 @@ pub const fn add_u32(a: u32, b: u32) -> u32 {
2645
//~^ ERROR the trait bound `u32: [const] Plus`
2746
}
2847

48+
49+
pub const unsafe fn sub_i32(a: i32, b: i32) -> i32 {
50+
a.minus(b) // ok
51+
}
52+
53+
pub const unsafe fn sub_u32(a: u32, b: u32) -> u32 {
54+
a.minus(b)
55+
//~^ ERROR the trait bound `u32: [const] Minus`
56+
}
57+
58+
2959
fn main() {}
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
error[E0277]: the trait bound `u32: [const] Plus` is not satisfied
2-
--> $DIR/call-const-trait-method-fail.rs:25:5
2+
--> $DIR/call-const-trait-method-fail.rs:44:5
33
|
44
LL | a.plus(b)
55
| ^
66
|
77
help: make the `impl` of trait `Plus` `const`
88
|
9-
LL | impl const Plus for u32 {
10-
| +++++
9+
LL | const impl Plus for u32 {
10+
| +++++
1111

12-
error: aborting due to 1 previous error
12+
error[E0277]: the trait bound `u32: [const] Minus` is not satisfied
13+
--> $DIR/call-const-trait-method-fail.rs:54:5
14+
|
15+
LL | a.minus(b)
16+
| ^
17+
|
18+
help: make the `impl` of trait `Minus` `const`
19+
|
20+
LL | const unsafe impl Minus for u32 {
21+
| +++++
22+
23+
error: aborting due to 2 previous errors
1324

1425
For more information about this error, try `rustc --explain E0277`.

tests/ui/traits/const-traits/call-generic-method-nonconst.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ LL | const fn equals_self<T: [const] Foo>(t: &T) -> bool {
1313
| ^^^^^^^^^^^ required by this bound in `equals_self`
1414
help: make the `impl` of trait `Foo` `const`
1515
|
16-
LL | impl const Foo for S {
17-
| +++++
16+
LL | const impl Foo for S {
17+
| +++++
1818

1919
error: aborting due to 1 previous error
2020

tests/ui/traits/const-traits/const-closure-trait-method-fail.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ LL | const _: () = assert!(need_const_closure(Tr::a) == 42);
66
|
77
help: make the `impl` of trait `Tr` `const`
88
|
9-
LL | impl const Tr for () {
10-
| +++++
9+
LL | const impl Tr for () {
10+
| +++++
1111

1212
error: aborting due to 1 previous error
1313

0 commit comments

Comments
 (0)