Skip to content

Commit

Permalink
Auto merge of rust-lang#127524 - oli-obk:feed_item_attrs2, r=<try>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jul 13, 2024
2 parents 44fb857 + e89df61 commit abb4640
Show file tree
Hide file tree
Showing 28 changed files with 934 additions and 849 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3638,6 +3638,7 @@ dependencies = [
"rustc_session",
"rustc_span",
"rustc_target",
"smallvec",
"thin-vec",
]

Expand Down
18 changes: 16 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,9 +1883,14 @@ pub enum LitKind {

impl LitKind {
pub fn str(&self) -> Option<Symbol> {
self.try_str().ok()?
}

pub fn try_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
match *self {
LitKind::Str(s, _) => Some(s),
_ => None,
LitKind::Str(s, _) => Ok(Some(s)),
LitKind::Err(guar) => Err(guar),
_ => Ok(None),
}
}

Expand Down Expand Up @@ -2827,6 +2832,15 @@ pub enum AttrKind {
DocComment(CommentKind, Symbol),
}

impl AttrKind {
pub fn normal_mut(&mut self) -> &mut AttrItem {
match self {
AttrKind::Normal(normal) => &mut normal.item,
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
}
}
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct NormalAttr {
pub item: AttrItem,
Expand Down
30 changes: 20 additions & 10 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::util::comments;
use crate::util::literal::escape_string_symbol;
use rustc_index::bit_set::GrowableBitSet;
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::Span;
use rustc_span::{ErrorGuaranteed, Span};
use smallvec::{smallvec, SmallVec};
use std::iter;
use std::sync::atomic::{AtomicU32, Ordering};
Expand Down Expand Up @@ -144,9 +144,13 @@ impl Attribute {
}

pub fn value_str(&self) -> Option<Symbol> {
self.try_value_str().ok()?
}

pub fn try_value_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
match &self.kind {
AttrKind::Normal(normal) => normal.item.value_str(),
AttrKind::DocComment(..) => None,
AttrKind::Normal(normal) => normal.item.try_value_str(),
AttrKind::DocComment(..) => Ok(None),
}
}

Expand Down Expand Up @@ -236,9 +240,13 @@ impl AttrItem {
}

fn value_str(&self) -> Option<Symbol> {
self.try_value_str().ok()?
}

fn try_value_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
match &self.args {
AttrArgs::Eq(_, args) => args.value_str(),
AttrArgs::Delimited(_) | AttrArgs::Empty => None,
AttrArgs::Delimited(_) | AttrArgs::Empty => Ok(None),
}
}

Expand All @@ -257,15 +265,17 @@ impl AttrItem {
}

impl AttrArgsEq {
fn value_str(&self) -> Option<Symbol> {
fn value_str(&self) -> Result<Option<Symbol>, ErrorGuaranteed> {
match self {
AttrArgsEq::Ast(expr) => match expr.kind {
ExprKind::Lit(token_lit) => {
LitKind::from_token_lit(token_lit).ok().and_then(|lit| lit.str())
}
_ => None,
ExprKind::Lit(token_lit) => LitKind::from_token_lit(token_lit)
.ok()
.and_then(|lit| lit.try_str().transpose())
.transpose(),
ExprKind::Err(guar) => Err(guar),
_ => Ok(None),
},
AttrArgsEq::Hir(lit) => lit.kind.str(),
AttrArgsEq::Hir(lit) => lit.kind.try_str(),
}
}
}
Expand Down
Loading

0 comments on commit abb4640

Please sign in to comment.