Skip to content

Commit 89a05c6

Browse files
committed
Make a distinction between public and sealed traits
1 parent 44f0b7f commit 89a05c6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

compiler/rustc_infer/src/traits/error_reporting/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,24 @@ pub fn report_object_safety_error<'tcx>(
123123
} else {
124124
vec![]
125125
};
126+
let externally_visible = if !impls.is_empty()
127+
&& let Some(def_id) = trait_def_id.as_local()
128+
&& tcx.effective_visibilities(()).is_exported(def_id)
129+
{
130+
true
131+
} else {
132+
false
133+
};
126134
match &impls[..] {
127135
[] => {}
128136
_ if impls.len() > 9 => {}
137+
[only] if externally_visible => {
138+
err.help(with_no_trimmed_paths!(format!(
139+
"only type `{}` is seen to implement the trait in this crate, consider using it \
140+
directly instead",
141+
tcx.type_of(*only).instantiate_identity(),
142+
)));
143+
}
129144
[only] => {
130145
err.help(with_no_trimmed_paths!(format!(
131146
"only type `{}` implements the trait, consider using it directly instead",
@@ -148,6 +163,12 @@ pub fn report_object_safety_error<'tcx>(
148163
));
149164
}
150165
}
166+
if externally_visible {
167+
err.note(format!(
168+
"`{trait_str}` can be implemented in other crates; if you want to support your users \
169+
passing their own types here, you can't refer to a specific type",
170+
));
171+
}
151172

152173
err
153174
}

tests/ui/generic-associated-types/issue-76535.base.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ LL | pub trait SuperTrait {
2828
LL | type SubType<'a>: SubTrait where Self: 'a;
2929
| ^^^^^^^ ...because it contains the generic associated type `SubType`
3030
= help: consider moving `SubType` to another trait
31-
= help: only type `SuperStruct` implements the trait, consider using it directly instead
31+
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
32+
= 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
3233

3334
error[E0038]: the trait `SuperTrait` cannot be made into an object
3435
--> $DIR/issue-76535.rs:39:57
@@ -44,7 +45,8 @@ LL | pub trait SuperTrait {
4445
LL | type SubType<'a>: SubTrait where Self: 'a;
4546
| ^^^^^^^ ...because it contains the generic associated type `SubType`
4647
= help: consider moving `SubType` to another trait
47-
= help: only type `SuperStruct` implements the trait, consider using it directly instead
48+
= help: only type `SuperStruct` is seen to implement the trait in this crate, consider using it directly instead
49+
= 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
4850
= note: required for the cast from `Box<SuperStruct>` to `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`
4951

5052
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)