Skip to content

Commit ae43ce3

Browse files
authored
Unrolled build for rust-lang#132209
Rollup merge of rust-lang#132209 - compiler-errors:modifiers, r=fmease Fix validation when lowering `?` trait bounds Pass the unlowered (`rustc_hir`) polarity to `lower_poly_trait_ref`. This allows us to actually *validate* that generic args are actually valid on `?Trait` paths. This actually regressed in rust-lang#113671 because that PR changed the behavior where we were inadvertently re-lowering paths as `BoundPolarity::Positive`, which was also coincidentally the only place we were enforcing the generics on `?Trait` paths were correct.
2 parents 24254ef + 06a49b6 commit ae43ce3

13 files changed

+119
-58
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

-6
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
168168
match hir_bound {
169169
hir::GenericBound::Trait(poly_trait_ref) => {
170170
let hir::TraitBoundModifiers { constness, polarity } = poly_trait_ref.modifiers;
171-
let polarity = match polarity {
172-
rustc_ast::BoundPolarity::Positive => ty::PredicatePolarity::Positive,
173-
rustc_ast::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
174-
rustc_ast::BoundPolarity::Maybe(_) => continue,
175-
};
176-
177171
let _ = self.lower_poly_trait_ref(
178172
&poly_trait_ref.trait_ref,
179173
poly_trait_ref.span,

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
5151
&trait_bound.trait_ref,
5252
trait_bound.span,
5353
hir::BoundConstness::Never,
54-
ty::PredicatePolarity::Positive,
54+
hir::BoundPolarity::Positive,
5555
dummy_self,
5656
&mut bounds,
5757
PredicateFilter::SelfOnly,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+34-13
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
668668
trait_ref: &hir::TraitRef<'tcx>,
669669
span: Span,
670670
constness: hir::BoundConstness,
671-
polarity: ty::PredicatePolarity,
671+
polarity: hir::BoundPolarity,
672672
self_ty: Ty<'tcx>,
673673
bounds: &mut Bounds<'tcx>,
674674
predicate_filter: PredicateFilter,
@@ -690,15 +690,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
690690
Some(self_ty),
691691
);
692692

693-
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
694-
&& !self.tcx().is_const_trait(trait_def_id)
695-
{
696-
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
697-
span,
698-
modifier: constness.as_str(),
699-
});
700-
}
701-
702693
let tcx = self.tcx();
703694
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
704695
debug!(?bound_vars);
@@ -708,6 +699,36 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
708699
bound_vars,
709700
);
710701

702+
let polarity = match polarity {
703+
rustc_ast::BoundPolarity::Positive => ty::PredicatePolarity::Positive,
704+
rustc_ast::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
705+
rustc_ast::BoundPolarity::Maybe(_) => {
706+
// Validate associated type at least. We may want to reject these
707+
// outright in the future...
708+
for constraint in trait_segment.args().constraints {
709+
let _ = self.lower_assoc_item_constraint(
710+
trait_ref.hir_ref_id,
711+
poly_trait_ref,
712+
constraint,
713+
&mut Default::default(),
714+
&mut Default::default(),
715+
constraint.span,
716+
predicate_filter,
717+
);
718+
}
719+
return arg_count;
720+
}
721+
};
722+
723+
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
724+
&& !self.tcx().is_const_trait(trait_def_id)
725+
{
726+
self.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
727+
span,
728+
modifier: constness.as_str(),
729+
});
730+
}
731+
711732
match predicate_filter {
712733
PredicateFilter::All
713734
| PredicateFilter::SelfOnly
@@ -763,11 +784,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
763784
// since we should have emitted an error for them earlier, and they
764785
// would not be well-formed!
765786
if polarity != ty::PredicatePolarity::Positive {
766-
assert!(
767-
self.dcx().has_errors().is_some(),
787+
self.dcx().span_delayed_bug(
788+
constraint.span,
768789
"negative trait bounds should not have assoc item constraints",
769790
);
770-
continue;
791+
break;
771792
}
772793

773794
// Specify type to assert that error was already reported in `Err` case.

tests/ui/impl-trait/precise-capturing/bound-modifiers.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
fn polarity() -> impl Sized + ?use<> {}
44
//~^ ERROR expected identifier, found keyword `use`
55
//~| ERROR cannot find trait `r#use` in this scope
6-
//~| WARN relaxing a default bound only does something for `?Sized`
7-
//~| WARN relaxing a default bound only does something for `?Sized`
86

97
fn asyncness() -> impl Sized + async use<> {}
108
//~^ ERROR expected identifier, found keyword `use`

tests/ui/impl-trait/precise-capturing/bound-modifiers.stderr

+9-23
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ LL | fn polarity() -> impl Sized + ?use<> {}
55
| ^^^ expected identifier, found keyword
66

77
error: expected identifier, found keyword `use`
8-
--> $DIR/bound-modifiers.rs:9:38
8+
--> $DIR/bound-modifiers.rs:7:38
99
|
1010
LL | fn asyncness() -> impl Sized + async use<> {}
1111
| ^^^ expected identifier, found keyword
1212

1313
error: expected identifier, found keyword `use`
14-
--> $DIR/bound-modifiers.rs:14:38
14+
--> $DIR/bound-modifiers.rs:12:38
1515
|
1616
LL | fn constness() -> impl Sized + const use<> {}
1717
| ^^^ expected identifier, found keyword
1818

1919
error: expected identifier, found keyword `use`
20-
--> $DIR/bound-modifiers.rs:19:37
20+
--> $DIR/bound-modifiers.rs:17:37
2121
|
2222
LL | fn binder() -> impl Sized + for<'a> use<> {}
2323
| ^^^ expected identifier, found keyword
@@ -29,25 +29,25 @@ LL | fn polarity() -> impl Sized + ?use<> {}
2929
| ^^^ not found in this scope
3030

3131
error[E0405]: cannot find trait `r#use` in this scope
32-
--> $DIR/bound-modifiers.rs:9:38
32+
--> $DIR/bound-modifiers.rs:7:38
3333
|
3434
LL | fn asyncness() -> impl Sized + async use<> {}
3535
| ^^^ not found in this scope
3636

3737
error[E0405]: cannot find trait `r#use` in this scope
38-
--> $DIR/bound-modifiers.rs:14:38
38+
--> $DIR/bound-modifiers.rs:12:38
3939
|
4040
LL | fn constness() -> impl Sized + const use<> {}
4141
| ^^^ not found in this scope
4242

4343
error[E0405]: cannot find trait `r#use` in this scope
44-
--> $DIR/bound-modifiers.rs:19:37
44+
--> $DIR/bound-modifiers.rs:17:37
4545
|
4646
LL | fn binder() -> impl Sized + for<'a> use<> {}
4747
| ^^^ not found in this scope
4848

4949
error[E0658]: async closures are unstable
50-
--> $DIR/bound-modifiers.rs:9:32
50+
--> $DIR/bound-modifiers.rs:7:32
5151
|
5252
LL | fn asyncness() -> impl Sized + async use<> {}
5353
| ^^^^^
@@ -58,7 +58,7 @@ LL | fn asyncness() -> impl Sized + async use<> {}
5858
= help: to use an async block, remove the `||`: `async {`
5959

6060
error[E0658]: const trait impls are experimental
61-
--> $DIR/bound-modifiers.rs:14:32
61+
--> $DIR/bound-modifiers.rs:12:32
6262
|
6363
LL | fn constness() -> impl Sized + const use<> {}
6464
| ^^^^^
@@ -67,21 +67,7 @@ LL | fn constness() -> impl Sized + const use<> {}
6767
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
6868
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
6969

70-
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
71-
--> $DIR/bound-modifiers.rs:3:31
72-
|
73-
LL | fn polarity() -> impl Sized + ?use<> {}
74-
| ^^^^^^
75-
76-
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
77-
--> $DIR/bound-modifiers.rs:3:31
78-
|
79-
LL | fn polarity() -> impl Sized + ?use<> {}
80-
| ^^^^^^
81-
|
82-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
83-
84-
error: aborting due to 10 previous errors; 2 warnings emitted
70+
error: aborting due to 10 previous errors
8571

8672
Some errors have detailed explanations: E0405, E0658.
8773
For more information about an error, try `rustc --explain E0405`.

tests/ui/issues/issue-37534.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
struct Foo<T: ?Hash> {}
22
//~^ ERROR expected trait, found derive macro `Hash`
3-
//~^^ ERROR parameter `T` is never used
4-
//~^^^ WARN relaxing a default bound only does something for `?Sized`
3+
//~| WARN relaxing a default bound only does something for `?Sized`
54

65
fn main() {}

tests/ui/issues/issue-37534.stderr

+2-11
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ warning: relaxing a default bound only does something for `?Sized`; all other tr
1515
LL | struct Foo<T: ?Hash> {}
1616
| ^^^^^
1717

18-
error[E0392]: type parameter `T` is never used
19-
--> $DIR/issue-37534.rs:1:12
20-
|
21-
LL | struct Foo<T: ?Hash> {}
22-
| ^ unused type parameter
23-
|
24-
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
25-
26-
error: aborting due to 2 previous errors; 1 warning emitted
18+
error: aborting due to 1 previous error; 1 warning emitted
2719

28-
Some errors have detailed explanations: E0392, E0404.
29-
For more information about an error, try `rustc --explain E0392`.
20+
For more information about this error, try `rustc --explain E0404`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn uwu<T: ?Sized<i32>>() {}
2+
//~^ ERROR trait takes 0 generic arguments but 1 generic argument was supplied
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0107]: trait takes 0 generic arguments but 1 generic argument was supplied
2+
--> $DIR/maybe-bound-generics-deny.rs:1:12
3+
|
4+
LL | fn uwu<T: ?Sized<i32>>() {}
5+
| ^^^^^----- help: remove the unnecessary generics
6+
| |
7+
| expected 0 generic arguments
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0107`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
trait Trait {}
2+
3+
fn test<T: ?self::<i32>::Trait>() {}
4+
//~^ ERROR type arguments are not allowed on this type
5+
//~| WARN relaxing a default bound only does something for `?Sized`
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
2+
--> $DIR/maybe-bound-has-path-args.rs:3:12
3+
|
4+
LL | fn test<T: ?self::<i32>::Trait>() {}
5+
| ^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0109]: type arguments are not allowed on this type
8+
--> $DIR/maybe-bound-has-path-args.rs:3:20
9+
|
10+
LL | fn test<T: ?self::<i32>::Trait>() {}
11+
| ---- ^^^ type argument not allowed
12+
| |
13+
| not allowed on this type
14+
15+
error: aborting due to 1 previous error; 1 warning emitted
16+
17+
For more information about this error, try `rustc --explain E0109`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait HasAssoc {
2+
type Assoc;
3+
}
4+
fn hasassoc<T: ?HasAssoc<Assoc = ()>>() {}
5+
//~^ WARN relaxing a default bound
6+
7+
trait NoAssoc {}
8+
fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
9+
//~^ WARN relaxing a default bound
10+
//~| ERROR associated type `Missing` not found for `NoAssoc`
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
2+
--> $DIR/maybe-bound-with-assoc.rs:4:16
3+
|
4+
LL | fn hasassoc<T: ?HasAssoc<Assoc = ()>>() {}
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
7+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
8+
--> $DIR/maybe-bound-with-assoc.rs:8:15
9+
|
10+
LL | fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0220]: associated type `Missing` not found for `NoAssoc`
14+
--> $DIR/maybe-bound-with-assoc.rs:8:24
15+
|
16+
LL | fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
17+
| ^^^^^^^ associated type `Missing` not found
18+
19+
error: aborting due to 1 previous error; 2 warnings emitted
20+
21+
For more information about this error, try `rustc --explain E0220`.

0 commit comments

Comments
 (0)