Skip to content

Commit

Permalink
Allow ast validation to mutate the ast (inserting error nodes in case…
Browse files Browse the repository at this point in the history
… of validation failures)
  • Loading branch information
oli-obk committed Jul 9, 2024
1 parent cb2ae24 commit e8d7873
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 188 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3632,6 +3632,7 @@ dependencies = [
"rustc_session",
"rustc_span",
"rustc_target",
"smallvec",
"thin-vec",
]

Expand Down
41 changes: 36 additions & 5 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ fn walk_meta_list_item<T: MutVisitor>(vis: &mut T, li: &mut NestedMetaItem) {
}
}

fn walk_meta_item<T: MutVisitor>(vis: &mut T, mi: &mut MetaItem) {
pub fn walk_meta_item<T: MutVisitor>(vis: &mut T, mi: &mut MetaItem) {
let MetaItem { unsafety: _, path: _, kind, span } = mi;
match kind {
MetaItemKind::Word => {}
Expand Down Expand Up @@ -898,7 +898,7 @@ fn walk_coroutine_kind<T: MutVisitor>(vis: &mut T, coroutine_kind: &mut Coroutin
}
}

fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
pub fn walk_fn<T: MutVisitor>(vis: &mut T, kind: FnKind<'_>) {
match kind {
FnKind::Fn(_ctxt, _ident, FnSig { header, decl, span }, generics, body) => {
// Identifier and visibility are visited as a part of the item.
Expand Down Expand Up @@ -931,7 +931,7 @@ fn walk_fn_ret_ty<T: MutVisitor>(vis: &mut T, fn_ret_ty: &mut FnRetTy) {
}
}

fn walk_param_bound<T: MutVisitor>(vis: &mut T, pb: &mut GenericBound) {
pub fn walk_param_bound<T: MutVisitor>(vis: &mut T, pb: &mut GenericBound) {
match pb {
GenericBound::Trait(ty, _modifier) => vis.visit_poly_trait_ref(ty),
GenericBound::Outlives(lifetime) => walk_lifetime(vis, lifetime),
Expand Down Expand Up @@ -981,11 +981,11 @@ pub fn walk_generic_param<T: MutVisitor>(
smallvec![param]
}

fn walk_label<T: MutVisitor>(vis: &mut T, Label { ident }: &mut Label) {
pub fn walk_label<T: MutVisitor>(vis: &mut T, Label { ident }: &mut Label) {
vis.visit_ident(ident);
}

fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifetime) {
pub fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifetime) {
vis.visit_id(id);
vis.visit_ident(ident);
}
Expand Down Expand Up @@ -1323,6 +1323,14 @@ pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
vis.visit_span(inject_use_span);
}

pub fn walk_assoc_item(
visitor: &mut impl MutVisitor,
item: P<Item<AssocItemKind>>,
ctxt: AssocCtxt,
) -> SmallVec<[P<Item<AssocItemKind>>; 1]> {
walk_item(visitor, item, Some(ctxt))
}

/// Mutates one item, returning the item again.
pub fn walk_item<K: NoopVisitItemKind>(
visitor: &mut impl MutVisitor,
Expand Down Expand Up @@ -1831,3 +1839,26 @@ pub enum FnKind<'a> {
/// E.g., `|x, y| body`.
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),
}

impl FnKind<'_> {
pub fn header(&self) -> Option<&FnHeader> {
match self {
FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
FnKind::Closure(_, _, _) => None,
}
}

pub fn ctxt(&self) -> Option<FnCtxt> {
match *self {
FnKind::Fn(ctxt, ..) => Some(ctxt),
FnKind::Closure(..) => None,
}
}

pub fn decl(&self) -> &FnDecl {
match self {
FnKind::Fn(_, _, sig, _, _) => &sig.decl,
FnKind::Closure(_, decl, _) => decl,
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_ast_passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ rustc_parse = { path = "../rustc_parse" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.12"
# tidy-alphabetical-end
Loading

0 comments on commit e8d7873

Please sign in to comment.