Skip to content

Commit e89df61

Browse files
committed
Erase AttrArgsEq::Ast in ast validation
1 parent 99ded8b commit e89df61

File tree

5 files changed

+23
-40
lines changed

5 files changed

+23
-40
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ build/
5050
/target
5151
/src/bootstrap/target
5252
/src/tools/x/target
53+
/inc-fat/
5354
# Created by default with `src/ci/docker/run.sh`
5455
/obj/
5556
/rustc-ice*

compiler/rustc_ast/src/mut_visit.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,10 @@ fn visit_attr_args<T: MutVisitor>(vis: &mut T, args: &mut AttrArgs) {
404404
vis.visit_expr(expr);
405405
vis.visit_span(eq_span);
406406
}
407-
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
408-
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
407+
AttrArgs::Eq(eq_span, AttrArgsEq::Hir(lit)) => {
408+
vis.visit_span(eq_span);
409+
let MetaItemLit { symbol: _, suffix: _, kind: _, span } = lit;
410+
vis.visit_span(span);
409411
}
410412
}
411413
}

compiler/rustc_ast/src/visit.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1233,9 +1233,7 @@ pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) -
12331233
AttrArgs::Empty => {}
12341234
AttrArgs::Delimited(_args) => {}
12351235
AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => try_visit!(visitor.visit_expr(expr)),
1236-
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
1237-
unreachable!("in literal form when walking mac args eq: {:?}", lit)
1238-
}
1236+
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(_lit)) => {}
12391237
}
12401238
V::Result::output()
12411239
}

compiler/rustc_ast_lowering/src/lib.rs

+2-23
Original file line numberDiff line numberDiff line change
@@ -939,31 +939,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
939939
match args {
940940
AttrArgs::Empty => AttrArgs::Empty,
941941
AttrArgs::Delimited(args) => AttrArgs::Delimited(self.lower_delim_args(args)),
942-
// This is an inert key-value attribute - it will never be visible to macros
943-
// after it gets lowered to HIR. Therefore, we can extract literals to handle
944-
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
945942
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
946-
// In valid code the value always ends up as a single literal. Otherwise, a dummy
947-
// literal suffices because the error is handled elsewhere.
948-
let lit = if let ExprKind::Lit(token_lit) = expr.kind
949-
&& let Ok(lit) = MetaItemLit::from_token_lit(token_lit, expr.span)
950-
{
951-
lit
952-
} else if let ExprKind::Err(guar) = expr.kind {
953-
MetaItemLit {
954-
symbol: kw::Empty,
955-
suffix: None,
956-
kind: LitKind::Err(guar),
957-
span: DUMMY_SP,
958-
}
959-
} else {
960-
span_bug!(*eq_span, "eq attrs can only be literals, but got {expr:#?}")
961-
};
962-
AttrArgs::Eq(*eq_span, AttrArgsEq::Hir(lit))
963-
}
964-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
965-
unreachable!("in literal form when lowering mac args eq: {:?}", lit)
943+
span_bug!(*eq_span, "eq attrs can only be errors, but got {expr:#?}")
966944
}
945+
AttrArgs::Eq(_, AttrArgsEq::Hir(_lit)) => args.clone(),
967946
}
968947
}
969948

compiler/rustc_parse/src/validate_attr.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ pub fn parse_meta(psess: &ParseSess, attr: &mut Attribute) -> Result<MetaItem, E
106106
.map_err(|d| d.emit())?;
107107
MetaItemKind::List(nmis)
108108
}
109-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => {
109+
// This is an inert key-value attribute - it will never be visible to macros
110+
// after it gets lowered to the `Hir` variant. Therefore, we can extract literals to handle
111+
// nonterminals in `#[doc]` (e.g. `#[doc = $e]`).
112+
AttrArgs::Eq(span, AttrArgsEq::Ast(expr)) => {
110113
let res = match expr.kind {
111114
ast::ExprKind::Lit(token_lit) => {
112115
let res = ast::MetaItemLit::from_token_lit(token_lit, expr.span);
@@ -140,18 +143,18 @@ pub fn parse_meta(psess: &ParseSess, attr: &mut Attribute) -> Result<MetaItem, E
140143
Err(psess.dcx().span_err(expr.span, msg))
141144
}
142145
};
143-
MetaItemKind::NameValue(match res {
146+
let lit = match res {
144147
Ok(lit) => lit,
145-
Err(guar) => {
146-
expr.kind = ast::ExprKind::Err(guar);
147-
ast::MetaItemLit {
148-
symbol: Symbol::intern(""),
149-
suffix: None,
150-
kind: ast::LitKind::Err(guar),
151-
span: expr.span,
152-
}
153-
}
154-
})
148+
Err(guar) => ast::MetaItemLit {
149+
symbol: Symbol::intern(""),
150+
suffix: None,
151+
kind: ast::LitKind::Err(guar),
152+
span: expr.span,
153+
},
154+
};
155+
156+
item.args = AttrArgs::Eq(*span, AttrArgsEq::Hir(lit.clone()));
157+
MetaItemKind::NameValue(lit)
155158
}
156159
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => MetaItemKind::NameValue(lit.clone()),
157160
},

0 commit comments

Comments
 (0)