Rustfmt: Leading :: removed but necessary for disambiguation in 2018 edition

Created on 17 Aug 2018  路  13Comments  路  Source: rust-lang/rustfmt

Heya, I think I ran into a a bug with the uniform_paths feature. I've included the expected output, and error case below. Run on rustfmt 0.99.2-nightly (2018-08-07). I hope this comes in useful. Thanks! :sparkles:

Expected

This is the code I'm trying to write. We need the ::log prefix because the compiler is unsure which instace of log we're trying to refer to.

#![feature(rust_2018_preview, uniform_paths)]

use futures::prelude::*;
use http_03::cli::Cli;
use hyper::{service::service_fn_ok, Body, Response, Server};
use ::log::{error, info, log};
use structopt::StructOpt;

Current

rustfmt changes ::log to log which prevents the code from compiling (error included below).

#![feature(rust_2018_preview, uniform_paths)]

use futures::prelude::*;
use http_03::cli::Cli;
use hyper::{service::service_fn_ok, Body, Response, Server};
use log::{error, info, log};   // The `::` prefix is removed here
use structopt::StructOpt;

Error Message

2018-08-17-133200_1920x1080

bug p-high

Most helpful comment

Yeah, this is fixed on master. I'll update nightly rustfmt later in the week.

All 13 comments

It doesn't need to be macro imports. In this code:

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

rustfmt will also rewrite use ::criterion::Criterion; to use criterion::Criterion;

I'm confused about this issue. I have a patch written but it is failing ./tests/import.rs at line 62 because the tests expect the formatter to remove the leading ModSpace (::) operator.

How should I go around fixing this issue? Check to see if the user is using the uniform_paths feature?

How should I go around fixing this issue?

There is an option to set for which edition Rustfmt is working under. We should only preserve leading :: in the 2018 edition. For the test for this issue we should use the special comments for setting options to set the edition to 2018.

@nrc Should this issue be closed, given the PR got merged ?

@nrc what is that option?

@orium you can add edition = "2018" to rustfmt.toml to fix the problem

That lead me to discover #2989 :)

Edit: ...which was just fixed a few hours ago by #2984.

This bug is still present even if you set the edition to 2018:

orium@laptop-orion tmp % cat .rustfmt.toml
edition = "Edition2018"
orium@laptop-orion tmp % cargo +nightly fmt -- --check
Diff in /tmp/orium/tmp/src/lib.rs at line 1:
 #![feature(rust_2018_preview, uniform_paths)]
 extern crate criterion;
-use ::criterion::Criterion;
+use criterion::Criterion;

The title of this issue should probably be updated. I filed #3026 before finding this by digging through the commit logs :p I guess this has now been fixed, but a new rustfmt release for nightly hasn't been cut yet?

@jonhoo you're right -- changed it to use the title of the issue you opened, because that seems like a better description of what's going on.

Yeah, this is fixed on master. I'll update nightly rustfmt later in the week.

@nrc Will this be released as part of the next beta? I'm mentioning this because it would the current beta is the 2018 edition RC.

Probably. I am planning a beta uplift, not sure exactly when it'll happen.

Was this page helpful?
0 / 5 - 0 ratings