Skip to content

Commit abb4640

Browse files
committed
Auto merge of rust-lang#127524 - oli-obk:feed_item_attrs2, r=<try>
Make ast `MutVisitor` have the same method name and style as `Visitor` It doesn't map 100% because some `MutVisitor` methods can filter or even expand to multiple items, but consistency seems nicer. The last commit showcases how similar they are by changing ast validation to a `MutVisitor` (without actually doing any mutation yet). My plan is to replace all nodes that support it with error nodes if validation failed on them. This allows ast lowering to just assume things are error or valid, and avoids having to redo some checks, delaying bugs or checking the global error counter. tracking issue: rust-lang#127615
2 parents 44fb857 + e89df61 commit abb4640

File tree

28 files changed

+934
-849
lines changed

28 files changed

+934
-849
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3638,6 +3638,7 @@ dependencies = [
36383638
"rustc_session",
36393639
"rustc_span",
36403640
"rustc_target",
3641+
"smallvec",
36413642
"thin-vec",
36423643
]
36433644

compiler/rustc_ast/src/ast.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -1883,9 +1883,14 @@ pub enum LitKind {
18831883

18841884
impl LitKind {
18851885
pub fn str(&self) -> Option<Symbol> {
1886+
self.try_str().ok()?
1887+
}
1888+
1889+
pub fn try_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
18861890
match *self {
1887-
LitKind::Str(s, _) => Some(s),
1888-
_ => None,
1891+
LitKind::Str(s, _) => Ok(Some(s)),
1892+
LitKind::Err(guar) => Err(guar),
1893+
_ => Ok(None),
18891894
}
18901895
}
18911896

@@ -2827,6 +2832,15 @@ pub enum AttrKind {
28272832
DocComment(CommentKind, Symbol),
28282833
}
28292834

2835+
impl AttrKind {
2836+
pub fn normal_mut(&mut self) -> &mut AttrItem {
2837+
match self {
2838+
AttrKind::Normal(normal) => &mut normal.item,
2839+
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
2840+
}
2841+
}
2842+
}
2843+
28302844
#[derive(Clone, Encodable, Decodable, Debug)]
28312845
pub struct NormalAttr {
28322846
pub item: AttrItem,

compiler/rustc_ast/src/attr/mod.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::util::comments;
1414
use crate::util::literal::escape_string_symbol;
1515
use rustc_index::bit_set::GrowableBitSet;
1616
use rustc_span::symbol::{sym, Ident, Symbol};
17-
use rustc_span::Span;
17+
use rustc_span::{ErrorGuaranteed, Span};
1818
use smallvec::{smallvec, SmallVec};
1919
use std::iter;
2020
use std::sync::atomic::{AtomicU32, Ordering};
@@ -144,9 +144,13 @@ impl Attribute {
144144
}
145145

146146
pub fn value_str(&self) -> Option<Symbol> {
147+
self.try_value_str().ok()?
148+
}
149+
150+
pub fn try_value_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
147151
match &self.kind {
148-
AttrKind::Normal(normal) => normal.item.value_str(),
149-
AttrKind::DocComment(..) => None,
152+
AttrKind::Normal(normal) => normal.item.try_value_str(),
153+
AttrKind::DocComment(..) => Ok(None),
150154
}
151155
}
152156

@@ -236,9 +240,13 @@ impl AttrItem {
236240
}
237241

238242
fn value_str(&self) -> Option<Symbol> {
243+
self.try_value_str().ok()?
244+
}
245+
246+
fn try_value_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
239247
match &self.args {
240248
AttrArgs::Eq(_, args) => args.value_str(),
241-
AttrArgs::Delimited(_) | AttrArgs::Empty => None,
249+
AttrArgs::Delimited(_) | AttrArgs::Empty => Ok(None),
242250
}
243251
}
244252

@@ -257,15 +265,17 @@ impl AttrItem {
257265
}
258266

259267
impl AttrArgsEq {
260-
fn value_str(&self) -> Option<Symbol> {
268+
fn value_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
261269
match self {
262270
AttrArgsEq::Ast(expr) => match expr.kind {
263-
ExprKind::Lit(token_lit) => {
264-
LitKind::from_token_lit(token_lit).ok().and_then(|lit| lit.str())
265-
}
266-
_ => None,
271+
ExprKind::Lit(token_lit) => LitKind::from_token_lit(token_lit)
272+
.ok()
273+
.and_then(|lit| lit.try_str().transpose())
274+
.transpose(),
275+
ExprKind::Err(guar) => Err(guar),
276+
_ => Ok(None),
267277
},
268-
AttrArgsEq::Hir(lit) => lit.kind.str(),
278+
AttrArgsEq::Hir(lit) => lit.kind.try_str(),
269279
}
270280
}
271281
}

0 commit comments

Comments
 (0)