Skip to content

Commit 64c14e7

Browse files
committed
Allow ast validation to mutate the ast (inserting error nodes in case of validation failures)
1 parent 55480fe commit 64c14e7

File tree

5 files changed

+253
-191
lines changed

5 files changed

+253
-191
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,6 +3632,7 @@ dependencies = [
36323632
"rustc_session",
36333633
"rustc_span",
36343634
"rustc_target",
3635+
"smallvec",
36353636
"thin-vec",
36363637
]
36373638

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ fn walk_meta_list_item<T: MutVisitor>(vis: &mut T, li: &mut NestedMetaItem) {
674674
}
675675
}
676676

677-
fn walk_meta_item<T: MutVisitor>(vis: &mut T, mi: &mut MetaItem) {
677+
pub fn walk_meta_item<T: MutVisitor>(vis: &mut T, mi: &mut MetaItem) {
678678
let MetaItem { unsafety: _, path: _, kind, span } = mi;
679679
match kind {
680680
MetaItemKind::Word => {}
@@ -890,7 +890,7 @@ fn walk_coroutine_kind<T: MutVisitor>(vis: &mut T, coroutine_kind: &mut Coroutin
890890
}
891891
}
892892

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

926-
fn walk_param_bound<T: MutVisitor>(vis: &mut T, pb: &mut GenericBound) {
926+
pub fn walk_param_bound<T: MutVisitor>(vis: &mut T, pb: &mut GenericBound) {
927927
match pb {
928928
GenericBound::Trait(ty, _modifier) => vis.visit_poly_trait_ref(ty),
929929
GenericBound::Outlives(lifetime) => walk_lifetime(vis, lifetime),
@@ -973,11 +973,11 @@ pub fn walk_flat_map_generic_param<T: MutVisitor>(
973973
smallvec![param]
974974
}
975975

976-
fn walk_label<T: MutVisitor>(vis: &mut T, Label { ident }: &mut Label) {
976+
pub fn walk_label<T: MutVisitor>(vis: &mut T, Label { ident }: &mut Label) {
977977
vis.visit_ident(ident);
978978
}
979979

980-
fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifetime) {
980+
pub fn walk_lifetime<T: MutVisitor>(vis: &mut T, Lifetime { id, ident }: &mut Lifetime) {
981981
vis.visit_id(id);
982982
vis.visit_ident(ident);
983983
}
@@ -1321,6 +1321,14 @@ pub fn walk_crate<T: MutVisitor>(vis: &mut T, krate: &mut Crate) {
13211321
vis.visit_span(inject_use_span);
13221322
}
13231323

1324+
pub fn walk_flat_map_assoc_item(
1325+
visitor: &mut impl MutVisitor,
1326+
item: P<Item<AssocItemKind>>,
1327+
ctxt: AssocCtxt,
1328+
) -> SmallVec<[P<Item<AssocItemKind>>; 1]> {
1329+
walk_flat_map_item(visitor, item, Some(ctxt))
1330+
}
1331+
13241332
/// Mutates one item, returning the item again.
13251333
pub fn walk_flat_map_item<K: NoopVisitItemKind>(
13261334
visitor: &mut impl MutVisitor,
@@ -1831,3 +1839,26 @@ pub enum FnKind<'a> {
18311839
/// E.g., `|x, y| body`.
18321840
Closure(&'a mut ClosureBinder, &'a mut P<FnDecl>, &'a mut P<Expr>),
18331841
}
1842+
1843+
impl FnKind<'_> {
1844+
pub fn header(&self) -> Option<&FnHeader> {
1845+
match self {
1846+
FnKind::Fn(_, _, sig, _, _) => Some(&sig.header),
1847+
FnKind::Closure(_, _, _) => None,
1848+
}
1849+
}
1850+
1851+
pub fn ctxt(&self) -> Option<FnCtxt> {
1852+
match *self {
1853+
FnKind::Fn(ctxt, ..) => Some(ctxt),
1854+
FnKind::Closure(..) => None,
1855+
}
1856+
}
1857+
1858+
pub fn decl(&self) -> &FnDecl {
1859+
match self {
1860+
FnKind::Fn(_, _, sig, _, _) => &sig.decl,
1861+
FnKind::Closure(_, decl, _) => decl,
1862+
}
1863+
}
1864+
}

compiler/rustc_ast_passes/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ rustc_parse = { path = "../rustc_parse" }
1818
rustc_session = { path = "../rustc_session" }
1919
rustc_span = { path = "../rustc_span" }
2020
rustc_target = { path = "../rustc_target" }
21+
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
2122
thin-vec = "0.2.12"
2223
# tidy-alphabetical-end

0 commit comments

Comments
 (0)