Skip to content

Commit 83bea88

Browse files
committed
Reword diagnostic for trying to relax non-Sized bound
* The phrasing "only does something for" made sense back when this diagnostic was a (hard) warning. Now however, it's simply a hard error and thus completely rules out. * The primary message was way too long * The new wording more closely mirrors the wording we use for applying other bound modifiers (like `const` and `async`) to incompatible traits. * "all other traits are not bound by default" is no longer accurate under Sized Hierarchy. E.g., traits and assoc tys are (currently) bounded by `MetaSized` by default but can't be relaxed using `?MetaSized` (instead, you relax it by adding `PointeeSized`). * I've decided against adding any diagnositic notes or suggestions for now like "trait `Trait` can't be relaxed as it's not bound by default" /which would be incorrect for `MetaSized` and assoc tys as mentioned above) or "consider changing `?MetaSized` to `PointeeSized`" as the Sized Hierarchy impl is still WIP)
1 parent 602523d commit 83bea88

21 files changed

+53
-60
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_errors::{
88
};
99
use rustc_hir::def::{CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::DefId;
11-
use rustc_hir::{self as hir, HirId, LangItem, PolyTraitRef};
11+
use rustc_hir::{self as hir, HirId, PolyTraitRef};
1212
use rustc_middle::bug;
1313
use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
1414
use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
@@ -41,7 +41,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
4141
) {
4242
let tcx = self.tcx();
4343

44-
let sized_did = tcx.require_lang_item(LangItem::Sized, DUMMY_SP);
44+
let sized_did = tcx.require_lang_item(hir::LangItem::Sized, DUMMY_SP);
4545

4646
let mut unique_bounds = FxIndexSet::default();
4747
let mut seen_repeat = false;
@@ -65,23 +65,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
6565

6666
for unbound in unbounds {
6767
if let Res::Def(DefKind::Trait, did) = unbound.trait_ref.path.res
68-
&& ((did == sized_did) || tcx.is_default_trait(did))
68+
&& (did == sized_did || tcx.is_default_trait(did))
6969
{
7070
continue;
7171
}
7272

73-
let unbound_traits = match tcx.sess.opts.unstable_opts.experimental_default_bounds {
74-
true => "`?Sized` and `experimental_default_bounds`",
75-
false => "`?Sized`",
73+
let message = match tcx.sess.opts.unstable_opts.experimental_default_bounds {
74+
true => "bound modifier `?` can only be applied to default traits like `Sized`",
75+
false => "bound modifier `?` can only be applied to `Sized`",
7676
};
77-
self.dcx().span_err(
78-
unbound.span,
79-
format!(
80-
"relaxing a default bound only does something for {}; all other traits are \
81-
not bound by default",
82-
unbound_traits
83-
),
84-
);
77+
let diag = self.dcx().struct_span_err(unbound.span, message);
8578
}
8679
}
8780

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ fn foo(_: Box<dyn Trait1 + ?Trait2>) {}
1111
//~^ ERROR `?Trait` is not permitted in trait object types
1212
fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
1313
//~^ ERROR type parameter has more than one relaxed default bound, only one is supported
14-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
15-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
14+
//~| ERROR bound modifier `?` can only be applied to `Sized`
15+
//~| ERROR bound modifier `?` can only be applied to `Sized`
1616

1717
trait Trait {}
1818
// Do not suggest `#![feature(more_maybe_bounds)]` for repetitions
1919
fn baz<T: ?Trait + ?Trait>(_ : T) {}
2020
//~^ ERROR type parameter has more than one relaxed default bound, only one is supported
21-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
22-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
21+
//~| ERROR bound modifier `?` can only be applied to `Sized`
22+
//~| ERROR bound modifier `?` can only be applied to `Sized`
2323

2424
fn main() {}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
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: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
37+
error: bound modifier `?` can only be applied to `Sized`
3838
--> $DIR/feature-gate-more-maybe-bounds.rs:12:11
3939
|
4040
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
4141
| ^^^^^^^
4242

43-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
43+
error: bound modifier `?` can only be applied to `Sized`
4444
--> $DIR/feature-gate-more-maybe-bounds.rs:12:21
4545
|
4646
LL | fn bar<T: ?Trait1 + ?Trait2>(_: T) {}
@@ -52,13 +52,13 @@ error[E0203]: type parameter has more than one relaxed default bound, only one i
5252
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
5353
| ^^^^^^ ^^^^^^
5454

55-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
55+
error: bound modifier `?` can only be applied to `Sized`
5656
--> $DIR/feature-gate-more-maybe-bounds.rs:19:11
5757
|
5858
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}
5959
| ^^^^^^
6060

61-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
61+
error: bound modifier `?` can only be applied to `Sized`
6262
--> $DIR/feature-gate-more-maybe-bounds.rs:19:20
6363
|
6464
LL | fn baz<T: ?Trait + ?Trait>(_ : T) {}

tests/ui/impl-trait/opt-out-bound-not-satisfied.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
44
use std::future::Future;
55
fn foo() -> impl ?Future<Output = impl Send> {
6-
//~^ ERROR: relaxing a default bound only does something for `?Sized`
7-
//~| ERROR: relaxing a default bound only does something for `?Sized`
6+
//~^ ERROR: bound modifier `?` can only be applied to `Sized`
7+
//~| ERROR: bound modifier `?` can only be applied to `Sized`
88
()
99
}
1010

tests/ui/impl-trait/opt-out-bound-not-satisfied.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
1+
error: bound modifier `?` can only be applied to `Sized`
22
--> $DIR/opt-out-bound-not-satisfied.rs:5:18
33
|
44
LL | fn foo() -> impl ?Future<Output = impl Send> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7+
error: bound modifier `?` can only be applied to `Sized`
88
--> $DIR/opt-out-bound-not-satisfied.rs:5:18
99
|
1010
LL | fn foo() -> impl ?Future<Output = impl Send> {

tests/ui/issues/issue-37534.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
struct Foo<T: ?Hash> {}
22
//~^ ERROR expected trait, found derive macro `Hash`
3-
//~| ERROR relaxing a default bound only does something for `?Sized`
3+
//~| ERROR bound modifier `?` can only be applied to `Sized`
44

55
fn main() {}

tests/ui/issues/issue-37534.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ help: consider importing this trait instead
99
LL + use std::hash::Hash;
1010
|
1111

12-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
12+
error: bound modifier `?` can only be applied to `Sized`
1313
--> $DIR/issue-37534.rs:1:15
1414
|
1515
LL | struct Foo<T: ?Hash> {}

tests/ui/issues/issue-87199.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
// Check that these function definitions only emit warnings, not errors
88
fn arg<T: ?Send>(_: T) {}
9-
//~^ ERROR: relaxing a default bound only does something for `?Sized`
9+
//~^ ERROR: bound modifier `?` can only be applied to `Sized`
1010
fn ref_arg<T: ?Send>(_: &T) {}
11-
//~^ ERROR: relaxing a default bound only does something for `?Sized`
11+
//~^ ERROR: bound modifier `?` can only be applied to `Sized`
1212
fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }
13-
//~^ ERROR: relaxing a default bound only does something for `?Sized`
14-
//~| ERROR: relaxing a default bound only does something for `?Sized`
13+
//~^ ERROR: bound modifier `?` can only be applied to `Sized`
14+
//~| ERROR: bound modifier `?` can only be applied to `Sized`
1515

1616
// Check that there's no `?Sized` relaxation!
1717
fn main() {

tests/ui/issues/issue-87199.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
1+
error: bound modifier `?` can only be applied to `Sized`
22
--> $DIR/issue-87199.rs:8:11
33
|
44
LL | fn arg<T: ?Send>(_: T) {}
55
| ^^^^^
66

7-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7+
error: bound modifier `?` can only be applied to `Sized`
88
--> $DIR/issue-87199.rs:10:15
99
|
1010
LL | fn ref_arg<T: ?Send>(_: &T) {}
1111
| ^^^^^
1212

13-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
13+
error: bound modifier `?` can only be applied to `Sized`
1414
--> $DIR/issue-87199.rs:12:40
1515
|
1616
LL | fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }
1717
| ^^^^^
1818

19-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
19+
error: bound modifier `?` can only be applied to `Sized`
2020
--> $DIR/issue-87199.rs:12:40
2121
|
2222
LL | fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ fn neg_sized<T: ?Sized>() {}
1414
fn metasized<T: MetaSized>() {}
1515

1616
fn neg_metasized<T: ?MetaSized>() {}
17-
//~^ ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
17+
//~^ ERROR bound modifier `?` can only be applied to `Sized`
1818

1919

2020
fn pointeesized<T: PointeeSized>() { }
2121

2222
fn neg_pointeesized<T: ?PointeeSized>() { }
23-
//~^ ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
23+
//~^ ERROR bound modifier `?` can only be applied to `Sized`
2424

2525

2626
fn main() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
1+
error: bound modifier `?` can only be applied to `Sized`
22
--> $DIR/default-bound.rs:16:21
33
|
44
LL | fn neg_metasized<T: ?MetaSized>() {}
55
| ^^^^^^^^^^
66

7-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7+
error: bound modifier `?` can only be applied to `Sized`
88
--> $DIR/default-bound.rs:22:24
99
|
1010
LL | fn neg_pointeesized<T: ?PointeeSized>() { }

tests/ui/trait-bounds/maybe-bound-has-path-args.rs

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

33
fn test<T: ?self::<i32>::Trait>() {}
44
//~^ ERROR type arguments are not allowed on module `maybe_bound_has_path_args`
5-
//~| ERROR relaxing a default bound only does something for `?Sized`
5+
//~| ERROR bound modifier `?` can only be applied to `Sized`
66

77
fn main() {}

tests/ui/trait-bounds/maybe-bound-has-path-args.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
1+
error: bound modifier `?` can only be applied to `Sized`
22
--> $DIR/maybe-bound-has-path-args.rs:3:12
33
|
44
LL | fn test<T: ?self::<i32>::Trait>() {}

tests/ui/trait-bounds/maybe-bound-with-assoc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ trait HasAssoc {
22
type Assoc;
33
}
44
fn hasassoc<T: ?HasAssoc<Assoc = ()>>() {}
5-
//~^ ERROR relaxing a default bound
5+
//~^ ERROR bound modifier `?` can only be applied to `Sized`
66

77
trait NoAssoc {}
88
fn noassoc<T: ?NoAssoc<Missing = ()>>() {}
9-
//~^ ERROR relaxing a default bound
9+
//~^ ERROR bound modifier `?` can only be applied to `Sized`
1010
//~| ERROR associated type `Missing` not found for `NoAssoc`
1111

1212
fn main() {}

tests/ui/trait-bounds/maybe-bound-with-assoc.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
1+
error: bound modifier `?` can only be applied to `Sized`
22
--> $DIR/maybe-bound-with-assoc.rs:4:16
33
|
44
LL | fn hasassoc<T: ?HasAssoc<Assoc = ()>>() {}
55
| ^^^^^^^^^^^^^^^^^^^^^
66

7-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7+
error: bound modifier `?` can only be applied to `Sized`
88
--> $DIR/maybe-bound-with-assoc.rs:8:15
99
|
1010
LL | fn noassoc<T: ?NoAssoc<Missing = ()>>() {}

tests/ui/traits/maybe-polarity-pass.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ trait Trait4 where Self: Trait1 {}
1010

1111
fn foo(_: Box<(dyn Trait3 + ?Trait2)>) {}
1212
fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
13-
//~^ ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
14-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
15-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
13+
//~^ ERROR bound modifier `?` can only be applied to `Sized`
14+
//~| ERROR bound modifier `?` can only be applied to `Sized`
15+
//~| ERROR bound modifier `?` can only be applied to `Sized`
1616

1717
struct S;
1818
impl !Trait2 for S {}

tests/ui/traits/maybe-polarity-pass.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
1+
error: bound modifier `?` can only be applied to `Sized`
22
--> $DIR/maybe-polarity-pass.rs:12:20
33
|
44
LL | fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
55
| ^^^^^^^
66

7-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7+
error: bound modifier `?` can only be applied to `Sized`
88
--> $DIR/maybe-polarity-pass.rs:12:30
99
|
1010
LL | fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}
1111
| ^^^^^^^
1212

13-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
13+
error: bound modifier `?` can only be applied to `Sized`
1414
--> $DIR/maybe-polarity-pass.rs:12:40
1515
|
1616
LL | fn bar<T: ?Sized + ?Trait2 + ?Trait1 + ?Trait4>(_: &T) {}

tests/ui/traits/maybe-polarity-repeated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
trait Trait {}
44
fn foo<T: ?Trait + ?Trait>(_: T) {}
55
//~^ ERROR type parameter has more than one relaxed default bound, only one is supported
6-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7-
//~| ERROR relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
6+
//~| ERROR bound modifier `?` can only be applied to `Sized`
7+
//~| ERROR bound modifier `?` can only be applied to `Sized`
88

99
fn main() {}

tests/ui/traits/maybe-polarity-repeated.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error[E0203]: type parameter has more than one relaxed default bound, only one i
44
LL | fn foo<T: ?Trait + ?Trait>(_: T) {}
55
| ^^^^^^ ^^^^^^
66

7-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
7+
error: bound modifier `?` can only be applied to `Sized`
88
--> $DIR/maybe-polarity-repeated.rs:4:11
99
|
1010
LL | fn foo<T: ?Trait + ?Trait>(_: T) {}
1111
| ^^^^^^
1212

13-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
13+
error: bound modifier `?` can only be applied to `Sized`
1414
--> $DIR/maybe-polarity-repeated.rs:4:20
1515
|
1616
LL | fn foo<T: ?Trait + ?Trait>(_: T) {}

tests/ui/unsized/maybe-bounds-where.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ trait Trait<'a> {}
1111

1212
struct S4<T>(T) where for<'a> T: ?Trait<'a>;
1313
//~^ ERROR `?Trait` bounds are only permitted at the point where a type parameter is declared
14-
//~| ERROR relaxing a default bound only does something for `?Sized`
14+
//~| ERROR bound modifier `?` can only be applied to `Sized`
1515

1616
struct S5<T>(*const T) where T: ?Trait<'static> + ?Sized;
1717
//~^ ERROR type parameter has more than one relaxed default bound
18-
//~| ERROR relaxing a default bound only does something for `?Sized`
18+
//~| ERROR bound modifier `?` can only be applied to `Sized`
1919

2020
impl<T> S1<T> {
2121
fn f() where T: ?Sized {}

tests/ui/unsized/maybe-bounds-where.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ LL | fn f() where T: ?Sized {}
4343
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
4444
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4545

46-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
46+
error: bound modifier `?` can only be applied to `Sized`
4747
--> $DIR/maybe-bounds-where.rs:12:34
4848
|
4949
LL | struct S4<T>(T) where for<'a> T: ?Trait<'a>;
@@ -58,7 +58,7 @@ LL | struct S5<T>(*const T) where T: ?Trait<'static> + ?Sized;
5858
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
5959
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
6060

61-
error: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
61+
error: bound modifier `?` can only be applied to `Sized`
6262
--> $DIR/maybe-bounds-where.rs:16:33
6363
|
6464
LL | struct S5<T>(*const T) where T: ?Trait<'static> + ?Sized;

0 commit comments

Comments
 (0)