Skip to content

Rustc does not warn about use with paths incompatible with uniform_paths for edition 2018 #53797

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
orium opened this issue Aug 29, 2018 · 22 comments
Labels
A-edition-2018 Area: The 2018 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. C-future-incompatibility Category: Future-incompatibility lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@orium
Copy link
Member

orium commented Aug 29, 2018

Updated Description:

This code generates no warnings when compiled with the 2015 edition:

#![deny(rust_2018_compatibility)]
#![allow(unused_imports)]

extern crate rayon;

mod foo {
    mod rayon {}
    
    use rayon::Scope;
}

but when compiled with the 2018 edition fails to compile:

error[E0432]: unresolved import `rayon::Scope`
 --> src/lib.rs:9:9
  |
9 |     use rayon::Scope;
  |         ^^^^^^^^^^^^ no `Scope` in `foo::rayon`

error[E0659]: `rayon` is ambiguous (name vs any other name during import resolution)
 --> src/lib.rs:9:9
  |
9 |     use rayon::Scope;
  |         ^^^^^ ambiguous name
  |
note: `rayon` could refer to the module defined here
 --> src/lib.rs:7:5
  |
7 |     mod rayon {}
  |     ^^^^^^^^^^^^
  = help: use `self::rayon` to refer to this module unambiguously
note: `rayon` could also refer to the extern crate imported here
 --> src/lib.rs:4:1
  |
4 | extern crate rayon;
  | ^^^^^^^^^^^^^^^^^^^
  = help: use `::rayon` to refer to this extern crate unambiguously

error: aborting due to 2 previous errors

Some errors occurred: E0432, E0659.
For more information about an error, try `rustc --explain E0432`.

Old Description

Rustc does not warn about use with paths incompatible with uniform_paths for edition 2018. This means that rustfix it will not fix your code for edition 2018.

To reproduce create the following project:

Cargo.toml:

[package]
name = "foobar"
version = "0.0.0"
authors = ["Foo <[email protected]>"]

[dependencies]
criterion = "0.2"

src/lib.rs:

#![allow(unused)]
#![feature(rust_2018_preview, uniform_paths)]
extern crate criterion;
use criterion::Criterion;

If you run cargo +nightly check you will not get any warning, which means that when you run cargo +nightly fix --edition nothing will change, but the code is not compilable in rust 2018. Change Cargo.toml to enable edition 2018 (by adding cargo-features = ["edition"] and package.edition = "2018") and run cargo +nightly build:

$ cargo +nightly build
[...]
error: `criterion` import is ambiguous
 --> src/lib.rs:4:5
  |
3 | extern crate criterion;
  | ----------------------- can refer to `self::criterion`
4 | use criterion::Criterion;
  |     ^^^^^^^^^ can refer to external crate `::criterion`
  |
  = help: write `::criterion` or `self::criterion` explicitly instead
  = note: relative `use` paths enabled by `#![feature(uniform_paths)]`
[...]

cargo +nightly fix --edition should have changed line 4 to use ::criterion::Criterion.

Ref: Bug in cargo: rust-lang/cargo#5905

@orium orium added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-future-incompatibility Category: Future-incompatibility lints C-bug Category: This is a bug. F-rust_2018_preview `#![feature(rust_2018_preview)]` A-edition-2018-lints labels Aug 29, 2018
@orium orium added this to the Rust 2018 RC milestone Sep 1, 2018
@zackmdavis
Copy link
Member

The error is emitted during name resolution:

// Special-case the error when `self::x` finds its own `use x;`.
if has_external_crate &&
results.module_scope == Some(span) &&
results.block_scopes.is_empty() {
let msg = format!("`{}` import is redundant", name);
this.session.struct_span_err(span, &msg)
.span_label(span,
format!("refers to external crate `::{}`", name))
.span_label(span,
format!("defines `self::{}`, shadowing itself", name))
.help(&format!("remove or write `::{}` explicitly instead", name))
.note("relative `use` paths enabled by `#![feature(uniform_paths)]`")
.emit();

To make this rustfix-able, we would want to reformulate the .help note as a structured suggestion instead. (With the caveat that rustfix can't yet handle overlapping/alternative suggestions, so hopefully one of the "remove" or "::cratename" alternatives can be the canonical fix.)

@eddyb
Copy link
Member

eddyb commented Sep 6, 2018

@zackmdavis That's not enough. What needs to happen is the canaries need to be injected in Rust 2015 before switching to Rust 2018, and that's where the correct suggestions can be generated, because we know what the user meant (in the working Rust 2015 code).

I've been meaning to do this ever since I realized it's actually plausible with the way the canaries are implemented, because we can inject them in more situations, and provide different diagnostics.

@eddyb eddyb self-assigned this Sep 6, 2018
@orium orium modified the milestones: Rust 2018 RC, Edition 2018 RC 2 Sep 22, 2018
@aturon aturon added I-nominated T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 17, 2018
@nikomatsakis
Copy link
Contributor

@eddyb what is the story with this one?

@eddyb
Copy link
Member

eddyb commented Oct 17, 2018

@nikomatsakis I didn't get to it, but also I've started to doubt the idea of reusing canaries.
Maybe it'd be better to have a separate check for the name resolving in the same module, whenever the suggestions to add crate:: are emitted?

cc @petrochenkov

@pnkfelix
Copy link
Member

Discussed at compiler team meeting.

This seems important enough to resolve that it should remain on the RC2 milestone.

@petrochenkov petrochenkov self-assigned this Oct 18, 2018
@nikomatsakis
Copy link
Contributor

I just tested this locally and the most important instance (criterion conflicting with itself) was fixed in the meantime.

@eddyb
Copy link
Member

eddyb commented Oct 18, 2018

The example works now, as the extern crate and the extern prelude entry with the same name are the same entity - we still might need to do some automatic migration for ambiguities involving local items, but it's lower priority I reckon.

@nikomatsakis
Copy link
Contributor

Closing for now — the problem described in this issue is fixed, though there may be a more general bug (we should try to make a test case for that?).

@eddyb
Copy link
Member

eddyb commented Oct 18, 2018

You can still reproduce this by having a local item (in the type/module namespace), with the same name, e.g. this triggers the error on the playground, on beta & nightly:

extern crate rayon;
mod foo {
    mod rayon {}
    use rayon::Scope;
}

I added #![deny(rust_2018_compatibility)] and got no errors, so rustfix wouldn't do anything.

@nikomatsakis
Copy link
Contributor

OK, I'm going to re-open, but move to Rust 2018 Release milestone, since I think that we can probably address this with some targeted tweaks and it seems like it will not affect The Entire World.

@nikomatsakis nikomatsakis reopened this Oct 18, 2018
@nikomatsakis
Copy link
Contributor

BTW, there is discussion on Zulip and in particular starting around here we are talking about the new reproduction

@petrochenkov petrochenkov removed their assignment Oct 25, 2018
@pnkfelix
Copy link
Member

removing old nomination tag.

@pnkfelix
Copy link
Member

pnkfelix commented Nov 8, 2018

visited for T-compiler triage. Marking P-high, since it is on the 2018 Release milestone.

@pnkfelix pnkfelix added the P-high High priority label Nov 8, 2018
@nikomatsakis
Copy link
Contributor

@petrochenkov does your PR address this issue?

@petrochenkov
Copy link
Contributor

does your PR address this issue?

No, it doesn't.

@petrochenkov
Copy link
Contributor

I haven't added any new lints on 2015 edition (which is this issue about), only uniform path reimplementation on 2018 edition.

@alexcrichton
Copy link
Member

I've updated the OP to reflect the current status here

@nikomatsakis it sounds like on the discussion linked you had a strawman of how to fix this. Did you get a chance to test that out? Or is someone lined up to fix this perhaps in a different fashion?

@nikomatsakis
Copy link
Contributor

I do think this is plausible to fix with a "quick hack". I might try to pull it off today, though I'm not sure how important it is. The original example no longer applies, and it's not clear how common the pattern described above is in real code.

@alexcrichton
Copy link
Member

I think I'd agree as well that this, while still a bug, is likely no longer edition-critical. Any opposition to removing it from the milestone and removing P-high?

@nikomatsakis
Copy link
Contributor

@alexcrichton sgtm, I didn't get to do any work on this today

@nikomatsakis nikomatsakis removed this from the Rust 2018 Release milestone Nov 21, 2018
@alexcrichton alexcrichton removed F-rust_2018_preview `#![feature(rust_2018_preview)]` P-high High priority labels Nov 21, 2018
@ehuss
Copy link
Contributor

ehuss commented Aug 2, 2020

Another example, encountered by a user:

#![deny(rust_2018_compatibility)]
extern crate ptr;

pub mod foo {
    pub use std::ptr;
    pub use ptr::Shared;
}

Does not generate any errors/warnings, but fails to compile on 2018.

@nikomatsakis
Copy link
Contributor

I think at this point we can close this bug -- we're not probably going to go back and make more changes here, the transition code seems to have worked "well enough". (On the other hand, we could leave it open to collect more examples that we may someday go back and fix, I suppose.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2018 Area: The 2018 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. C-future-incompatibility Category: Future-incompatibility lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
Archived in project
Development

No branches or pull requests

10 participants