Skip to content

Commit 20656d9

Browse files
authored
Rollup merge of rust-lang#123898 - fmease:gci-cmp-impl-item-lt-params, r=compiler-errors
Generic associated consts: Check regions earlier when comparing impl with trait item def Fixes rust-lang#123836. r? compiler-errors or compiler
2 parents d1782db + dfbdce0 commit 20656d9

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ pub(super) fn compare_impl_const_raw(
17231723

17241724
compare_number_of_generics(tcx, impl_const_item, trait_const_item, false)?;
17251725
compare_generic_param_kinds(tcx, impl_const_item, trait_const_item, false)?;
1726+
check_region_bounds_on_impl_item(tcx, impl_const_item, trait_const_item, false)?;
17261727
compare_const_predicate_entailment(tcx, impl_const_item, trait_const_item, impl_trait_ref)
17271728
}
17281729

@@ -1763,8 +1764,6 @@ fn compare_const_predicate_entailment<'tcx>(
17631764
let impl_ct_predicates = tcx.predicates_of(impl_ct.def_id);
17641765
let trait_ct_predicates = tcx.predicates_of(trait_ct.def_id);
17651766

1766-
check_region_bounds_on_impl_item(tcx, impl_ct, trait_ct, false)?;
1767-
17681767
// The predicates declared by the impl definition, the trait and the
17691768
// associated const in the trait are assumed.
17701769
let impl_predicates = tcx.predicates_of(impl_ct_predicates.parent.unwrap());
@@ -1866,6 +1865,7 @@ pub(super) fn compare_impl_ty<'tcx>(
18661865
let _: Result<(), ErrorGuaranteed> = try {
18671866
compare_number_of_generics(tcx, impl_ty, trait_ty, false)?;
18681867
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
1868+
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
18691869
compare_type_predicate_entailment(tcx, impl_ty, trait_ty, impl_trait_ref)?;
18701870
check_type_bounds(tcx, trait_ty, impl_ty, impl_trait_ref)?;
18711871
};
@@ -1886,8 +1886,6 @@ fn compare_type_predicate_entailment<'tcx>(
18861886
let impl_ty_predicates = tcx.predicates_of(impl_ty.def_id);
18871887
let trait_ty_predicates = tcx.predicates_of(trait_ty.def_id);
18881888

1889-
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
1890-
18911889
let impl_ty_own_bounds = impl_ty_predicates.instantiate_own(tcx, impl_args);
18921890
if impl_ty_own_bounds.len() == 0 {
18931891
// Nothing to check.

tests/ui/generic-const-items/compare-impl-item.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ trait Trait<P> {
66
const B<const K: u64, const Q: u64>: u64;
77
const C<T>: T;
88
const D<const N: usize>: usize;
9+
const E<'a>: &'a ();
910

10-
const E: usize;
11-
const F<T: PartialEq>: ();
11+
const F: usize;
12+
const G<T: PartialEq>: ();
1213
}
1314

1415
impl<P> Trait<P> for () {
@@ -20,11 +21,13 @@ impl<P> Trait<P> for () {
2021
//~^ ERROR const `C` has 0 type parameters but its trait declaration has 1 type parameter
2122
const D<const N: u16>: u16 = N;
2223
//~^ ERROR const `D` has an incompatible generic parameter for trait `Trait`
24+
const E: &'static () = &();
25+
//~^ ERROR lifetime parameters or bounds on const `E` do not match the trait declaration
2326

24-
const E: usize = 1024
27+
const F: usize = 1024
2528
where
2629
P: Copy; //~ ERROR impl has stricter requirements than trait
27-
const F<T: Eq>: () = (); //~ ERROR impl has stricter requirements than trait
30+
const G<T: Eq>: () = (); //~ ERROR impl has stricter requirements than trait
2831
}
2932

3033
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0049]: const `A` has 1 type parameter but its trait declaration has 0 type parameters
2-
--> $DIR/compare-impl-item.rs:15:13
2+
--> $DIR/compare-impl-item.rs:16:13
33
|
44
LL | const A: ();
55
| - expected 0 type parameters
@@ -8,7 +8,7 @@ LL | const A<T>: () = ();
88
| ^ found 1 type parameter
99

1010
error[E0049]: const `B` has 1 const parameter but its trait declaration has 2 const parameters
11-
--> $DIR/compare-impl-item.rs:17:13
11+
--> $DIR/compare-impl-item.rs:18:13
1212
|
1313
LL | const B<const K: u64, const Q: u64>: u64;
1414
| ------------ ------------
@@ -19,7 +19,7 @@ LL | const B<const K: u64>: u64 = 0;
1919
| ^^^^^^^^^^^^ found 1 const parameter
2020

2121
error[E0049]: const `C` has 0 type parameters but its trait declaration has 1 type parameter
22-
--> $DIR/compare-impl-item.rs:19:13
22+
--> $DIR/compare-impl-item.rs:20:13
2323
|
2424
LL | const C<T>: T;
2525
| - expected 1 type parameter
@@ -28,7 +28,7 @@ LL | const C<'a>: &'a str = "";
2828
| ^^ found 0 type parameters
2929

3030
error[E0053]: const `D` has an incompatible generic parameter for trait `Trait`
31-
--> $DIR/compare-impl-item.rs:21:13
31+
--> $DIR/compare-impl-item.rs:22:13
3232
|
3333
LL | trait Trait<P> {
3434
| -----
@@ -42,25 +42,34 @@ LL | impl<P> Trait<P> for () {
4242
LL | const D<const N: u16>: u16 = N;
4343
| ^^^^^^^^^^^^ found const parameter of type `u16`
4444

45+
error[E0195]: lifetime parameters or bounds on const `E` do not match the trait declaration
46+
--> $DIR/compare-impl-item.rs:24:12
47+
|
48+
LL | const E<'a>: &'a ();
49+
| ---- lifetimes in impl do not match this const in trait
50+
...
51+
LL | const E: &'static () = &();
52+
| ^ lifetimes do not match const in trait
53+
4554
error[E0276]: impl has stricter requirements than trait
46-
--> $DIR/compare-impl-item.rs:26:12
55+
--> $DIR/compare-impl-item.rs:29:12
4756
|
48-
LL | const E: usize;
49-
| -------------- definition of `E` from trait
57+
LL | const F: usize;
58+
| -------------- definition of `F` from trait
5059
...
5160
LL | P: Copy;
5261
| ^^^^ impl has extra requirement `P: Copy`
5362

5463
error[E0276]: impl has stricter requirements than trait
55-
--> $DIR/compare-impl-item.rs:27:16
64+
--> $DIR/compare-impl-item.rs:30:16
5665
|
57-
LL | const F<T: PartialEq>: ();
58-
| ------------------------- definition of `F` from trait
66+
LL | const G<T: PartialEq>: ();
67+
| ------------------------- definition of `G` from trait
5968
...
60-
LL | const F<T: Eq>: () = ();
69+
LL | const G<T: Eq>: () = ();
6170
| ^^ impl has extra requirement `T: Eq`
6271

63-
error: aborting due to 6 previous errors
72+
error: aborting due to 7 previous errors
6473

65-
Some errors have detailed explanations: E0049, E0053, E0276.
74+
Some errors have detailed explanations: E0049, E0053, E0195, E0276.
6675
For more information about an error, try `rustc --explain E0049`.

0 commit comments

Comments
 (0)