Skip to content

Commit

Permalink
Make a distinction between public and sealed traits
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 27, 2023
1 parent 44f0b7f commit 89a05c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
21 changes: 21 additions & 0 deletions compiler/rustc_infer/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,24 @@ pub fn report_object_safety_error<'tcx>(
} else {
vec![]
};
let externally_visible = if !impls.is_empty()
&& let Some(def_id) = trait_def_id.as_local()
&& tcx.effective_visibilities(()).is_exported(def_id)
{
true
} else {
false
};
match &impls[..] {
[] => {}
_ if impls.len() > 9 => {}
[only] if externally_visible => {
err.help(with_no_trimmed_paths!(format!(
"only type `{}` is seen to implement the trait in this crate, consider using it \
directly instead",
tcx.type_of(*only).instantiate_identity(),
)));
}
[only] => {
err.help(with_no_trimmed_paths!(format!(
"only type `{}` implements the trait, consider using it directly instead",
Expand All @@ -148,6 +163,12 @@ pub fn report_object_safety_error<'tcx>(
));
}
}
if externally_visible {
err.note(format!(
"`{trait_str}` can be implemented in other crates; if you want to support your users \
passing their own types here, you can't refer to a specific type",
));
}

err
}
6 changes: 4 additions & 2 deletions tests/ui/generic-associated-types/issue-76535.base.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ LL | pub trait SuperTrait {
LL | type SubType<'a>: SubTrait where Self: 'a;
| ^^^^^^^ ...because it contains the generic associated type `SubType`
= help: consider moving `SubType` to another trait
= help: only type `SuperStruct` implements the trait, consider using it directly instead
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
= note: `SuperTrait` can be implemented in other crates; if you want to support your users passing their own types here, you can't refer to a specific type

error[E0038]: the trait `SuperTrait` cannot be made into an object
--> $DIR/issue-76535.rs:39:57
Expand All @@ -44,7 +45,8 @@ LL | pub trait SuperTrait {
LL | type SubType<'a>: SubTrait where Self: 'a;
| ^^^^^^^ ...because it contains the generic associated type `SubType`
= help: consider moving `SubType` to another trait
= help: only type `SuperStruct` implements the trait, consider using it directly instead
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
= note: `SuperTrait` can be implemented in other crates; if you want to support your users passing their own types here, you can't refer to a specific type
= note: required for the cast from `Box<SuperStruct>` to `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`

error: aborting due to 3 previous errors
Expand Down

0 comments on commit 89a05c6

Please sign in to comment.