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

Extend proc_macro_back_compat lint to procedural-masquerade #83168

Merged
merged 1 commit into from
Mar 18, 2021
Merged
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
27 changes: 0 additions & 27 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -784,33 +784,6 @@ impl Nonterminal {
NtTT(tt) => tt.span(),
}
}

/// This nonterminal looks like some specific enums from
/// `proc-macro-hack` and `procedural-masquerade` crates.
/// We need to maintain some special pretty-printing behavior for them due to incorrect
/// asserts in old versions of those crates and their wide use in the ecosystem.
/// See issue #73345 for more details.
/// FIXME(#73933): Remove this eventually.
pub fn pretty_printing_compatibility_hack(&self) -> bool {
let item = match self {
NtItem(item) => item,
NtStmt(stmt) => match &stmt.kind {
ast::StmtKind::Item(item) => item,
_ => return false,
},
_ => return false,
};

let name = item.ident.name;
if name == sym::ProceduralMasqueradeDummyType || name == sym::ProcMacroHack {
if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
if let [variant] = &*enum_def.variants {
return variant.ident.name == sym::Input;
}
}
}
false
}
}

impl PartialEq for Nonterminal {
Expand Down
40 changes: 40 additions & 0 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use rustc_attr::{self as attr, Deprecation, Stability};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::{self, Lrc};
use rustc_errors::{DiagnosticBuilder, ErrorReported};
use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
use rustc_lint_defs::BuiltinLintDiagnostics;
use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
use rustc_session::{parse::ParseSess, Limit, Session};
use rustc_span::def_id::DefId;
Expand Down Expand Up @@ -1241,3 +1243,41 @@ pub fn get_exprs_from_tts(
}
Some(es)
}

/// This nonterminal looks like some specific enums from
/// `proc-macro-hack` and `procedural-masquerade` crates.
/// We need to maintain some special pretty-printing behavior for them due to incorrect
/// asserts in old versions of those crates and their wide use in the ecosystem.
/// See issue #73345 for more details.
/// FIXME(#73933): Remove this eventually.
pub(crate) fn pretty_printing_compatibility_hack(nt: &Nonterminal, sess: &ParseSess) -> bool {
let item = match nt {
Nonterminal::NtItem(item) => item,
Nonterminal::NtStmt(stmt) => match &stmt.kind {
ast::StmtKind::Item(item) => item,
_ => return false,
},
_ => return false,
};

let name = item.ident.name;
if name == sym::ProceduralMasqueradeDummyType {
if let ast::ItemKind::Enum(enum_def, _) = &item.kind {
if let [variant] = &*enum_def.variants {
if variant.ident.name == sym::Input {
sess.buffer_lint_with_diagnostic(
&PROC_MACRO_BACK_COMPAT,
item.ident.span,
ast::CRATE_NODE_ID,
"using `procedural-masquerade` crate",
BuiltinLintDiagnostics::ProcMacroBackCompat(
"The `procedural-masquerade` crate has been unnecessary since Rust 1.30.0. \
Versions of this crate below 0.1.7 will eventually stop compiling.".to_string())
);
return true;
}
}
}
}
false
}
3 changes: 2 additions & 1 deletion compiler/rustc_expand/src/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ impl MultiItemModifier for ProcMacroDerive {
}
_ => unreachable!(),
};
let input = if item.pretty_printing_compatibility_hack() {
let input = if crate::base::pretty_printing_compatibility_hack(&item, &ecx.sess.parse_sess)
{
TokenTree::token(token::Interpolated(Lrc::new(item)), DUMMY_SP).into()
} else {
nt_to_tokenstream(&item, &ecx.sess.parse_sess, CanSynthesizeMissingTokens::Yes)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl FromInternal<(TreeAndSpacing, &'_ ParseSess, &'_ mut Vec<Self>)>
delimiter: Delimiter::None,
stream,
span: DelimSpan::from_single(span),
flatten: nt.pretty_printing_compatibility_hack(),
flatten: crate::base::pretty_printing_compatibility_hack(&nt, sess),
})
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/proc-macro/issue-73933-procedural-masquerade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// check-pass
// aux-build:test-macros.rs

#[macro_use]
extern crate test_macros;

#[derive(Print)]
enum ProceduralMasqueradeDummyType { //~ WARN using
//~| WARN this was previously
Input
}

fn main() {}
25 changes: 25 additions & 0 deletions src/test/ui/proc-macro/issue-73933-procedural-masquerade.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
warning: using `procedural-masquerade` crate
--> $DIR/issue-73933-procedural-masquerade.rs:8:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(proc_macro_back_compat)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #83125 <https://github.com/rust-lang/rust/issues/83125>
= note: The `procedural-masquerade` crate has been unnecessary since Rust 1.30.0. Versions of this crate below 0.1.7 will eventually stop compiling.

warning: 1 warning emitted

Future incompatibility report: Future breakage date: None, diagnostic:
warning: using `procedural-masquerade` crate
--> $DIR/issue-73933-procedural-masquerade.rs:8:6
|
LL | enum ProceduralMasqueradeDummyType {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(proc_macro_back_compat)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #83125 <https://github.com/rust-lang/rust/issues/83125>
= note: The `procedural-masquerade` crate has been unnecessary since Rust 1.30.0. Versions of this crate below 0.1.7 will eventually stop compiling.

22 changes: 22 additions & 0 deletions src/test/ui/proc-macro/issue-73933-procedural-masquerade.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
PRINT-DERIVE INPUT (DISPLAY): enum ProceduralMasqueradeDummyType { Input, }
PRINT-DERIVE RE-COLLECTED (DISPLAY): enum ProceduralMasqueradeDummyType { Input }
PRINT-DERIVE INPUT (DEBUG): TokenStream [
Ident {
ident: "enum",
span: #0 bytes(100..104),
},
Ident {
ident: "ProceduralMasqueradeDummyType",
span: #0 bytes(105..134),
},
Group {
delimiter: Brace,
stream: TokenStream [
Ident {
ident: "Input",
span: #0 bytes(186..191),
},
],
span: #0 bytes(135..193),
},
]