-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Permit duplicate macro imports #141043
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
base: master
Are you sure you want to change the base?
Permit duplicate macro imports #141043
Conversation
This comment has been minimized.
This comment has been minimized.
da5a69d
to
a978e7c
Compare
This comment has been minimized.
This comment has been minimized.
a978e7c
to
3241967
Compare
Can we have some tests about how does this incorporate with |
Consider a crate that depends on both `serde` (without the `derive` feature) and `serde_derive`, and imports `serde::Serialize` (a trait) and `serde_derive::Serialize` (a macro). Then, imagine some other crate in a build graph depends on `serde` *with* the `derive` feature; they import both the macro and trait simultaneously with `use serde::Serialize`. If duplicate imports of the same item are always forbidden, these crates cannot co-exist in the same build-graph; the former crate will fail to build, as its first import (which will now also import the `Serialize` macro) conflicts with its second import. This build hazard is confusing — the author of the second crate had no idea that their dependence on the `derive` feature might be problematic for other crates. The author of the first crate can mitigate the hazard by only glob-importing from proc-macro crates, but glob imports run against many's personal preference and tooling affordances (e.g., `rust-analyzer`'s auto-import feature). We mitigate this hazard across the ecosystem by permitting duplicate imports of macros. We don't limit this exception to proc macros, as it should not be a breaking change to rewrite a proc macro into a by-example macro. Although it would be semantically unproblematic to permit *all* duplicate imports (not just those of macros), other kinds of imports have not, in practice, posed the same hazard, and there might be cases we'd like to warn-by-default against. For now, we only permit duplicate macro imports. See https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/Allowing.20same-name.20imports.20of.20the.20same.20item/near/516777221
3241967
to
9d97e12
Compare
Not sure how this got closed... I've updated |
Consider a crate that depends on both
serde
(without thederive
feature) andserde_derive
, and importsserde::Serialize
(a trait) andserde_derive::Serialize
(a macro). Then, imagine some other crate in a build graph depends onserde
with thederive
feature; they import both the macro and trait simultaneously withuse serde::Serialize
. If duplicate imports of the same item are always forbidden, these crates cannot co-exist in the same build-graph; the former crate will fail to build, as its first import (which will now also import theSerialize
macro) conflicts with its second import.This build hazard is confusing — the author of the second crate had no idea that their dependence on the
derive
feature might be problematic for other crates. The author of the first crate can mitigate the hazard by only glob-importing from proc-macro crates, but glob imports run against many's personal preference and tooling affordances (e.g.,rust-analyzer
's auto-import feature).We mitigate this hazard across the ecosystem by permitting duplicate imports of macros. We don't limit this exception to proc macros, as it should not be a breaking change to rewrite a proc macro into a by-example macro. Although it would be semantically unproblematic to permit all duplicate imports (not just those of macros), other kinds of imports have not, in practice, posed the same hazard, and there might be cases we'd like to warn-by-default against. For now, we only permit duplicate macro imports.
See https://rust-lang.zulipchat.com/#narrow/channel/213817-t-lang/topic/Allowing.20same-name.20imports.20of.20the.20same.20item/near/516777221
r? @compiler-errors
I'm lang-nominating this because I'm not sure if this carve-out rises to the point of requiring an RFC and for the below open questions.
Open Questions
Permitting All Duplicate Identifiers
So long as two bindings resolve to the same item, it's semantically unproblematic for them to have the same name. This PR currently takes the most conservative approach and only carves out macro imports as a case in which duplicate imports of the same item are accepted. However, the warn-by-default
unusued_import
lint already effectively nudges against duplicate imports. I think we could permit duplicate imports of the the same item — for all kinds of items — without issue.