Skip to content

Commit 26f1522

Browse files
authored
Unrolled build for rust-lang#117411
Rollup merge of rust-lang#117411 - oli-obk:query_merge_immobile_game, r=compiler-errors,Nilstrieb Improve some diagnostics around `?Trait` bounds * uses better spans * clarifies a message that was only talking about generic params, but applies to `dyn ?Trait` and `impl ?Trait` as well
2 parents 236ac91 + 455cf5a commit 26f1522

File tree

8 files changed

+63
-58
lines changed

8 files changed

+63
-58
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_middle::ty::{self as ty, Ty, TypeVisitableExt};
88
use rustc_span::symbol::Ident;
99
use rustc_span::{ErrorGuaranteed, Span};
1010
use rustc_trait_selection::traits;
11+
use smallvec::SmallVec;
1112

1213
use crate::astconv::{
1314
AstConv, ConvertedBinding, ConvertedBindingKind, OnlySelfBounds, PredicateFilter,
@@ -28,15 +29,11 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
2829
let tcx = self.tcx();
2930

3031
// Try to find an unbound in bounds.
31-
let mut unbound = None;
32+
let mut unbounds: SmallVec<[_; 1]> = SmallVec::new();
3233
let mut search_bounds = |ast_bounds: &'tcx [hir::GenericBound<'tcx>]| {
3334
for ab in ast_bounds {
3435
if let hir::GenericBound::Trait(ptr, hir::TraitBoundModifier::Maybe) = ab {
35-
if unbound.is_none() {
36-
unbound = Some(&ptr.trait_ref);
37-
} else {
38-
tcx.sess.emit_err(errors::MultipleRelaxedDefaultBounds { span });
39-
}
36+
unbounds.push(ptr)
4037
}
4138
}
4239
};
@@ -51,33 +48,41 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
5148
}
5249
}
5350

51+
if unbounds.len() > 1 {
52+
tcx.sess.emit_err(errors::MultipleRelaxedDefaultBounds {
53+
spans: unbounds.iter().map(|ptr| ptr.span).collect(),
54+
});
55+
}
56+
5457
let sized_def_id = tcx.lang_items().sized_trait();
55-
match (&sized_def_id, unbound) {
56-
(Some(sized_def_id), Some(tpb))
57-
if tpb.path.res == Res::Def(DefKind::Trait, *sized_def_id) =>
58-
{
59-
// There was in fact a `?Sized` bound, return without doing anything
60-
return;
61-
}
62-
(_, Some(_)) => {
63-
// There was a `?Trait` bound, but it was not `?Sized`; warn.
64-
tcx.sess.span_warn(
65-
span,
66-
"default bound relaxed for a type parameter, but \
67-
this does nothing because the given bound is not \
68-
a default; only `?Sized` is supported",
69-
);
70-
// Otherwise, add implicitly sized if `Sized` is available.
71-
}
72-
_ => {
73-
// There was no `?Sized` bound; add implicitly sized if `Sized` is available.
58+
59+
let mut seen_sized_unbound = false;
60+
for unbound in unbounds {
61+
if let Some(sized_def_id) = sized_def_id {
62+
if unbound.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id) {
63+
seen_sized_unbound = true;
64+
continue;
65+
}
7466
}
67+
// There was a `?Trait` bound, but it was not `?Sized`; warn.
68+
tcx.sess.span_warn(
69+
unbound.span,
70+
"relaxing a default bound only does something for `?Sized`; \
71+
all other traits are not bound by default",
72+
);
7573
}
74+
75+
// If the above loop finished there was no `?Sized` bound; add implicitly sized if `Sized` is available.
7676
if sized_def_id.is_none() {
7777
// No lang item for `Sized`, so we can't add it as a bound.
7878
return;
7979
}
80-
bounds.push_sized(tcx, self_ty, span);
80+
if seen_sized_unbound {
81+
// There was in fact a `?Sized` bound, return without doing anything
82+
} else {
83+
// There was no `?Sized` bound; add implicitly sized if `Sized` is available.
84+
bounds.push_sized(tcx, self_ty, span);
85+
}
8186
}
8287

8388
/// This helper takes a *converted* parameter type (`param_ty`)

compiler/rustc_hir_analysis/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct CopyImplOnTypeWithDtor {
9696
#[diag(hir_analysis_multiple_relaxed_default_bounds, code = "E0203")]
9797
pub struct MultipleRelaxedDefaultBounds {
9898
#[primary_span]
99-
pub span: Span,
99+
pub spans: Vec<Span>,
100100
}
101101

102102
#[derive(Diagnostic)]

tests/ui/issues/issue-37534.rs

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

6-
fn main() { }
6+
fn main() {}

tests/ui/issues/issue-37534.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
error[E0404]: expected trait, found derive macro `Hash`
22
--> $DIR/issue-37534.rs:1:16
33
|
4-
LL | struct Foo<T: ?Hash> { }
4+
LL | struct Foo<T: ?Hash> {}
55
| ^^^^ not a trait
66
|
77
help: consider importing this trait instead
88
|
99
LL + use std::hash::Hash;
1010
|
1111

12-
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
13-
--> $DIR/issue-37534.rs:1:12
12+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
13+
--> $DIR/issue-37534.rs:1:15
1414
|
15-
LL | struct Foo<T: ?Hash> { }
16-
| ^
15+
LL | struct Foo<T: ?Hash> {}
16+
| ^^^^^
1717

1818
error[E0392]: parameter `T` is never used
1919
--> $DIR/issue-37534.rs:1:12
2020
|
21-
LL | struct Foo<T: ?Hash> { }
21+
LL | struct Foo<T: ?Hash> {}
2222
| ^ unused parameter
2323
|
2424
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`

tests/ui/issues/issue-87199.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
// Check that these function definitions only emit warnings, not errors
88
fn arg<T: ?Send>(_: T) {}
9-
//~^ warning: default bound relaxed for a type parameter, but this does nothing
9+
//~^ warning: relaxing a default bound only does something for `?Sized`
1010
fn ref_arg<T: ?Send>(_: &T) {}
11-
//~^ warning: default bound relaxed for a type parameter, but this does nothing
11+
//~^ warning: relaxing a default bound only does something for `?Sized`
1212
fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }
13-
//~^ warning: default bound relaxed for a type parameter, but this does nothing
13+
//~^ warning: relaxing a default bound only does something for `?Sized`
1414

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

tests/ui/issues/issue-87199.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
2-
--> $DIR/issue-87199.rs:8:8
1+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
2+
--> $DIR/issue-87199.rs:8:11
33
|
44
LL | fn arg<T: ?Send>(_: T) {}
5-
| ^
5+
| ^^^^^
66

7-
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
8-
--> $DIR/issue-87199.rs:10:12
7+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
8+
--> $DIR/issue-87199.rs:10:15
99
|
1010
LL | fn ref_arg<T: ?Send>(_: &T) {}
11-
| ^
11+
| ^^^^^
1212

13-
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
14-
--> $DIR/issue-87199.rs:12:13
13+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
14+
--> $DIR/issue-87199.rs:12:40
1515
|
1616
LL | fn ret() -> impl Iterator<Item = ()> + ?Send { std::iter::empty() }
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
| ^^^^^
1818

1919
error[E0277]: the size for values of type `[i32]` cannot be known at compilation time
2020
--> $DIR/issue-87199.rs:18:15

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

+2-2
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-
//~| WARN default bound relaxed for a type parameter
14+
//~| WARN relaxing a default bound only does something for `?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-
//~| WARN default bound relaxed for a type parameter
18+
//~| WARN relaxing a default bound only does something for `?Sized`
1919

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

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@ error: `?Trait` bounds are only permitted at the point where a type parameter is
2828
LL | fn f() where T: ?Sized {}
2929
| ^^^^^^
3030

31-
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
32-
--> $DIR/maybe-bounds-where.rs:12:11
31+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
32+
--> $DIR/maybe-bounds-where.rs:12:34
3333
|
3434
LL | struct S4<T>(T) where for<'a> T: ?Trait<'a>;
35-
| ^
35+
| ^^^^^^^^^^
3636

3737
error[E0203]: type parameter has more than one relaxed default bound, only one is supported
38-
--> $DIR/maybe-bounds-where.rs:16:11
38+
--> $DIR/maybe-bounds-where.rs:16:33
3939
|
4040
LL | struct S5<T>(*const T) where T: ?Trait<'static> + ?Sized;
41-
| ^
41+
| ^^^^^^^^^^^^^^^ ^^^^^^
4242

43-
warning: default bound relaxed for a type parameter, but this does nothing because the given bound is not a default; only `?Sized` is supported
44-
--> $DIR/maybe-bounds-where.rs:16:11
43+
warning: relaxing a default bound only does something for `?Sized`; all other traits are not bound by default
44+
--> $DIR/maybe-bounds-where.rs:16:33
4545
|
4646
LL | struct S5<T>(*const T) where T: ?Trait<'static> + ?Sized;
47-
| ^
47+
| ^^^^^^^^^^^^^^^
4848

4949
error: aborting due to 6 previous errors; 2 warnings emitted
5050

0 commit comments

Comments
 (0)