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

pprust::item_to_string incorrectly prints ident for macro invocation items #24995

Closed
hugoduncan opened this issue Apr 30, 2015 · 2 comments
Closed
Labels
A-syntaxext Area: Syntax extensions

Comments

@hugoduncan
Copy link

pprust::item_to_string outputs the Item's ident. Usually the ident is a zero name, and thus doesn't show anything, but a user generated AST may have an ident, which is then shown.

The Item documentation isn't clear about how to generate a dummy name, and there is no DUMMY_IDENT or DUMMY_NAME, similar to the existing DUMMY_SP and DUMMY_NODE_ID.

The fix for this is probably just to remove the line. that outputs the ident. The output of a visibility should probably also be removed.

It might be useful to have a DUMMY_IDENT const or syntax::ast::Ident::dummy() function to generate a dummy ident, and/or a DUMMY_NAME const.

Playpen

#![feature(rustc_private)]
extern crate syntax;

use syntax::ast;
use syntax::codemap::{DUMMY_SP, respan};
use syntax::parse::token;
use syntax::print::pprust;
use syntax::ptr::P;

fn main() {
    let item = P(ast::Item {
            ident: token::gensym_ident("DUMMY"), // This ident is shown
            id: ast::DUMMY_NODE_ID,
            attrs: vec![],
            node: ast::ItemMac(
                respan(
                    DUMMY_SP,
                    ast::Mac_::MacInvocTT(
                        ast::Path{ span: DUMMY_SP, global: false, 
                                segments: vec![ast::PathSegment{
                                    identifier: token::gensym_ident("my_macro"),
                                    parameters: ast::PathParameters::none()
                                  }]
                            },
                        vec![ast::TokenTree::TtToken(
                            DUMMY_SP,
                            token::Token::Literal(
                                token::Lit::Str_(
                                    token::gensym("abc")
                                        ), None))],
                        0))
            ),
            vis: ast::Inherited,
            span: DUMMY_SP,
        });
    println!("{}", pprust::item_to_string(&item));
    }
@steveklabnik steveklabnik added the A-syntaxext Area: Syntax extensions label May 2, 2015
@lifthrasiir
Copy link
Contributor

lifthrasiir commented Oct 5, 2016

Triage: Is this an issue after all? We have macro_rules! which is a macro that takes an identifier (and I'm sure that we don't have a separate AST node for macro_rules!). Since it is also (while uncommon) possible to define macro_rules!-like macros via a compiler plugin, I consider this to be a non-issue. This behavior is correct.

For the posteriority, by the way, the following is a modern rendition of the example code (tested with rustc 1.14.0-nightly (144af3e97 2016-10-02)):

#![feature(rustc_private)]
extern crate syntax;

use syntax::ast;
use syntax::codemap::{DUMMY_SP, respan};
use syntax::parse::token;
use syntax::print::pprust;
use syntax::ptr::P;
use syntax::tokenstream;

fn main() {
    let item = P(ast::Item {
            ident: token::gensym_ident("DUMMY"),
            id: ast::DUMMY_NODE_ID,
            attrs: vec![],
            node: ast::ItemKind::Mac(
                respan(
                    DUMMY_SP,
                    ast::Mac_ {
                        path: ast::Path{ span: DUMMY_SP, global: false, 
                                segments: vec![ast::PathSegment{
                                    identifier: token::gensym_ident("my_macro"),
                                    parameters: ast::PathParameters::none()
                                  }]
                            },
                        tts: vec![tokenstream::TokenTree::Token(
                            DUMMY_SP,
                            token::Token::Literal(
                                token::Lit::Str_(
                                    token::gensym("abc")
                                        ), None))]
                        }
                    )
            ),
            vis: ast::Visibility::Inherited,
            span: DUMMY_SP,
        });
    println!("{}", pprust::item_to_string(&item));
    }

@Mark-Simulacrum
Copy link
Member

Mark-Simulacrum commented May 14, 2017

I cannot reproduce this today, so I'm going to close.

Edit: That is, the code examples don't compile -- and there's been some major churn in this area with macros 1.1 / 2.0 impending.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-syntaxext Area: Syntax extensions
Projects
None yet
Development

No branches or pull requests

4 participants