Skip to content

Commit

Permalink
Remove ast:: & base:: prefixes from some builtin macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ShE3py committed Feb 25, 2024
1 parent c440a5b commit 34eae07
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 118 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use rustc_ast::token;
use rustc_ast::tokenstream::TokenStream;
use rustc_attr as attr;
use rustc_errors::PResult;
use rustc_expand::base::{self, *};
use rustc_expand::base::{DummyResult, ExtCtxt, MacEager, MacResult};
use rustc_span::Span;

pub fn expand_cfg(
cx: &mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'static> {
) -> Box<dyn MacResult + 'static> {
let sp = cx.with_def_site_ctxt(sp);

match parse_cfg(cx, sp, tts) {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/compile_error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// The compiler code necessary to support the compile_error! extension.

use rustc_ast::tokenstream::TokenStream;
use rustc_expand::base::{self, *};
use rustc_expand::base::{get_single_str_from_tts, DummyResult, ExtCtxt, MacResult};
use rustc_span::Span;

pub fn expand_compile_error<'cx>(
cx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'cx> {
) -> Box<dyn MacResult + 'cx> {
let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") {
Ok(var) => var,
Err(guar) => return DummyResult::any(sp, guar),
Expand Down
44 changes: 21 additions & 23 deletions compiler/rustc_builtin_macros/src/concat.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use rustc_ast as ast;
use rustc_ast::tokenstream::TokenStream;
use rustc_expand::base::{self, DummyResult};
use rustc_ast::{ExprKind, LitKind, UnOp};
use rustc_expand::base::{get_exprs_from_tts, DummyResult, ExtCtxt, MacEager, MacResult};
use rustc_session::errors::report_lit_error;
use rustc_span::symbol::Symbol;

use crate::errors;

pub fn expand_concat(
cx: &mut base::ExtCtxt<'_>,
cx: &mut ExtCtxt<'_>,
sp: rustc_span::Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'static> {
let es = match base::get_exprs_from_tts(cx, tts) {
) -> Box<dyn MacResult + 'static> {
let es = match get_exprs_from_tts(cx, tts) {
Ok(es) => es,
Err(guar) => return DummyResult::any(sp, guar),
};
Expand All @@ -20,52 +20,50 @@ pub fn expand_concat(
let mut guar = None;
for e in es {
match e.kind {
ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::Str(s, _) | ast::LitKind::Float(s, _)) => {
ExprKind::Lit(token_lit) => match LitKind::from_token_lit(token_lit) {
Ok(LitKind::Str(s, _) | LitKind::Float(s, _)) => {
accumulator.push_str(s.as_str());
}
Ok(ast::LitKind::Char(c)) => {
Ok(LitKind::Char(c)) => {
accumulator.push(c);
}
Ok(ast::LitKind::Int(i, _)) => {
Ok(LitKind::Int(i, _)) => {
accumulator.push_str(&i.to_string());
}
Ok(ast::LitKind::Bool(b)) => {
Ok(LitKind::Bool(b)) => {
accumulator.push_str(&b.to_string());
}
Ok(ast::LitKind::CStr(..)) => {
Ok(LitKind::CStr(..)) => {
guar = Some(cx.dcx().emit_err(errors::ConcatCStrLit { span: e.span }));
}
Ok(ast::LitKind::Byte(..) | ast::LitKind::ByteStr(..)) => {
Ok(LitKind::Byte(..) | LitKind::ByteStr(..)) => {
guar = Some(cx.dcx().emit_err(errors::ConcatBytestr { span: e.span }));
}
Ok(ast::LitKind::Err(guarantee)) => {
Ok(LitKind::Err(guarantee)) => {
guar = Some(guarantee);
}
Err(err) => {
guar = Some(report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span));
}
},
// We also want to allow negative numeric literals.
ast::ExprKind::Unary(ast::UnOp::Neg, ref expr)
if let ast::ExprKind::Lit(token_lit) = expr.kind =>
{
match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::Int(i, _)) => accumulator.push_str(&format!("-{i}")),
Ok(ast::LitKind::Float(f, _)) => accumulator.push_str(&format!("-{f}")),
ExprKind::Unary(UnOp::Neg, ref expr) if let ExprKind::Lit(token_lit) = expr.kind => {
match LitKind::from_token_lit(token_lit) {
Ok(LitKind::Int(i, _)) => accumulator.push_str(&format!("-{i}")),
Ok(LitKind::Float(f, _)) => accumulator.push_str(&format!("-{f}")),
Err(err) => {
guar = Some(report_lit_error(&cx.sess.parse_sess, err, token_lit, e.span));
}
_ => missing_literal.push(e.span),
}
}
ast::ExprKind::IncludedBytes(..) => {
ExprKind::IncludedBytes(..) => {
cx.dcx().emit_err(errors::ConcatBytestr { span: e.span });
}
ast::ExprKind::Err(guarantee) => {
ExprKind::Err(guarantee) => {
guar = Some(guarantee);
}
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
_ => {
missing_literal.push(e.span);
}
Expand All @@ -79,5 +77,5 @@ pub fn expand_concat(
return DummyResult::any(sp, guar);
}
let sp = cx.with_def_site_ctxt(sp);
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
}
89 changes: 42 additions & 47 deletions compiler/rustc_builtin_macros/src/concat_bytes.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use rustc_ast as ast;
use rustc_ast::{ptr::P, tokenstream::TokenStream};
use rustc_expand::base::{self, DummyResult};
use rustc_ast::{ptr::P, token, tokenstream::TokenStream, ExprKind, LitIntType, LitKind, UintTy};
use rustc_expand::base::{get_exprs_from_tts, DummyResult, ExtCtxt, MacEager, MacResult};
use rustc_session::errors::report_lit_error;
use rustc_span::{ErrorGuaranteed, Span};

use crate::errors;

/// Emits errors for literal expressions that are invalid inside and outside of an array.
fn invalid_type_err(
cx: &mut base::ExtCtxt<'_>,
token_lit: ast::token::Lit,
cx: &mut ExtCtxt<'_>,
token_lit: token::Lit,
span: Span,
is_nested: bool,
) -> ErrorGuaranteed {
Expand All @@ -18,18 +17,18 @@ fn invalid_type_err(
};
let snippet = cx.sess.source_map().span_to_snippet(span).ok();
let dcx = cx.dcx();
match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::CStr(_, _)) => {
match LitKind::from_token_lit(token_lit) {
Ok(LitKind::CStr(_, _)) => {
// Avoid ambiguity in handling of terminal `NUL` by refusing to
// concatenate C string literals as bytes.
dcx.emit_err(errors::ConcatCStrLit { span })
}
Ok(ast::LitKind::Char(_)) => {
Ok(LitKind::Char(_)) => {
let sugg =
snippet.map(|snippet| ConcatBytesInvalidSuggestion::CharLit { span, snippet });
dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "character", sugg })
}
Ok(ast::LitKind::Str(_, _)) => {
Ok(LitKind::Str(_, _)) => {
// suggestion would be invalid if we are nested
let sugg = if !is_nested {
snippet.map(|snippet| ConcatBytesInvalidSuggestion::StrLit { span, snippet })
Expand All @@ -38,27 +37,24 @@ fn invalid_type_err(
};
dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "string", sugg })
}
Ok(ast::LitKind::Float(_, _)) => {
Ok(LitKind::Float(_, _)) => {
dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "float", sugg: None })
}
Ok(ast::LitKind::Bool(_)) => {
Ok(LitKind::Bool(_)) => {
dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "boolean", sugg: None })
}
Ok(ast::LitKind::Int(_, _)) if !is_nested => {
Ok(LitKind::Int(_, _)) if !is_nested => {
let sugg =
snippet.map(|snippet| ConcatBytesInvalidSuggestion::IntLit { span: span, snippet });
snippet.map(|snippet| ConcatBytesInvalidSuggestion::IntLit { span, snippet });
dcx.emit_err(ConcatBytesInvalid { span, lit_kind: "numeric", sugg })
}
Ok(ast::LitKind::Int(
val,
ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
)) => {
Ok(LitKind::Int(val, LitIntType::Unsuffixed | LitIntType::Unsigned(UintTy::U8))) => {
assert!(val.get() > u8::MAX.into()); // must be an error
dcx.emit_err(ConcatBytesOob { span })
}
Ok(ast::LitKind::Int(_, _)) => dcx.emit_err(ConcatBytesNonU8 { span }),
Ok(ast::LitKind::ByteStr(..) | ast::LitKind::Byte(_)) => unreachable!(),
Ok(ast::LitKind::Err(guar)) => guar,
Ok(LitKind::Int(_, _)) => dcx.emit_err(ConcatBytesNonU8 { span }),
Ok(LitKind::ByteStr(..) | LitKind::Byte(_)) => unreachable!(),
Ok(LitKind::Err(guar)) => guar,
Err(err) => report_lit_error(&cx.sess.parse_sess, err, token_lit, span),
}
}
Expand All @@ -68,24 +64,24 @@ fn invalid_type_err(
/// Otherwise, returns `None`, and either pushes the `expr`'s span to `missing_literals` or
/// updates `guar` accordingly.
fn handle_array_element(
cx: &mut base::ExtCtxt<'_>,
cx: &mut ExtCtxt<'_>,
guar: &mut Option<ErrorGuaranteed>,
missing_literals: &mut Vec<rustc_span::Span>,
expr: &P<rustc_ast::Expr>,
) -> Option<u8> {
let dcx = cx.dcx();

match expr.kind {
ast::ExprKind::Lit(token_lit) => {
match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::Int(
ExprKind::Lit(token_lit) => {
match LitKind::from_token_lit(token_lit) {
Ok(LitKind::Int(
val,
ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
LitIntType::Unsuffixed | LitIntType::Unsigned(UintTy::U8),
)) if let Ok(val) = u8::try_from(val.get()) => {
return Some(val);
}
Ok(ast::LitKind::Byte(val)) => return Some(val),
Ok(ast::LitKind::ByteStr(..)) => {
Ok(LitKind::Byte(val)) => return Some(val),
Ok(LitKind::ByteStr(..)) => {
guar.get_or_insert_with(|| {
dcx.emit_err(errors::ConcatBytesArray { span: expr.span, bytestr: true })
});
Expand All @@ -95,12 +91,12 @@ fn handle_array_element(
}
};
}
ast::ExprKind::Array(_) | ast::ExprKind::Repeat(_, _) => {
ExprKind::Array(_) | ExprKind::Repeat(_, _) => {
guar.get_or_insert_with(|| {
dcx.emit_err(errors::ConcatBytesArray { span: expr.span, bytestr: false })
});
}
ast::ExprKind::IncludedBytes(..) => {
ExprKind::IncludedBytes(..) => {
guar.get_or_insert_with(|| {
dcx.emit_err(errors::ConcatBytesArray { span: expr.span, bytestr: false })
});
Expand All @@ -112,11 +108,11 @@ fn handle_array_element(
}

pub fn expand_concat_bytes(
cx: &mut base::ExtCtxt<'_>,
sp: rustc_span::Span,
cx: &mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'static> {
let es = match base::get_exprs_from_tts(cx, tts) {
) -> Box<dyn MacResult + 'static> {
let es = match get_exprs_from_tts(cx, tts) {
Ok(es) => es,
Err(guar) => return DummyResult::any(sp, guar),
};
Expand All @@ -125,7 +121,7 @@ pub fn expand_concat_bytes(
let mut guar = None;
for e in es {
match &e.kind {
ast::ExprKind::Array(exprs) => {
ExprKind::Array(exprs) => {
for expr in exprs {
if let Some(elem) =
handle_array_element(cx, &mut guar, &mut missing_literals, expr)
Expand All @@ -134,10 +130,9 @@ pub fn expand_concat_bytes(
}
}
}
ast::ExprKind::Repeat(expr, count) => {
if let ast::ExprKind::Lit(token_lit) = count.value.kind
&& let Ok(ast::LitKind::Int(count_val, _)) =
ast::LitKind::from_token_lit(token_lit)
ExprKind::Repeat(expr, count) => {
if let ExprKind::Lit(token_lit) = count.value.kind
&& let Ok(LitKind::Int(count_val, _)) = LitKind::from_token_lit(token_lit)
{
if let Some(elem) =
handle_array_element(cx, &mut guar, &mut missing_literals, expr)
Expand All @@ -152,35 +147,35 @@ pub fn expand_concat_bytes(
);
}
}
&ast::ExprKind::Lit(token_lit) => match ast::LitKind::from_token_lit(token_lit) {
Ok(ast::LitKind::Byte(val)) => {
&ExprKind::Lit(token_lit) => match LitKind::from_token_lit(token_lit) {
Ok(LitKind::Byte(val)) => {
accumulator.push(val);
}
Ok(ast::LitKind::ByteStr(ref bytes, _)) => {
Ok(LitKind::ByteStr(ref bytes, _)) => {
accumulator.extend_from_slice(bytes);
}
_ => {
guar.get_or_insert_with(|| invalid_type_err(cx, token_lit, e.span, false));
}
},
ast::ExprKind::IncludedBytes(bytes) => {
ExprKind::IncludedBytes(bytes) => {
accumulator.extend_from_slice(bytes);
}
ast::ExprKind::Err(guarantee) => {
ExprKind::Err(guarantee) => {
guar = Some(*guarantee);
}
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
_ => {
missing_literals.push(e.span);
}
}
}
if !missing_literals.is_empty() {
let guar = cx.dcx().emit_err(errors::ConcatBytesMissingLiteral { spans: missing_literals });
return base::MacEager::expr(DummyResult::raw_expr(sp, Some(guar)));
return MacEager::expr(DummyResult::raw_expr(sp, Some(guar)));
} else if let Some(guar) = guar {
return base::MacEager::expr(DummyResult::raw_expr(sp, Some(guar)));
return MacEager::expr(DummyResult::raw_expr(sp, Some(guar)));
}
let sp = cx.with_def_site_ctxt(sp);
base::MacEager::expr(cx.expr_byte_str(sp, accumulator))
MacEager::expr(cx.expr_byte_str(sp, accumulator))
}
26 changes: 13 additions & 13 deletions compiler/rustc_builtin_macros/src/concat_idents.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use rustc_ast as ast;
use rustc_ast::ptr::P;
use rustc_ast::token::{self, Token};
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_expand::base::{self, *};
use rustc_ast::{AttrVec, Expr, ExprKind, Path, Ty, TyKind, DUMMY_NODE_ID};
use rustc_expand::base::{DummyResult, ExtCtxt, MacResult};
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::Span;

Expand All @@ -12,7 +12,7 @@ pub fn expand_concat_idents<'cx>(
cx: &'cx mut ExtCtxt<'_>,
sp: Span,
tts: TokenStream,
) -> Box<dyn base::MacResult + 'cx> {
) -> Box<dyn MacResult + 'cx> {
if tts.is_empty() {
let guar = cx.dcx().emit_err(errors::ConcatIdentsMissingArgs { span: sp });
return DummyResult::any(sp, guar);
Expand Down Expand Up @@ -47,21 +47,21 @@ pub fn expand_concat_idents<'cx>(
ident: Ident,
}

impl base::MacResult for ConcatIdentsResult {
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
Some(P(ast::Expr {
id: ast::DUMMY_NODE_ID,
kind: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
impl MacResult for ConcatIdentsResult {
fn make_expr(self: Box<Self>) -> Option<P<Expr>> {
Some(P(Expr {
id: DUMMY_NODE_ID,
kind: ExprKind::Path(None, Path::from_ident(self.ident)),
span: self.ident.span,
attrs: ast::AttrVec::new(),
attrs: AttrVec::new(),
tokens: None,
}))
}

fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
Some(P(ast::Ty {
id: ast::DUMMY_NODE_ID,
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
fn make_ty(self: Box<Self>) -> Option<P<Ty>> {
Some(P(Ty {
id: DUMMY_NODE_ID,
kind: TyKind::Path(None, Path::from_ident(self.ident)),
span: self.ident.span,
tokens: None,
}))
Expand Down
Loading

0 comments on commit 34eae07

Please sign in to comment.