Skip to content

Add note when inherent impl for a alias type defined outside of the crate #142415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Member

@xizheyin xizheyin commented Jun 12, 2025

Fixes #141679

r? compiler

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 12, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Jun 23, 2025

r? compiler

@rustbot rustbot assigned lcnr and unassigned BoxyUwU Jun 23, 2025
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 16, 2025
Copy link
Member Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rustbot ready

Comment on lines -244 to +247
.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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 20, 2025
@fee1-dead
Copy link
Member

do you not get TyKind::Alias when you use type_of on the impl? coherence is probably fine to use middle queries

Copy link
Member Author

@xizheyin xizheyin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines +112 to +114
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged the two ifs.

@compiler-errors
Copy link
Member

r? compiler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

error message for inherent impl on type that is external could be more explanatory
7 participants