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

Consider propagating rustc's compile_error representation as a token #405

Closed
dtolnay opened this issue Sep 10, 2023 · 0 comments · Fixed by #406
Closed

Consider propagating rustc's compile_error representation as a token #405

dtolnay opened this issue Sep 10, 2023 · 0 comments · Fixed by #406

Comments

@dtolnay
Copy link
Owner

dtolnay commented Sep 10, 2023

$ rustc -Zunpretty=expanded <(echo 'fn main() { let _ = compile_error!("..."); }')

Stderr:

error: ...
 --> /dev/fd/63:1:21
  |
1 | fn main() { let _ = compile_error!("..."); }
  |                     ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Stdout:

#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
fn main() { let _ = (/*ERROR*/); }

Notice that an error during macro expansion materializes in the expanded code in the form (/*ERROR*/).

This corresponds to rustc_ast's ExprKind::Err. https://github.com/rust-lang/rust/blob/1.72.0/compiler/rustc_ast/src/ast.rs#L1533-L1534

All expansion errors in expression position and type position are treated this way, such as a failure to match rule during macro_rules expansion, a procedural macro panic, or invalid token stream produced by a macro. (Macro failure in pattern position instead appears as _, and in item position the whole item is omitted.)

We should consider whether it would be appropriate for proc-macro2 to capture /*ERROR*/, or (/*ERROR*/), when parsed by our fallback parser. Most likely this would take the form of parsing it into TokenTree::Literal. For the above code snippet, syn would get a syntax tree resembling:

Stmt::Local {
    attrs: [],
    let_token: Token![let],
    pat: Pat::Wild {
        attrs: [],
        underscore_token: Token![_],
    },
    init: Some(LocalInit {
        eq_token: Token![=],
        expr: Expr::Lit {
            attrs: [],
            lit: Lit::Verbatim(
                Literal {
                    kind: Err,
                    symbol: "(/*ERROR*/)",
                },
            ),
        },
    }),
    semi_token: Token![;],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant