Skip to content

Commit 0cdc673

Browse files
Actually do validation for poly trait refs with ? modifier
1 parent 1e4f10b commit 0cdc673

File tree

11 files changed

+62
-46
lines changed

11 files changed

+62
-46
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

+10-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
659659
trait_ref: &hir::TraitRef<'tcx>,
660660
span: Span,
661661
constness: hir::BoundConstness,
662-
polarity: ty::PredicatePolarity,
662+
polarity: hir::BoundPolarity,
663663
self_ty: Ty<'tcx>,
664664
bounds: &mut Bounds<'tcx>,
665665
predicate_filter: PredicateFilter,
@@ -681,6 +681,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
681681
Some(self_ty),
682682
);
683683

684+
let polarity = match polarity {
685+
rustc_ast::BoundPolarity::Positive => ty::PredicatePolarity::Positive,
686+
rustc_ast::BoundPolarity::Negative(_) => ty::PredicatePolarity::Negative,
687+
rustc_ast::BoundPolarity::Maybe(_) => {
688+
// No-op.
689+
return arg_count;
690+
}
691+
};
692+
684693
if let hir::BoundConstness::Always(span) | hir::BoundConstness::Maybe(span) = constness
685694
&& !self.tcx().is_const_trait(trait_def_id)
686695
{

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`.

0 commit comments

Comments
 (0)