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

make .stash(..) work in macros without ICEing #69537

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
let expn_data = span.ctxt().outer_expn_data();
match expn_data.kind {
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
// well, it's "external"
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) | ExpnKind::ParserRecovery => true,
ExpnKind::Macro(MacroKind::Bang, _) => {
if expn_data.def_site.is_dummy() {
// Dummy span for the `def_site` means it's an external macro.
Expand Down
26 changes: 21 additions & 5 deletions src/librustc_errors/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{Applicability, Handler, Level, StashKey};
use crate::{Diagnostic, DiagnosticId, DiagnosticStyledString};

use log::debug;
use rustc_span::{MultiSpan, Span};
use rustc_span::{ExpnData, ExpnKind, MultiSpan, Span};
use std::fmt::{self, Debug};
use std::ops::{Deref, DerefMut};
use std::thread::panicking;
Expand Down Expand Up @@ -115,13 +115,29 @@ impl<'a> DiagnosticBuilder<'a> {

/// Stashes diagnostic for possible later improvement in a different,
/// later stage of the compiler. The diagnostic can be accessed with
/// the provided `span` and `key` through `.steal_diagnostic` on `Handler`.
/// the *returned* `span` and `key` through `.steal_diagnostic` on `Handler`.
/// Do not use the `span` passed into this function when calling `.steal_diagnostic`.
///
/// As with `buffer`, this is unless the handler has disabled such buffering.
pub fn stash(self, span: Span, key: StashKey) {
if let Some((diag, handler)) = self.into_diagnostic() {
#[must_use = "`Span` returned by `.stash(...)` must be later used in `.steal_diagnostic(..)`"]
pub fn stash(self, span: Span, key: StashKey) -> Span {
self.into_diagnostic().map_or(span, |(diag, handler)| {
// Before stashing the diagnostic, make sure the `span` is unique so that
// we do not attempt overwriting the key's slot later when dealing with macros.
// If we don't do this, then something like the following...:
// ```
// macro_rules! suite {
// ( $( $fn:ident; )* ) => { $(const A = "A".$fn();)* }
// }
//
// suite! { len; is_empty; }
// ```
// ...would result in overwriting due to using the same `def_site` span for `A`.
let data = ExpnData::default(ExpnKind::ParserRecovery, span, span.edition());
let span = span.fresh_expansion(data);
handler.stash_diagnostic(span, key, diag);
}
span
})
}

/// Converts the builder to a `Diagnostic` for later emission,
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ pub trait Emitter {

// Skip past non-macro entries, just in case there
// are some which do actually involve macros.
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
ExpnKind::Desugaring(..)
| ExpnKind::AstPass(..)
| ExpnKind::ParserRecovery => None,

ExpnKind::Macro(macro_kind, _) => Some(macro_kind),
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,11 @@ impl<'a> Parser<'a> {
format!("{}: <type>", id),
Applicability::HasPlaceholders,
);
err.stash(id.span, StashKey::ItemNoType);
let span = err.stash(id.span, StashKey::ItemNoType);

// The user intended that the type be inferred,
// so treat this as if the user wrote e.g. `const A: _ = expr;`.
P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
P(Ty { kind: TyKind::Infer, span, id: ast::DUMMY_NODE_ID })
}

/// Parses an enum declaration.
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,10 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {

// These are not macros.
// FIXME(eddyb) maybe there is a way to handle them usefully?
ExpnKind::Root | ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => return None,
ExpnKind::Root
| ExpnKind::AstPass(_)
| ExpnKind::Desugaring(_)
| ExpnKind::ParserRecovery => return None,
};

// If the callee is an imported macro from an external crate, need to get
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_span/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@ pub enum ExpnKind {
AstPass(AstPass),
/// Desugaring done by the compiler during HIR lowering.
Desugaring(DesugaringKind),
/// AST fragements produced by parser recovery.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// AST fragements produced by parser recovery.
/// AST fragments produced by parser recovery.

ParserRecovery,
}

impl ExpnKind {
Expand All @@ -742,6 +744,7 @@ impl ExpnKind {
},
ExpnKind::AstPass(kind) => kind.descr().to_string(),
ExpnKind::Desugaring(kind) => format!("desugaring of {}", kind.descr()),
ExpnKind::ParserRecovery => "parser recovery".to_string(),
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/issues/issue-69396-const-no-type-in-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
macro_rules! suite {
( $( $fn:ident; )* ) => {
$(
const A = "A".$fn();
//~^ ERROR the name `A` is defined multiple times
//~| ERROR missing type for `const` item
//~| ERROR missing type for `const` item
)*
}
}

suite! {
len;
is_empty;
}

fn main() {}
49 changes: 49 additions & 0 deletions src/test/ui/issues/issue-69396-const-no-type-in-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error[E0428]: the name `A` is defined multiple times
--> $DIR/issue-69396-const-no-type-in-macro.rs:4:13
|
LL | const A = "A".$fn();
| ^^^^^^^^^^^^^^^^^^^^
| |
| `A` redefined here
| previous definition of the value `A` here
...
LL | / suite! {
LL | | len;
LL | | is_empty;
LL | | }
| |_- in this macro invocation
|
= note: `A` must be defined only once in the value namespace of this module
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: missing type for `const` item
--> $DIR/issue-69396-const-no-type-in-macro.rs:4:19
|
LL | const A = "A".$fn();
| ^ help: provide a type for the item: `A: usize`
...
LL | / suite! {
LL | | len;
LL | | is_empty;
LL | | }
| |_- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: missing type for `const` item
--> $DIR/issue-69396-const-no-type-in-macro.rs:4:19
|
LL | const A = "A".$fn();
| ^ help: provide a type for the item: `A: bool`
...
LL | / suite! {
LL | | len;
LL | | is_empty;
LL | | }
| |_- in this macro invocation
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0428`.