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

-Zunpretty=expanded does not preserve hygiene #13573

Open
klutzy opened this issue Apr 17, 2014 · 5 comments
Open

-Zunpretty=expanded does not preserve hygiene #13573

klutzy opened this issue Apr 17, 2014 · 5 comments
Labels
A-hygiene Area: Macro hygiene A-pretty Area: Pretty printing (including `-Z unpretty`) C-feature-request Category: A feature request, i.e: not implemented / a PR. P-low Low priority requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@klutzy
Copy link
Contributor

klutzy commented Apr 17, 2014

rustc internally converts for val in iter { ... } to:

match &mut iter {
    i =>
    loop  { match i.next() { None => break , Some(val) => { ... } } }
}

where i is created by let local_ident = token::gensym_ident("i");, so i does not clash with other local variables.

However, pprust just prints it as plain i. So the following code:

let mut i: int = 0;
for _ in iter {
    i += 1;
}

is prettified by rustc as:

let mut i: int = 0;
match &mut iter {
    i =>
    loop  { match i.next() { None => break , Some(_) => { i += 1; } } }
}

which is not correct anymore.

@jbclements
Copy link
Contributor

The problem here is generally that rustc --pretty doesn't respect hygiene. You can see this by expanding this program

#![feature(macro_rules)]

macro_rules! dont_capture {($v:expr) => ({let _x = 13; $v})}

fn main() -> (){
    let _x = 2;
    let y = dont_capture!(_x);
    println!("2 = {}",y);
}

using rustc --pretty expanded, which shows a program where the inner let should clearly capture the inner varref.

As far as I know, rustc --pretty doesn't claim to preserve semantics. I would argue that this is currently not a bug, or perhaps a change request.

@steveklabnik steveklabnik added the A-pretty Area: Pretty printing (including `-Z unpretty`) label Jan 23, 2015
@steveklabnik
Copy link
Member

Triage: no changes I'm aware of.

@Mark-Simulacrum
Copy link
Member

Loops are no longer expanded as suggested by this issue as far as I can tell. However, the macro example still doesn't work quite correctly in that dont_capture!(_x) is expanded to let _x = 13; _x, not the more-proper let _x_mangled = 13; _x. Updating issue title to be more accurate.

@Mark-Simulacrum Mark-Simulacrum changed the title pprust exposes local variable i when for is expanded pretty=expanded does not preserve semantics of macro expansion Apr 29, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 21, 2017
@steveklabnik
Copy link
Member

Triage: no change

@Mark-Simulacrum Mark-Simulacrum changed the title pretty=expanded does not preserve semantics of macro expansion pretty=expanded does not preserve hygiene Sep 22, 2019
@Mark-Simulacrum Mark-Simulacrum added C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-bug Category: This is a bug. labels Sep 22, 2019
@jonas-schievink jonas-schievink added requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 12, 2020
@jyn514
Copy link
Member

jyn514 commented Aug 16, 2021

Triage: no change. However, there is a new option -Zunpretty=expanded,hygiene, which does distinguish the two (although it's quite verbose):

macro_rules! dont_capture
    /*
    1348#0
    */ {
    ($v : expr) => ({ let _x = 13 ; $v })
}

fn main /* 691#0 */() -> () {
    let _x /* 1350#0 */ = 2;
    let y /* 1352#0 */ = { let _x /* 1350#3 */ = 13; _x /* 1350#0 */ };
...

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Nov 16, 2022
internal: error instead of panic on invalid file range

Fixes the panic in rust-lang/rust-analyzer#13170
@fmease fmease changed the title pretty=expanded does not preserve hygiene -Zunpretty=expanded does not preserve hygiene Nov 5, 2024
flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 7, 2024
Don't lint unnamed consts and nested items within functions in `missing_docs_in_private_items`

With this change we no longer require doc comments for `const _: ()` items as well as nested items in functions or other bodies. In both of those cases, rustdoc generates no documentation even with `--document-private-items`.

Fixes rust-lang#13427 (first commit)
Fixes rust-lang#13298 (second commit)
cc rust-lang/rust-clippy#5736 (comment)

changelog: [`missing_docs_in_private_items`]: avoid linting in more cases where rustdoc generates no documentation
@fmease fmease added A-hygiene Area: Macro hygiene P-low Low priority labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-hygiene Area: Macro hygiene A-pretty Area: Pretty printing (including `-Z unpretty`) C-feature-request Category: A feature request, i.e: not implemented / a PR. P-low Low priority requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants