Add note when inherent impl for a alias type defined outside of the crate#142415
Add note when inherent impl for a alias type defined outside of the crate#142415xizheyin wants to merge 2 commits intorust-lang:mainfrom
Conversation
tests/ui/incoherent-inherent-impls/insufficient-suggestion-issue-141679.stderr
Show resolved
Hide resolved
tests/ui/incoherent-inherent-impls/insufficient-suggestion-issue-141679.stderr
Show resolved
Hide resolved
|
r? compiler |
| .note = define and implement a trait or new type instead | ||
| .help = consider defining a trait and implementing it for the type or using a newtype wrapper like `struct MyType(ExternalType);` and implement it | ||
| .note = For more details about orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules> | ||
|
|
||
| hir_analysis_inherent_ty_outside_new_alias_note = `{$ty_name}` does not define a new type, just alias of `{$alias_ty_name}` defined here |
There was a problem hiding this comment.
It seems difficult to apply structured suggestions here, because not only do we have to implement a new type, we also need to implement it with impl. Maybe we can use a clearer explanation:
help: consider defining a trait and implementing it for the type or using a newtype wrapper like `struct MyType(ExternalType);` and implement it.
Note: For more details about orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules>
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
|
do you not get TyKind::Alias when you use type_of on the impl? coherence is probably fine to use middle queries |
…rate Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
There was a problem hiding this comment.
I've tried using type_of before, but I don't seem to be getting the information I'm looking for. When I try this code
let ty = self.tcx.type_of(impl_def_id).skip_binder();
if let ty::Adt(def, _) = ty.kind() {
let def_id = def.did();
let ty_name = self.tcx.def_path_str(def_id);
let alias_ty_name = self.tcx.type_of(def_id).skip_binder().to_string();
println!("ty_name: {:?}", ty_name);
println!("alias_ty_name: {:?}", alias_ty_name);
}
it prints:
ty_name: "Rc"
alias_ty_name: "Rc<T, A>"
It should be:
ty_name: "Function"
alias_ty_name: "Rc<Foo>"
Local variable ty is a adt of type Rc. Maybe the information we need only exists in HIR? Or am I using it wrong?
| if let hir::TyKind::Path(rustc_hir::QPath::Resolved(_, path)) = | ||
| self.tcx.hir_node_by_def_id(impl_def_id).expect_item().expect_impl().self_ty.kind | ||
| && let rustc_hir::def::Res::Def(DefKind::TyAlias, def_id) = path.res |
|
r? compiler |
|
Only under the experimental feature |
| .help = consider defining a trait and implementing it for the type or using a newtype wrapper like `struct MyType(ExternalType);` and implement it | ||
| .note = For more details about orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules> | ||
|
|
||
| hir_analysis_inherent_ty_outside_new_alias_note = `{$ty_name}` does not define a new type, just alias of `{$alias_ty_name}` defined here |
There was a problem hiding this comment.
| hir_analysis_inherent_ty_outside_new_alias_note = `{$ty_name}` does not define a new type, just alias of `{$alias_ty_name}` defined here | |
| hir_analysis_inherent_ty_outside_new_alias_note = `{$ty_name}` does not define a new type, only an alias of `{$alias_ty_name}` |
| .label = impl for type defined outside of crate | ||
| .note = define and implement a trait or new type instead | ||
| .help = consider defining a trait and implementing it for the type or using a newtype wrapper like `struct MyType(ExternalType);` and implement it | ||
| .note = For more details about orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules> |
There was a problem hiding this comment.
| .note = For more details about orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules> | |
| .note = for more details about the orphan rules, see <https://doc.rust-lang.org/reference/items/implementations.html?highlight=orphan#orphan-rules> |
|
☔ The latest upstream changes (presumably #152213) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #141679
r? compiler