Skip to content

Commit 84ed70b

Browse files
committed
Reword diagnostics about relaxed bounds in invalid contexts
1 parent 1df99f2 commit 84ed70b

19 files changed

+125
-240
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use rustc_index::{Idx, IndexSlice, IndexVec};
6060
use rustc_macros::extension;
6161
use rustc_middle::span_bug;
6262
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
63-
use rustc_session::parse::{add_feature_diagnostics, feature_err};
63+
use rustc_session::parse::add_feature_diagnostics;
6464
use rustc_span::symbol::{Ident, Symbol, kw, sym};
6565
use rustc_span::{DUMMY_SP, DesugaringKind, Span};
6666
use smallvec::SmallVec;
@@ -2071,7 +2071,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20712071
span: Span,
20722072
rbp: RelaxedBoundPolicy<'_>,
20732073
) {
2074-
let err = |message| feature_err(&self.tcx.sess, sym::more_maybe_bounds, span, message);
2074+
// Even though feature `more_maybe_bounds` bypasses the given policy and (currently) enables
2075+
// relaxed bounds in every conceivable position[^1], we don't want to advertise it to the user
2076+
// (via a feature gate) since it's super internal. Besides this, it'd be quite distracting.
2077+
//
2078+
// [^1]: Strictly speaking, this is incorrect (at the very least for `Sized`) because it's
2079+
// no longer fully consistent with default trait elaboration in HIR ty lowering.
20752080

20762081
match rbp {
20772082
RelaxedBoundPolicy::Allowed => return,
@@ -2093,11 +2098,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20932098

20942099
match reason {
20952100
RelaxedBoundForbiddenReason::TraitObjectTy => {
2096-
err("`?Trait` is not permitted in trait object types").emit();
2101+
self.dcx().span_err(
2102+
span,
2103+
"relaxed bounds are not permitted in trait object types",
2104+
);
20972105
return;
20982106
}
20992107
RelaxedBoundForbiddenReason::SuperTrait => {
2100-
let mut diag = err("`?Trait` is not permitted in supertraits");
2108+
let mut diag = self.dcx().struct_span_err(
2109+
span,
2110+
"relaxed bounds are not permitted in supertrait bounds",
2111+
);
21012112
if let Some(def_id) = trait_ref.trait_def_id()
21022113
&& self.tcx.is_lang_item(def_id, hir::LangItem::Sized)
21032114
{
@@ -2111,7 +2122,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21112122
}
21122123
}
21132124

2114-
err("`?Trait` bounds are only permitted at the point where a type parameter is declared")
2125+
self.dcx()
2126+
.struct_span_err(span, "this relaxed bound is not permitted here")
2127+
.with_note(
2128+
"in this context, relaxed bounds are only allowed on \
2129+
type parameters defined by the closest item",
2130+
)
21152131
.emit();
21162132
}
21172133

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: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ 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: 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) {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
|
13-
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
14-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1512

1613
error: aborting due to 2 previous errors
1714

18-
Some errors have detailed explanations: E0404, E0658.
19-
For more information about an error, try `rustc --explain E0404`.
15+
For more information about this error, try `rustc --explain E0404`.

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: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ 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: 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 = ()>;
1111
| ^^^^^^^^^^^^^^^^
12-
|
13-
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
14-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1512

1613
error: aborting due to 2 previous errors
1714

18-
Some errors have detailed explanations: E0405, E0658.
19-
For more information about an error, try `rustc --explain E0405`.
15+
For more information about this error, try `rustc --explain E0405`.

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

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
7+
error: this relaxed bound is not permitted here
8+
--> $DIR/feature-gate-more-maybe-bounds.rs:6:26
129
|
1310
LL | trait Trait4 where Self: ?Trait1 {}
1411
| ^^^^^^^
1512
|
16-
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
17-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
13+
= note: in this context, relaxed bounds are only allowed on type parameters defined by the closest item
1814

19-
error[E0658]: `?Trait` is not permitted in trait object types
20-
--> $DIR/feature-gate-more-maybe-bounds.rs:10:28
15+
error: relaxed bounds are not permitted in trait object types
16+
--> $DIR/feature-gate-more-maybe-bounds.rs:8:28
2117
|
2218
LL | fn foo(_: Box<dyn Trait1 + ?Trait2>) {}
2319
| ^^^^^^^
24-
|
25-
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
26-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2720

2821
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
22+
--> $DIR/feature-gate-more-maybe-bounds.rs:10:11
3023
|
3124
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
3225
| ^^^^^^^ ^^^^^^^
@@ -35,36 +28,35 @@ LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
3528
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3629

3730
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
31+
--> $DIR/feature-gate-more-maybe-bounds.rs:10:11
3932
|
4033
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
4134
| ^^^^^^^
4235

4336
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
37+
--> $DIR/feature-gate-more-maybe-bounds.rs:10:21
4538
|
4639
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
4740
| ^^^^^^^
4841

4942
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
43+
--> $DIR/feature-gate-more-maybe-bounds.rs:17:11
5144
|
5245
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
5346
| ^^^^^^ ^^^^^^
5447

5548
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
49+
--> $DIR/feature-gate-more-maybe-bounds.rs:17:11
5750
|
5851
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
5952
| ^^^^^^
6053

6154
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
55+
--> $DIR/feature-gate-more-maybe-bounds.rs:17:20
6356
|
6457
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
6558
| ^^^^^^
6659

6760
error: aborting due to 9 previous errors
6861

69-
Some errors have detailed explanations: E0203, E0658.
70-
For more information about an error, try `rustc --explain E0203`.
62+
For more information about this error, try `rustc --explain E0203`.

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: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
1-
error[E0658]: `?Trait` is not permitted in trait object types
1+
error: 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>)>;
55
| ^^^^^^^^
6-
|
7-
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
8-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
96

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

19-
error[E0658]: `?Trait` is not permitted in trait object types
13+
error: relaxed bounds are not permitted in trait object types
2014
--> $DIR/trait-object-trait-parens.rs:18:44
2115
|
2216
LL | let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;
2317
| ^^^^^^^^
24-
|
25-
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
26-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2718

2819
warning: trait objects without an explicit `dyn` are deprecated
2920
--> $DIR/trait-object-trait-parens.rs:8:16
@@ -100,5 +91,4 @@ LL | let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;
10091

10192
error: aborting due to 6 previous errors; 3 warnings emitted
10293

103-
Some errors have detailed explanations: E0225, E0658.
104-
For more information about an error, try `rustc --explain E0225`.
94+
For more information about this error, try `rustc --explain E0225`.

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

0 commit comments

Comments
 (0)