Skip to content

Eliminate $crate in --pretty=expanded #38016

@dtolnay

Description

@dtolnay
Member

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.

Activity

nagisa

nagisa commented on Nov 26, 2016

@nagisa
Member

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

bluss commented on Nov 26, 2016

@bluss
Member

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

dtolnay

dtolnay commented on Nov 26, 2016

@dtolnay
MemberAuthor

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.

self-assigned this
on Nov 26, 2016
jseyfried

jseyfried commented on Dec 21, 2016

@jseyfried
Contributor

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

xiaoniu-578fa6bff964d005

xiaoniu-578fa6bff964d005 commented on Jan 20, 2019

@xiaoniu-578fa6bff964d005

$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

petrochenkov commented on Jan 20, 2019

@petrochenkov
Contributor

Caused by #57155.
Reopening and assigning to myself.

11 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-1-2Area: Declarative macros 1.2C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @nagisa@jonas-schievink@dtolnay@bluss@petrochenkov

      Issue actions

        Eliminate $crate in --pretty=expanded · Issue #38016 · rust-lang/rust