Skip to content
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

Eliminate $crate in --pretty=expanded #38016

Closed
dtolnay opened this issue Nov 26, 2016 · 7 comments · Fixed by #38232 or #57915
Closed

Eliminate $crate in --pretty=expanded #38016

dtolnay opened this issue Nov 26, 2016 · 7 comments · Fixed by #38232 or #57915
Labels
A-macros-1.2 Area: Declarative macros 1.2 C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Nov 26, 2016

Similar to #37637 so cc @jseyfried.

cat src/main.rs

fn main() {
    println!("rust");
}

rustc --pretty=expanded -Zunstable-options src/main.rs

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
fn main() {
    $crate::io::_print(::std::fmt::Arguments::new_v1({
                                                         static __STATIC_FMTSTR:
                                                                &'static [&'static str]
                                                                =
                                                             &["rust\n"];
                                                         __STATIC_FMTSTR
                                                     },
                                                     &match () {
                                                          () => [],
                                                      }));

The $crate prevents the expanded code from being parsed by rustfmt.

rustc --pretty=expanded -Zunstable-options src/main.rs | rustfmt

error: expected expression, found `$`
 --> stdin:8:5
  |
8 |     $crate::io::_print(::std::fmt::Arguments::new_v1({
  |     ^

This breaks cargo expand which runs the code through rustfmt by default.

I think --pretty=expanded should produce valid Rust code which means either eliminate $crate like we did for #37637, or allow rustfmt to parse it.

@nagisa
Copy link
Member

nagisa commented Nov 26, 2016

Why are you relying on unstable features for, umm, stable tooling? --pretty et al by now are only used for debugging purposes and won’t be stabilised.

@bluss
Copy link
Member

bluss commented Nov 26, 2016

I don't think expand can produce valid Rust code (how does it handle hygiene?)

@dtolnay
Copy link
Member Author

dtolnay commented Nov 26, 2016

Before the $crate change, it produced syntactically valid Rust code even if the semantics were different in the case of hygiene collisions. This was useful for debugging purposes as @nagisa points out.

Since the $crate change, it produces syntactically invalid code which I consider a bug.

@jseyfried
Copy link
Contributor

Fixed in #38232 (#38232 (comment)).
After that lands, we will pretty-print $crate::io::_print as ::io::_print.

bors added a commit that referenced this issue Dec 23, 2016
Refactor global paths

This PR removes the field `global: bool` from `ast::Path` and `hir::Path`, instead representing a global path `::foo::bar` as `{{root}}::foo::bar`, where `{{root}}` is a virtual keyword `keywords::CrateRoot`.

Also, fixes #38016.

r? @nrc
@xiaoniu-578fa6bff964d005
Copy link

xiaoniu-578fa6bff964d005 commented Jan 20, 2019

$crate shows up again!

rustc +nightly-2018-06-13-x86_64-unknown-linux-gnu --pretty=expanded -Zunstable-options main.rs

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
fn main() {
    ::io::_print(::std::fmt::Arguments::new_v1(&["rust\n"],
                                               &match () { () => [], }));
}

rustc +nightly --pretty=expanded -Zunstable-options main.rs

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
fn main() {
    {
        $crate::io::_print($crate::fmt::Arguments::new_v1(&["rust\n"],
                                                          &match () {
                                                               () => [],
                                                           }));
    };
}

rustc +nightly --version

rustc 1.33.0-nightly (0c0c58528 2019-01-19)

@petrochenkov
Copy link
Contributor

Caused by #57155.
Reopening and assigning to myself.

@petrochenkov
Copy link
Contributor

Fixed in #57915

@petrochenkov petrochenkov removed their assignment Jan 26, 2019
@jonas-schievink jonas-schievink added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. A-macros-1.2 Area: Declarative macros 1.2 labels Jan 28, 2019
Centril added a commit to Centril/rust that referenced this issue Jan 28, 2019
Pretty print `$crate` as `crate` or `crate_name` in more cases

So, people do parse output of `--pretty=expanded` (sigh), so covering only the legacy proc-macro case (like it was done in rust-lang#57155) is not enough.

This PRs resolves all `$crate`s produced by macros, so they are all printed in the parseable form `$crate::foo` -> `crate::foo` or `crate_name::foo`.

Fixes rust-lang#38016 (comment)
Fixes rust-lang#57155 (comment)
@bors bors closed this as completed in 3fe8b4c Jan 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-1.2 Area: Declarative macros 1.2 C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
7 participants