Skip to content

Commit e890bd9

Browse files
authored
Unrolled build for rust-lang#120323
Rollup merge of rust-lang#120323 - estebank:issue-120178, r=fmease On E0277 be clearer about implicit `Sized` bounds on type params and assoc types ``` error[E0277]: the size for values of type `[i32]` cannot be known at compilation time --> f100.rs:2:33 | 2 | let _ = std::mem::size_of::<[i32]>(); | ^^^^^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `[i32]` note: required by an implicit `Sized` bound in `std::mem::size_of` --> /home/gh-estebank/rust/library/core/src/mem/mod.rs:312:22 | 312 | pub const fn size_of<T>() -> usize { | ^ required by the implicit `Sized` requirement on this bound in `size_of` ``` Fix rust-lang#120178.
2 parents 268dbbb + 0df6dfd commit e890bd9

File tree

51 files changed

+238
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+238
-229
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
2828
let tcx = self.tcx();
2929
let sized_def_id = tcx.lang_items().sized_trait();
3030
let mut seen_negative_sized_bound = false;
31+
let mut seen_positive_sized_bound = false;
3132

3233
// Try to find an unbound in bounds.
3334
let mut unbounds: SmallVec<[_; 1]> = SmallVec::new();
@@ -45,6 +46,13 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
4546
seen_negative_sized_bound = true;
4647
}
4748
}
49+
hir::TraitBoundModifier::None => {
50+
if let Some(sized_def_id) = sized_def_id
51+
&& ptr.trait_ref.path.res == Res::Def(DefKind::Trait, sized_def_id)
52+
{
53+
seen_positive_sized_bound = true;
54+
}
55+
}
4856
_ => {}
4957
}
5058
}
@@ -82,11 +90,11 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
8290
);
8391
}
8492

85-
if seen_sized_unbound || seen_negative_sized_bound {
86-
// There was in fact a `?Sized` or `!Sized` bound;
93+
if seen_sized_unbound || seen_negative_sized_bound || seen_positive_sized_bound {
94+
// There was in fact a `?Sized`, `!Sized` or explicit `Sized` bound;
8795
// we don't need to do anything.
8896
} else if sized_def_id.is_some() {
89-
// There was no `?Sized` or `!Sized` bound;
97+
// There was no `?Sized`, `!Sized` or explicit `Sized` bound;
9098
// add `Sized` if it's available.
9199
bounds.push_sized(tcx, self_ty, span);
92100
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+90-69
Original file line numberDiff line numberDiff line change
@@ -3009,35 +3009,44 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30093009
);
30103010
}
30113011
}
3012-
let descr = format!("required by a bound in `{item_name}`");
3013-
if span.is_visible(sm) {
3014-
let msg = format!("required by this bound in `{short_item_name}`");
3015-
multispan.push_span_label(span, msg);
3016-
err.span_note(multispan, descr);
3017-
if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder()
3018-
&& let ty::ClauseKind::Trait(trait_pred) = clause
3019-
{
3020-
let def_id = trait_pred.def_id();
3021-
let visible_item = if let Some(local) = def_id.as_local() {
3022-
// Check for local traits being reachable.
3023-
let vis = &tcx.resolutions(()).effective_visibilities;
3024-
// Account for non-`pub` traits in the root of the local crate.
3025-
let is_locally_reachable = tcx.parent(def_id).is_crate_root();
3026-
vis.is_reachable(local) || is_locally_reachable
3027-
} else {
3028-
// Check for foreign traits being reachable.
3029-
tcx.visible_parent_map(()).get(&def_id).is_some()
3030-
};
3031-
if Some(def_id) == tcx.lang_items().sized_trait()
3032-
&& let Some(hir::Node::TraitItem(hir::TraitItem {
3033-
ident,
3034-
kind: hir::TraitItemKind::Type(bounds, None),
3035-
..
3036-
})) = tcx.hir().get_if_local(item_def_id)
3037-
// Do not suggest relaxing if there is an explicit `Sized` obligation.
3038-
&& !bounds.iter()
3039-
.filter_map(|bound| bound.trait_ref())
3040-
.any(|tr| tr.trait_def_id() == tcx.lang_items().sized_trait())
3012+
let mut a = "a";
3013+
let mut this = "this bound";
3014+
let mut note = None;
3015+
let mut help = None;
3016+
if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder()
3017+
&& let ty::ClauseKind::Trait(trait_pred) = clause
3018+
{
3019+
let def_id = trait_pred.def_id();
3020+
let visible_item = if let Some(local) = def_id.as_local() {
3021+
// Check for local traits being reachable.
3022+
let vis = &tcx.resolutions(()).effective_visibilities;
3023+
// Account for non-`pub` traits in the root of the local crate.
3024+
let is_locally_reachable = tcx.parent(def_id).is_crate_root();
3025+
vis.is_reachable(local) || is_locally_reachable
3026+
} else {
3027+
// Check for foreign traits being reachable.
3028+
tcx.visible_parent_map(()).get(&def_id).is_some()
3029+
};
3030+
if Some(def_id) == tcx.lang_items().sized_trait() {
3031+
// Check if this is an implicit bound, even in foreign crates.
3032+
if tcx
3033+
.generics_of(item_def_id)
3034+
.params
3035+
.iter()
3036+
.any(|param| tcx.def_span(param.def_id) == span)
3037+
{
3038+
a = "an implicit `Sized`";
3039+
this = "the implicit `Sized` requirement on this type parameter";
3040+
}
3041+
if let Some(hir::Node::TraitItem(hir::TraitItem {
3042+
ident,
3043+
kind: hir::TraitItemKind::Type(bounds, None),
3044+
..
3045+
})) = tcx.hir().get_if_local(item_def_id)
3046+
// Do not suggest relaxing if there is an explicit `Sized` obligation.
3047+
&& !bounds.iter()
3048+
.filter_map(|bound| bound.trait_ref())
3049+
.any(|tr| tr.trait_def_id() == tcx.lang_items().sized_trait())
30413050
{
30423051
let (span, separator) = if let [.., last] = bounds {
30433052
(last.span().shrink_to_hi(), " +")
@@ -3051,52 +3060,64 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
30513060
Applicability::MachineApplicable,
30523061
);
30533062
}
3054-
if let DefKind::Trait = tcx.def_kind(item_def_id)
3055-
&& !visible_item
3056-
{
3057-
err.note(format!(
3058-
"`{short_item_name}` is a \"sealed trait\", because to implement \
3059-
it you also need to implement `{}`, which is not accessible; \
3060-
this is usually done to force you to use one of the provided \
3061-
types that already implement it",
3062-
with_no_trimmed_paths!(tcx.def_path_str(def_id)),
3063-
));
3064-
let impls_of = tcx.trait_impls_of(def_id);
3065-
let impls = impls_of
3066-
.non_blanket_impls()
3067-
.values()
3068-
.flatten()
3069-
.chain(impls_of.blanket_impls().iter())
3063+
}
3064+
if let DefKind::Trait = tcx.def_kind(item_def_id)
3065+
&& !visible_item
3066+
{
3067+
note = Some(format!(
3068+
"`{short_item_name}` is a \"sealed trait\", because to implement it \
3069+
you also need to implement `{}`, which is not accessible; this is \
3070+
usually done to force you to use one of the provided types that \
3071+
already implement it",
3072+
with_no_trimmed_paths!(tcx.def_path_str(def_id)),
3073+
));
3074+
let impls_of = tcx.trait_impls_of(def_id);
3075+
let impls = impls_of
3076+
.non_blanket_impls()
3077+
.values()
3078+
.flatten()
3079+
.chain(impls_of.blanket_impls().iter())
3080+
.collect::<Vec<_>>();
3081+
if !impls.is_empty() {
3082+
let len = impls.len();
3083+
let mut types = impls
3084+
.iter()
3085+
.map(|t| {
3086+
with_no_trimmed_paths!(format!(
3087+
" {}",
3088+
tcx.type_of(*t).instantiate_identity(),
3089+
))
3090+
})
30703091
.collect::<Vec<_>>();
3071-
if !impls.is_empty() {
3072-
let len = impls.len();
3073-
let mut types = impls
3074-
.iter()
3075-
.map(|t| {
3076-
with_no_trimmed_paths!(format!(
3077-
" {}",
3078-
tcx.type_of(*t).instantiate_identity(),
3079-
))
3080-
})
3081-
.collect::<Vec<_>>();
3082-
let post = if types.len() > 9 {
3083-
types.truncate(8);
3084-
format!("\nand {} others", len - 8)
3085-
} else {
3086-
String::new()
3087-
};
3088-
err.help(format!(
3089-
"the following type{} implement{} the trait:\n{}{post}",
3090-
pluralize!(len),
3091-
if len == 1 { "s" } else { "" },
3092-
types.join("\n"),
3093-
));
3094-
}
3092+
let post = if types.len() > 9 {
3093+
types.truncate(8);
3094+
format!("\nand {} others", len - 8)
3095+
} else {
3096+
String::new()
3097+
};
3098+
help = Some(format!(
3099+
"the following type{} implement{} the trait:\n{}{post}",
3100+
pluralize!(len),
3101+
if len == 1 { "s" } else { "" },
3102+
types.join("\n"),
3103+
));
30953104
}
30963105
}
3106+
};
3107+
let descr = format!("required by {a} bound in `{item_name}`");
3108+
if span.is_visible(sm) {
3109+
let msg = format!("required by {this} in `{short_item_name}`");
3110+
multispan.push_span_label(span, msg);
3111+
err.span_note(multispan, descr);
30973112
} else {
30983113
err.span_note(tcx.def_span(item_def_id), descr);
30993114
}
3115+
if let Some(note) = note {
3116+
err.note(note);
3117+
}
3118+
if let Some(help) = help {
3119+
err.help(help);
3120+
}
31003121
}
31013122
ObligationCauseCode::Coercion { source, target } => {
31023123
let mut file = None;

tests/incremental/hashes/trait_defs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,10 @@ trait TraitAddBuiltinBoundToMethodTypeParameter {
559559
#[cfg(not(any(cfail1,cfail4)))]
560560
#[rustc_clean(cfg="cfail2")]
561561
#[rustc_clean(cfg="cfail3")]
562-
#[rustc_clean(cfg="cfail5")]
562+
#[rustc_clean(except="opt_hir_owner_nodes", cfg="cfail5")]
563563
#[rustc_clean(cfg="cfail6")]
564564
trait TraitAddBuiltinBoundToMethodTypeParameter {
565-
#[rustc_clean(except="opt_hir_owner_nodes,predicates_of", cfg="cfail2")]
565+
#[rustc_clean(except="opt_hir_owner_nodes", cfg="cfail2")]
566566
#[rustc_clean(cfg="cfail3")]
567567
#[rustc_clean(except="opt_hir_owner_nodes,predicates_of", cfg="cfail5")]
568568
#[rustc_clean(cfg="cfail6")]

tests/ui/associated-types/defaults-wf.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | type Ty = Vec<[u8]>;
55
| ^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u8]`
8-
note: required by a bound in `Vec`
8+
note: required by an implicit `Sized` bound in `Vec`
99
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1010

1111
error: aborting due to 1 previous error

tests/ui/associated-types/issue-20005.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
44
LL | ) -> <Dst as From<Self>>::Result where Dst: From<Self> {
55
| ^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
note: required by a bound in `From`
7+
note: required by an implicit `Sized` bound in `From`
88
--> $DIR/issue-20005.rs:1:12
99
|
1010
LL | trait From<Src> {
11-
| ^^^ required by this bound in `From`
11+
| ^^^ required by the implicit `Sized` requirement on this type parameter in `From`
1212
help: consider further restricting `Self`
1313
|
1414
LL | ) -> <Dst as From<Self>>::Result where Dst: From<Self>, Self: Sized {

tests/ui/associated-types/trait-with-supertraits-needing-sized-self.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
44
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> {}
55
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
note: required by a bound in `Add`
7+
note: required by an implicit `Sized` bound in `Add`
88
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
99
help: consider further restricting `Self`
1010
|

tests/ui/closures/issue-111932.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | println!("{:?}", foo);
1717
| required by a bound introduced by this call
1818
|
1919
= help: the trait `Sized` is not implemented for `dyn Foo`
20-
note: required by a bound in `core::fmt::rt::Argument::<'a>::new_debug`
20+
note: required by an implicit `Sized` bound in `core::fmt::rt::Argument::<'a>::new_debug`
2121
--> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
2222
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
2323

tests/ui/coroutine/sized-yield.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | Pin::new(&mut gen).resume(());
1818
| ^^^^^^ doesn't have a size known at compile-time
1919
|
2020
= help: the trait `Sized` is not implemented for `str`
21-
note: required by a bound in `CoroutineState`
21+
note: required by an implicit `Sized` bound in `CoroutineState`
2222
--> $SRC_DIR/core/src/ops/coroutine.rs:LL:COL
2323

2424
error: aborting due to 2 previous errors

tests/ui/dst/dst-sized-trait-param.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ LL | impl Foo<[isize]> for usize { }
55
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[isize]`
8-
note: required by a bound in `Foo`
8+
note: required by an implicit `Sized` bound in `Foo`
99
--> $DIR/dst-sized-trait-param.rs:5:11
1010
|
1111
LL | trait Foo<T> : Sized { fn take(self, x: &T) { } } // Note: T is sized
12-
| ^ required by this bound in `Foo`
12+
| ^ required by the implicit `Sized` requirement on this type parameter in `Foo`
1313
help: consider relaxing the implicit `Sized` restriction
1414
|
1515
LL | trait Foo<T: ?Sized> : Sized { fn take(self, x: &T) { } } // Note: T is sized

tests/ui/extern/extern-types-unsized.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ LL | assert_sized::<A>();
55
| ^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `A`
8-
note: required by a bound in `assert_sized`
8+
note: required by an implicit `Sized` bound in `assert_sized`
99
--> $DIR/extern-types-unsized.rs:19:17
1010
|
1111
LL | fn assert_sized<T>() {}
12-
| ^ required by this bound in `assert_sized`
12+
| ^ required by the implicit `Sized` requirement on this type parameter in `assert_sized`
1313
help: consider relaxing the implicit `Sized` restriction
1414
|
1515
LL | fn assert_sized<T: ?Sized>() {}
@@ -27,11 +27,11 @@ note: required because it appears within the type `Foo`
2727
|
2828
LL | struct Foo {
2929
| ^^^
30-
note: required by a bound in `assert_sized`
30+
note: required by an implicit `Sized` bound in `assert_sized`
3131
--> $DIR/extern-types-unsized.rs:19:17
3232
|
3333
LL | fn assert_sized<T>() {}
34-
| ^ required by this bound in `assert_sized`
34+
| ^ required by the implicit `Sized` requirement on this type parameter in `assert_sized`
3535
help: consider relaxing the implicit `Sized` restriction
3636
|
3737
LL | fn assert_sized<T: ?Sized>() {}
@@ -49,11 +49,11 @@ note: required because it appears within the type `Bar<A>`
4949
|
5050
LL | struct Bar<T: ?Sized> {
5151
| ^^^
52-
note: required by a bound in `assert_sized`
52+
note: required by an implicit `Sized` bound in `assert_sized`
5353
--> $DIR/extern-types-unsized.rs:19:17
5454
|
5555
LL | fn assert_sized<T>() {}
56-
| ^ required by this bound in `assert_sized`
56+
| ^ required by the implicit `Sized` requirement on this type parameter in `assert_sized`
5757
help: consider relaxing the implicit `Sized` restriction
5858
|
5959
LL | fn assert_sized<T: ?Sized>() {}
@@ -71,11 +71,11 @@ note: required because it appears within the type `Bar<A>`
7171
|
7272
LL | struct Bar<T: ?Sized> {
7373
| ^^^
74-
note: required by a bound in `assert_sized`
74+
note: required by an implicit `Sized` bound in `assert_sized`
7575
--> $DIR/extern-types-unsized.rs:19:17
7676
|
7777
LL | fn assert_sized<T>() {}
78-
| ^ required by this bound in `assert_sized`
78+
| ^ required by the implicit `Sized` requirement on this type parameter in `assert_sized`
7979
help: consider relaxing the implicit `Sized` restriction
8080
|
8181
LL | fn assert_sized<T: ?Sized>() {}

tests/ui/generic-associated-types/issue-88287.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | type SearchFutureTy<'f, A, B: 'f>
77
LL | async move { todo!() }
88
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
99
|
10-
note: required by a bound in `<T as SearchableResourceExt<Criteria>>`
10+
note: required by an implicit `Sized` bound in `<T as SearchableResourceExt<Criteria>>`
1111
--> $DIR/issue-88287.rs:24:6
1212
|
1313
LL | impl<T, Criteria> SearchableResourceExt<Criteria> for T
14-
| ^ required by this bound in `<T as SearchableResourceExt<Criteria>>`
14+
| ^ required by the implicit `Sized` requirement on this type parameter in `<T as SearchableResourceExt<Criteria>>`
1515
help: consider removing the `?Sized` bound to make the type parameter `Sized`
1616
|
1717
LL - A: SearchableResource<B> + ?Sized + 'f,

tests/ui/generics/issue-61631-default-type-param-can-reference-self-in-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ LL | impl Tsized for () {}
66
|
77
= help: the trait `Sized` is not implemented for `[()]`
88
note: required by a bound in `Tsized`
9-
--> $DIR/issue-61631-default-type-param-can-reference-self-in-trait.rs:17:14
9+
--> $DIR/issue-61631-default-type-param-can-reference-self-in-trait.rs:17:17
1010
|
1111
LL | trait Tsized<P: Sized = [Self]> {}
12-
| ^^^^^^^^^^^^^^^^^ required by this bound in `Tsized`
12+
| ^^^^^ required by this bound in `Tsized`
1313

1414
error: aborting due to 1 previous error
1515

0 commit comments

Comments
 (0)