Skip to content

Commit

Permalink
Rollup merge of rust-lang#116911 - estebank:issue-85378, r=oli-obk
Browse files Browse the repository at this point in the history
Suggest relaxing implicit `type Assoc: Sized;` bound

Fix rust-lang#85378.
  • Loading branch information
matthiaskrgr committed Oct 20, 2023
2 parents 96027d9 + e8d4fb8 commit 4d1cb02
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2665,6 +2665,29 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Check for foreign traits being reachable.
self.tcx.visible_parent_map(()).get(&def_id).is_some()
};
if Some(def_id) == self.tcx.lang_items().sized_trait()
&& let Some(hir::Node::TraitItem(hir::TraitItem {
ident,
kind: hir::TraitItemKind::Type(bounds, None),
..
})) = tcx.hir().get_if_local(item_def_id)
// Do not suggest relaxing if there is an explicit `Sized` obligation.
&& !bounds.iter()
.filter_map(|bound| bound.trait_ref())
.any(|tr| tr.trait_def_id() == self.tcx.lang_items().sized_trait())
{
let (span, separator) = if let [.., last] = bounds {
(last.span().shrink_to_hi(), " +")
} else {
(ident.span.shrink_to_hi(), ":")
};
err.span_suggestion_verbose(
span,
"consider relaxing the implicit `Sized` restriction",
format!("{separator} ?Sized"),
Applicability::MachineApplicable,
);
}
if let DefKind::Trait = tcx.def_kind(item_def_id)
&& !visible_item
{
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/object-safety/assoc_type_bounds_implicit_sized.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix
trait TraitWithAType {
type Item: ?Sized;
}
trait Trait {}
struct A {}
impl TraitWithAType for A {
type Item = dyn Trait; //~ ERROR E0277
}
fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/object-safety/assoc_type_bounds_implicit_sized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// run-rustfix
trait TraitWithAType {
type Item;
}
trait Trait {}
struct A {}
impl TraitWithAType for A {
type Item = dyn Trait; //~ ERROR E0277
}
fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/object-safety/assoc_type_bounds_implicit_sized.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
--> $DIR/assoc_type_bounds_implicit_sized.rs:8:17
|
LL | type Item = dyn Trait;
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`
note: required by a bound in `TraitWithAType::Item`
--> $DIR/assoc_type_bounds_implicit_sized.rs:3:5
|
LL | type Item;
| ^^^^^^^^^^ required by this bound in `TraitWithAType::Item`
help: consider relaxing the implicit `Sized` restriction
|
LL | type Item: ?Sized;
| ++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
4 changes: 4 additions & 0 deletions tests/ui/object-safety/assoc_type_bounds_sized_used.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
LL - fn bop<T: Bop + ?Sized>() {
LL + fn bop<T: Bop>() {
|
help: consider relaxing the implicit `Sized` restriction
|
LL | type Bar: Default + ?Sized
| ++++++++

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 4d1cb02

Please sign in to comment.