Skip to content

Commit 63dce2e

Browse files
committed
Reword diagnostics about relaxed bounds in invalid contexts
1 parent a39d64d commit 63dce2e

20 files changed

+85
-80
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,9 +1874,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
18741874
bounded_ty,
18751875
bounds,
18761876
}) => {
1877-
let rbp = match bound_generic_params.is_empty() {
1878-
true => RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params),
1879-
false => RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::BoundVars),
1877+
let rbp = if bound_generic_params.is_empty() {
1878+
RelaxedBoundPolicy::AllowedIfOnTyParam(bounded_ty.id, params)
1879+
} else {
1880+
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::LateBoundVarsInScope)
18801881
};
18811882
hir::WherePredicateKind::BoundPredicate(hir::WhereBoundPredicate {
18821883
bound_generic_params: self.lower_generic_params(

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ enum RelaxedBoundPolicy<'a> {
287287
enum RelaxedBoundForbiddenReason {
288288
TraitObjectTy,
289289
SuperTrait,
290-
BoundVars,
290+
LateBoundVarsInScope,
291291
}
292292

293293
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -2085,11 +2085,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20852085

20862086
match reason {
20872087
RelaxedBoundForbiddenReason::TraitObjectTy => {
2088-
err("`?Trait` is not permitted in trait object types").emit();
2088+
err("relaxed bounds are not permitted in trait object types").emit();
20892089
return;
20902090
}
20912091
RelaxedBoundForbiddenReason::SuperTrait => {
2092-
let mut diag = err("`?Trait` is not permitted in supertraits");
2092+
let mut diag = err("relaxed bounds are not permitted in supertrait bounds");
20932093
if let Some(def_id) = trait_ref.trait_def_id()
20942094
&& self.tcx.is_lang_item(def_id, hir::LangItem::Sized)
20952095
{
@@ -2098,12 +2098,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20982098
diag.emit();
20992099
return;
21002100
}
2101-
RelaxedBoundForbiddenReason::BoundVars => {}
2101+
RelaxedBoundForbiddenReason::LateBoundVarsInScope => {}
21022102
};
21032103
}
21042104
}
21052105

2106-
err("`?Trait` bounds are only permitted at the point where a type parameter is declared")
2106+
err("this relaxed bound is not permitted here")
2107+
.with_note(
2108+
"in this context, relaxed bounds are only allowed on \
2109+
type parameters defined by the closest item",
2110+
)
21072111
.emit();
21082112
}
21092113

tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
44
//~^ ERROR expected trait, found associated function `Iterator::advance_by`
5-
//~| ERROR `?Trait` is not permitted in trait object types
5+
//~| ERROR relaxed bounds are not permitted in trait object types
66
todo!()
77
}

tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0404]: expected trait, found associated function `Iterator::advance_by`
44
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a trait
66

7-
error[E0658]: `?Trait` is not permitted in trait object types
7+
error[E0658]: relaxed bounds are not permitted in trait object types
88
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:29
99
|
1010
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {

tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ trait Tr {
77
fn main() {
88
let _: dyn Tr + ?Foo<Assoc = ()>;
99
//~^ ERROR: cannot find trait `Foo` in this scope
10-
//~| ERROR: `?Trait` is not permitted in trait object types
10+
//~| ERROR: relaxed bounds are not permitted in trait object types
1111
}

tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0405]: cannot find trait `Foo` in this scope
44
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
55
| ^^^ not found in this scope
66

7-
error[E0658]: `?Trait` is not permitted in trait object types
7+
error[E0658]: relaxed bounds are not permitted in trait object types
88
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:21
99
|
1010
LL | let _: dyn Tr + ?Foo<Assoc = ()>;

tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
trait Trait1 {}
44
auto trait Trait2 {}
5-
trait Trait3: ?Trait1 {}
6-
//~^ ERROR `?Trait` is not permitted in supertraits
7-
trait Trait4 where Self: ?Trait1 {}
8-
//~^ ERROR ?Trait` bounds are only permitted at the point where a type parameter is declared
5+
trait Trait3: ?Trait1 {} //~ ERROR relaxed bounds are not permitted in supertrait bounds
6+
trait Trait4 where Self: ?Trait1 {} //~ ERROR this relaxed bound is not permitted here
97

108
fn foo(_: Box<dyn Trait1 + ?Trait2>) {}
11-
//~^ ERROR `?Trait` is not permitted in trait object types
9+
//~^ ERROR relaxed bounds are not permitted in trait object types
1210
fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
1311
//~^ ERROR type parameter has more than one relaxed default bound, only one is supported
1412
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default

tests/ui/feature-gates/feature-gate-more-maybe-bounds.stderr

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `?Trait` is not permitted in supertraits
1+
error[E0658]: relaxed bounds are not permitted in supertrait bounds
22
--> $DIR/feature-gate-more-maybe-bounds.rs:5:15
33
|
44
LL | trait Trait3: ?Trait1 {}
@@ -7,17 +7,18 @@ LL | trait Trait3: ?Trait1 {}
77
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99

10-
error[E0658]: `?Trait` bounds are only permitted at the point where a type parameter is declared
11-
--> $DIR/feature-gate-more-maybe-bounds.rs:7:26
10+
error[E0658]: this relaxed bound is not permitted here
11+
--> $DIR/feature-gate-more-maybe-bounds.rs:6:26
1212
|
1313
LL | trait Trait4 where Self: ?Trait1 {}
1414
| ^^^^^^^
1515
|
1616
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
1717
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
18+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
1819

19-
error[E0658]: `?Trait` is not permitted in trait object types
20-
--> $DIR/feature-gate-more-maybe-bounds.rs:10:28
20+
error[E0658]: relaxed bounds are not permitted in trait object types
21+
--> $DIR/feature-gate-more-maybe-bounds.rs:8:28
2122
|
2223
LL | fn foo(_: Box<dyn Trait1 + ?Trait2>) {}
2324
| ^^^^^^^
@@ -26,7 +27,7 @@ LL | fn foo(_: Box<dyn Trait1 + ?Trait2>) {}
2627
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2728

2829
error[E0203]: type parameter has more than one relaxed default bound, only one is supported
29-
--> $DIR/feature-gate-more-maybe-bounds.rs:12:11
30+
--> $DIR/feature-gate-more-maybe-bounds.rs:10:11
3031
|
3132
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
3233
| ^^^^^^^ ^^^^^^^
@@ -35,31 +36,31 @@ LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
3536
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3637

3738
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
38-
--> $DIR/feature-gate-more-maybe-bounds.rs:12:11
39+
--> $DIR/feature-gate-more-maybe-bounds.rs:10:11
3940
|
4041
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
4142
| ^^^^^^^
4243

4344
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
44-
--> $DIR/feature-gate-more-maybe-bounds.rs:12:21
45+
--> $DIR/feature-gate-more-maybe-bounds.rs:10:21
4546
|
4647
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
4748
| ^^^^^^^
4849

4950
error[E0203]: type parameter has more than one relaxed default bound, only one is supported
50-
--> $DIR/feature-gate-more-maybe-bounds.rs:19:11
51+
--> $DIR/feature-gate-more-maybe-bounds.rs:17:11
5152
|
5253
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
5354
| ^^^^^^ ^^^^^^
5455

5556
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
56-
--> $DIR/feature-gate-more-maybe-bounds.rs:19:11
57+
--> $DIR/feature-gate-more-maybe-bounds.rs:17:11
5758
|
5859
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
5960
| ^^^^^^
6061

6162
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
62-
--> $DIR/feature-gate-more-maybe-bounds.rs:19:20
63+
--> $DIR/feature-gate-more-maybe-bounds.rs:17:20
6364
|
6465
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
6566
| ^^^^^^

tests/ui/maybe-bounds.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
trait Tr: ?Sized {}
2-
//~^ ERROR `?Trait` is not permitted in supertraits
2+
//~^ ERROR relaxed bounds are not permitted in supertrait bounds
33

44
type A1 = dyn Tr + (?Sized);
5-
//~^ ERROR `?Trait` is not permitted in trait object types
5+
//~^ ERROR relaxed bounds are not permitted in trait object types
66
type A2 = dyn for<'a> Tr + (?Sized);
7-
//~^ ERROR `?Trait` is not permitted in trait object types
7+
//~^ ERROR relaxed bounds are not permitted in trait object types
88

99
fn main() {}

tests/ui/maybe-bounds.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `?Trait` is not permitted in supertraits
1+
error[E0658]: relaxed bounds are not permitted in supertrait bounds
22
--> $DIR/maybe-bounds.rs:1:11
33
|
44
LL | trait Tr: ?Sized {}
@@ -8,7 +8,7 @@ LL | trait Tr: ?Sized {}
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99
= note: traits are `?Sized` by default
1010

11-
error[E0658]: `?Trait` is not permitted in trait object types
11+
error[E0658]: relaxed bounds are not permitted in trait object types
1212
--> $DIR/maybe-bounds.rs:4:20
1313
|
1414
LL | type A1 = dyn Tr + (?Sized);
@@ -17,7 +17,7 @@ LL | type A1 = dyn Tr + (?Sized);
1717
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
1818
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1919

20-
error[E0658]: `?Trait` is not permitted in trait object types
20+
error[E0658]: relaxed bounds are not permitted in trait object types
2121
--> $DIR/maybe-bounds.rs:6:28
2222
|
2323
LL | type A2 = dyn for<'a> Tr + (?Sized);

tests/ui/parser/trait-object-trait-parens.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ fn f<T: (Copy) + (?Sized) + (for<'a> Trait<'a>)>() {}
66

77
fn main() {
88
let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>;
9-
//~^ ERROR `?Trait` is not permitted in trait object types
9+
//~^ ERROR relaxed bounds are not permitted in trait object types
1010
//~| ERROR only auto traits can be used as additional traits
1111
//~| WARN trait objects without an explicit `dyn` are deprecated
1212
//~| WARN this is accepted in the current edition
1313
let _: Box<?Sized + (for<'a> Trait<'a>) + (Obj)>;
14-
//~^ ERROR `?Trait` is not permitted in trait object types
14+
//~^ ERROR relaxed bounds are not permitted in trait object types
1515
//~| ERROR only auto traits can be used as additional traits
1616
//~| WARN trait objects without an explicit `dyn` are deprecated
1717
//~| WARN this is accepted in the current edition
1818
let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;
19-
//~^ ERROR `?Trait` is not permitted in trait object types
19+
//~^ ERROR relaxed bounds are not permitted in trait object types
2020
//~| ERROR only auto traits can be used as additional traits
2121
//~| WARN trait objects without an explicit `dyn` are deprecated
2222
//~| WARN this is accepted in the current edition

tests/ui/parser/trait-object-trait-parens.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `?Trait` is not permitted in trait object types
1+
error[E0658]: relaxed bounds are not permitted in trait object types
22
--> $DIR/trait-object-trait-parens.rs:8:24
33
|
44
LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>;
@@ -7,7 +7,7 @@ LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>;
77
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99

10-
error[E0658]: `?Trait` is not permitted in trait object types
10+
error[E0658]: relaxed bounds are not permitted in trait object types
1111
--> $DIR/trait-object-trait-parens.rs:13:16
1212
|
1313
LL | let _: Box<?Sized + (for<'a> Trait<'a>) + (Obj)>;
@@ -16,7 +16,7 @@ LL | let _: Box<?Sized + (for<'a> Trait<'a>) + (Obj)>;
1616
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
1717
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1818

19-
error[E0658]: `?Trait` is not permitted in trait object types
19+
error[E0658]: relaxed bounds are not permitted in trait object types
2020
--> $DIR/trait-object-trait-parens.rs:18:44
2121
|
2222
LL | let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;

tests/ui/sized-hierarchy/default-supertrait.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ use std::marker::{MetaSized, PointeeSized};
66
trait Sized_: Sized { }
77

88
trait NegSized: ?Sized { }
9-
//~^ ERROR `?Trait` is not permitted in supertraits
9+
//~^ ERROR relaxed bounds are not permitted in supertrait bounds
1010

1111
trait MetaSized_: MetaSized { }
1212

1313
trait NegMetaSized: ?MetaSized { }
14-
//~^ ERROR `?Trait` is not permitted in supertraits
14+
//~^ ERROR relaxed bounds are not permitted in supertrait bounds
1515

1616

1717
trait PointeeSized_: PointeeSized { }
1818

1919
trait NegPointeeSized: ?PointeeSized { }
20-
//~^ ERROR `?Trait` is not permitted in supertraits
20+
//~^ ERROR relaxed bounds are not permitted in supertrait bounds
2121

2222
trait Bare {}
2323

tests/ui/sized-hierarchy/default-supertrait.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `?Trait` is not permitted in supertraits
1+
error[E0658]: relaxed bounds are not permitted in supertrait bounds
22
--> $DIR/default-supertrait.rs:8:17
33
|
44
LL | trait NegSized: ?Sized { }
@@ -8,7 +8,7 @@ LL | trait NegSized: ?Sized { }
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99
= note: traits are `?Sized` by default
1010

11-
error[E0658]: `?Trait` is not permitted in supertraits
11+
error[E0658]: relaxed bounds are not permitted in supertrait bounds
1212
--> $DIR/default-supertrait.rs:13:21
1313
|
1414
LL | trait NegMetaSized: ?MetaSized { }
@@ -17,7 +17,7 @@ LL | trait NegMetaSized: ?MetaSized { }
1717
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
1818
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1919

20-
error[E0658]: `?Trait` is not permitted in supertraits
20+
error[E0658]: relaxed bounds are not permitted in supertrait bounds
2121
--> $DIR/default-supertrait.rs:19:24
2222
|
2323
LL | trait NegPointeeSized: ?PointeeSized { }
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
// Test that `dyn ... + ?Sized + ...` is okay (though `?Sized` has no effect in trait objects).
1+
// Test that relaxed `Sized` bounds are rejected in trait object types.
22

33
trait Foo {}
44

55
type _0 = dyn ?Sized + Foo;
6-
//~^ ERROR `?Trait` is not permitted in trait object types
6+
//~^ ERROR relaxed bounds are not permitted in trait object types
77

88
type _1 = dyn Foo + ?Sized;
9-
//~^ ERROR `?Trait` is not permitted in trait object types
9+
//~^ ERROR relaxed bounds are not permitted in trait object types
1010

1111
type _2 = dyn Foo + ?Sized + ?Sized;
12-
//~^ ERROR `?Trait` is not permitted in trait object types
13-
//~| ERROR `?Trait` is not permitted in trait object types
12+
//~^ ERROR relaxed bounds are not permitted in trait object types
13+
//~| ERROR relaxed bounds are not permitted in trait object types
1414

1515
type _3 = dyn ?Sized + Foo;
16-
//~^ ERROR `?Trait` is not permitted in trait object types
16+
//~^ ERROR relaxed bounds are not permitted in trait object types
1717

1818
fn main() {}

tests/ui/traits/wf-object/maybe-bound.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `?Trait` is not permitted in trait object types
1+
error[E0658]: relaxed bounds are not permitted in trait object types
22
--> $DIR/maybe-bound.rs:5:15
33
|
44
LL | type _0 = dyn ?Sized + Foo;
@@ -7,7 +7,7 @@ LL | type _0 = dyn ?Sized + Foo;
77
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99

10-
error[E0658]: `?Trait` is not permitted in trait object types
10+
error[E0658]: relaxed bounds are not permitted in trait object types
1111
--> $DIR/maybe-bound.rs:8:21
1212
|
1313
LL | type _1 = dyn Foo + ?Sized;
@@ -16,7 +16,7 @@ LL | type _1 = dyn Foo + ?Sized;
1616
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
1717
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1818

19-
error[E0658]: `?Trait` is not permitted in trait object types
19+
error[E0658]: relaxed bounds are not permitted in trait object types
2020
--> $DIR/maybe-bound.rs:11:21
2121
|
2222
LL | type _2 = dyn Foo + ?Sized + ?Sized;
@@ -25,7 +25,7 @@ LL | type _2 = dyn Foo + ?Sized + ?Sized;
2525
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
2626
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2727

28-
error[E0658]: `?Trait` is not permitted in trait object types
28+
error[E0658]: relaxed bounds are not permitted in trait object types
2929
--> $DIR/maybe-bound.rs:11:30
3030
|
3131
LL | type _2 = dyn Foo + ?Sized + ?Sized;
@@ -34,7 +34,7 @@ LL | type _2 = dyn Foo + ?Sized + ?Sized;
3434
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
3535
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3636

37-
error[E0658]: `?Trait` is not permitted in trait object types
37+
error[E0658]: relaxed bounds are not permitted in trait object types
3838
--> $DIR/maybe-bound.rs:15:15
3939
|
4040
LL | type _3 = dyn ?Sized + Foo;

tests/ui/traits/wf-object/only-maybe-bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
type _0 = dyn ?Sized;
44
//~^ ERROR at least one trait is required for an object type [E0224]
5-
//~| ERROR ?Trait` is not permitted in trait object types
5+
//~| ERROR relaxed bounds are not permitted in trait object types
66

77
fn main() {}

tests/ui/traits/wf-object/only-maybe-bound.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: `?Trait` is not permitted in trait object types
1+
error[E0658]: relaxed bounds are not permitted in trait object types
22
--> $DIR/only-maybe-bound.rs:3:15
33
|
44
LL | type _0 = dyn ?Sized;

0 commit comments

Comments
 (0)