You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for RFC #2166: impl-only-use #48216
I was thinking about this RFC recently. I wonder if we should enable users of existing preludes to take advantage of this feature. For example, for back-compat reasons, the Rayon prelude cannot change to export _, but I wonder if on the other side we could support some notation like use rayon::prelude::* as _ -- or is that too weird?
(Note that -- with the feature as currently implemented at least -- preludes can have pub use Trait as _; and that will get picked up by a glob import.)
One deficiency of the current implementation is that _ in use Trait as _ is not hygienic.
Normal trait imports generated by macros produce the next error that doesn't happen with _.
#![feature(decl_macro)]#![allow(unused)]structS;mod m {pubtraitTr{fnf(&self){}}implTrfor::S{}}pub macro m(){use m::Tr;}m!();fnmain(){S.f();// ERROR no method named `f` found for type `S` in the current scope}
This is going to be fixed by #48842 or equivalent.
Actually, everything is more interesting - this bug is hidden by another bug!
Currently there's no way to use a trait in macro so that its methods are in scope after expansion of that macro.
Nothing of this works:
pub macro outer_use1($Rename: ident){use m::Tras $Rename;}pub macro outer_use2($Tr: ident){use m::$Tr;}pub macro outer_use3($Tr: path){pubuse $Tr;}outer_use1!(Rename);outer_use2!(Tr);outer_use3!(m::Tr);// still a "no method named `f`" error after expanding all the macros aboveS.f();
I just ran into an interesting corner case: if I write use somecrate as _;, that produces an unused_imports warning:
warning: unused import: `somecrate as _`
That seems wrong; use somecrate as _ should pull in somecrate even if the module doesn't use any of its symbols, such as for the purposes of linking, allocators, etc.
@eddyb I would suggest preparing a "stabilization report" that contains summary of what behavior is to be stabilized, what corner cases were uncovered, what changed with respect to the RFC, etc. Given that you wrote "Rust 2018-only" that seems to imply to me that there have been some corner cases or other things discovered? Or some interaction with the module system changes?
It seems like the behavior is the same when you use macro_rules!, presumably because of hygiene. e.g., this works either with or without _:
#![feature(decl_macro)]
#![feature(underscore_imports)]
#![allow(unused)]
struct S;
mod m {
pub trait Tr {
fn f(&self) {}
}
impl Tr for ::S {}
}
macro_rules! m {
() => {
use m::Tr as _;
}
}
m!();
fn main() {
S.f(); // ERROR no method named `f` found for type `S` in the current scope
}
decl macros are not stable, so I guess the hygiene interactions there don't matter too much.
I don't think we need to stabilize yet, but allowing use crate_name as _; in the new edition seems necessary in some situations (e.g. when you want to link against a crate but not use items from it).
However, if we make use foo as _foo; not warn when foo is a crate, then that's fine too.
If possible, I'd like for this feature to be included in Rust 2015 as well.
Are there any blockers / problems with doing that?
The hygiene problem doesn't seem to actually matter per #48216 (comment).
I'm not aware of any issues preventing stabilization of this on Rust 2015. as _ and all the other module/import stuff reformed in 2018 are pretty much orthogonal.
The hygiene issues appear only if we have both call-site and def-site hygiene, so they affect only nightly.
Activity
petrochenkov commentedon Mar 8, 2018
I'm going to implement it this weekend.
nikomatsakis commentedon Mar 13, 2018
I was thinking about this RFC recently. I wonder if we should enable users of existing preludes to take advantage of this feature. For example, for back-compat reasons, the Rayon prelude cannot change to export
_, but I wonder if on the other side we could support some notation likeuse rayon::prelude::* as _-- or is that too weird?(Note that -- with the feature as currently implemented at least -- preludes can have
pub use Trait as _;and that will get picked up by a glob import.)Rollup merge of rust-lang#48922 - petrochenkov:asunder, r=nikomatsakis
petrochenkov commentedon Mar 16, 2018
One deficiency of the current implementation is that
_inuse Trait as _is not hygienic.Normal trait imports generated by macros produce the next error that doesn't happen with
_.This is going to be fixed by #48842 or equivalent.
nikomatsakis commentedon Mar 16, 2018
@petrochenkov
You are saying that if we alter the macro in your example to this:
then the code will compile, right? (And that is the problem?)
In general, it seems sort of unclear whether one would expect this to work, but I guess consistency is good.
nikomatsakis commentedon Mar 16, 2018
Why would that fix the problem?
petrochenkov commentedon Mar 16, 2018
@nikomatsakis
Yes.
Due to attaching hygiene context to
_(#48842 (comment)).petrochenkov commentedon Mar 16, 2018
Actually, everything is more interesting - this bug is hidden by another bug!
Currently there's no way to
usea trait inmacroso that its methods are in scope after expansion of that macro.Nothing of this works:
Auto merge of #48842 - petrochenkov:under, r=nikomatsakis
joshtriplett commentedon Aug 18, 2018
I just ran into an interesting corner case: if I write
use somecrate as _;, that produces anunused_importswarning:That seems wrong;
use somecrate as _should pull insomecrateeven if the module doesn't use any of its symbols, such as for the purposes of linking, allocators, etc.eddyb commentedon Aug 18, 2018
That bug is not limited to
as _: if you writeuse std::io as _io;on stable, it warns.joshtriplett commentedon Aug 18, 2018
Working on fixing this.
2 remaining items
eddyb commentedon Aug 19, 2018
Nominating for discussion of Rust 2018-only stabilization at the next @rust-lang/lang meeting.
nikomatsakis commentedon Aug 23, 2018
@eddyb I would suggest preparing a "stabilization report" that contains summary of what behavior is to be stabilized, what corner cases were uncovered, what changed with respect to the RFC, etc. Given that you wrote "Rust 2018-only" that seems to imply to me that there have been some corner cases or other things discovered? Or some interaction with the module system changes?
nikomatsakis commentedon Aug 23, 2018
It seems like the behavior is the same when you use
macro_rules!, presumably because of hygiene. e.g., this works either with or without_:decl macros are not stable, so I guess the hygiene interactions there don't matter too much.
eddyb commentedon Aug 23, 2018
@nikomatsakis Sadly I am not as informed as @petrochenkov on this feature.
But #48216 (comment) appears to be fixed on nightly.
I don't think we need to stabilize yet, but allowing
use crate_name as _;in the new edition seems necessary in some situations (e.g. when you want to link against a crate but not use items from it).However, if we make
use foo as _foo;not warn whenfoois a crate, then that's fine too.Centril commentedon Aug 23, 2018
If possible, I'd like for this feature to be included in Rust 2015 as well.
Are there any blockers / problems with doing that?
The hygiene problem doesn't seem to actually matter per #48216 (comment).
petrochenkov commentedon Aug 24, 2018
I'm not aware of any issues preventing stabilization of this on Rust 2015.
as _and all the other module/import stuff reformed in 2018 are pretty much orthogonal.The hygiene issues appear only if we have both call-site and def-site hygiene, so they affect only nightly.
Centril commentedon Sep 15, 2018
@petrochenkov Since you are most informed about this, could you prepare a stabilization report for this for both Rust 2015 and Rust 2018?
Centril commentedon Nov 11, 2018
@petrochenkov any progress?
petrochenkov commentedon Nov 11, 2018
It's in the queue, but with lower priority than uniform paths.
petrochenkov commentedon Nov 28, 2018
Stabilization report + stabilization PR - #56303.
Auto merge of #56303 - petrochenkov:stabuseas, r=cramertj