Skip to content

Commit

Permalink
Erase AttrArgsEq::Ast in ast validation
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jul 11, 2024
1 parent 99ded8b commit e89df61
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ build/
/target
/src/bootstrap/target
/src/tools/x/target
/inc-fat/
# Created by default with `src/ci/docker/run.sh`
/obj/
/rustc-ice*
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,10 @@ fn visit_attr_args<T: MutVisitor>(vis: &mut T, args: &mut AttrArgs) {
vis.visit_expr(expr);
vis.visit_span(eq_span);
}
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
AttrArgs::Eq(eq_span, AttrArgsEq::Hir(lit)) => {
vis.visit_span(eq_span);
let MetaItemLit { symbol: _, suffix: _, kind: _, span } = lit;
vis.visit_span(span);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,9 +1233,7 @@ pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) -
AttrArgs::Empty => {}
AttrArgs::Delimited(_args) => {}
AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => try_visit!(visitor.visit_expr(expr)),
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
unreachable!("in literal form when walking mac args eq: {:?}", lit)
}
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(_lit)) => {}
}
V::Result::output()
}
25 changes: 2 additions & 23 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,31 +939,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
match args {
AttrArgs::Empty => AttrArgs::Empty,
AttrArgs::Delimited(args) => AttrArgs::Delimited(self.lower_delim_args(args)),
// This is an inert key-value attribute - it will never be visible to macros
// after it gets lowered to HIR. Therefore, we can extract literals to handle
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
// In valid code the value always ends up as a single literal. Otherwise, a dummy
// literal suffices because the error is handled elsewhere.
let lit = if let ExprKind::Lit(token_lit) = expr.kind
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
{
lit
} else if let ExprKind::Err(guar) = expr.kind {
MetaItemLit {
symbol: kw::Empty,
suffix: None,
kind: LitKind::Err(guar),
span: DUMMY_SP,
}
} else {
span_bug!(*eq_span, "eq attrs can only be literals, but got {expr:#?}")
};
AttrArgs::Eq(*eq_span, AttrArgsEq::Hir(lit))
}
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
span_bug!(*eq_span, "eq attrs can only be errors, but got {expr:#?}")
}
AttrArgs::Eq(_, AttrArgsEq::Hir(_lit)) => args.clone(),
}
}

Expand Down
27 changes: 15 additions & 12 deletions compiler/rustc_parse/src/validate_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ pub fn parse_meta(psess: &ParseSess, attr: &mut Attribute) -> Result<MetaItem, E
.map_err(|d| d.emit())?;
MetaItemKind::List(nmis)
}
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => {
// This is an inert key-value attribute - it will never be visible to macros
// after it gets lowered to the `Hir` variant. Therefore, we can extract literals to handle
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
AttrArgs::Eq(span, AttrArgsEq::Ast(expr)) => {
let res = match expr.kind {
ast::ExprKind::Lit(token_lit) => {
let res = ast::MetaItemLit::from_token_lit(token_lit, expr.span);
Expand Down Expand Up @@ -140,18 +143,18 @@ pub fn parse_meta(psess: &ParseSess, attr: &mut Attribute) -> Result<MetaItem, E
Err(psess.dcx().span_err(expr.span, msg))
}
};
MetaItemKind::NameValue(match res {
let lit = match res {
Ok(lit) => lit,
Err(guar) => {
expr.kind = ast::ExprKind::Err(guar);
ast::MetaItemLit {
symbol: Symbol::intern(""),
suffix: None,
kind: ast::LitKind::Err(guar),
span: expr.span,
}
}
})
Err(guar) => ast::MetaItemLit {
symbol: Symbol::intern(""),
suffix: None,
kind: ast::LitKind::Err(guar),
span: expr.span,
},
};

item.args = AttrArgs::Eq(*span, AttrArgsEq::Hir(lit.clone()));
MetaItemKind::NameValue(lit)
}
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => MetaItemKind::NameValue(lit.clone()),
},
Expand Down

0 comments on commit e89df61

Please sign in to comment.