Skip to content

Commit ab4cc44

Browse files
authored
Rollup merge of #127818 - oli-obk:ast_validation_simplifications, r=petrochenkov
Various ast validation simplifications Changes pulled out of #127524 These are needed to make ast validation a mutable visitor, as we can't keep immutable references to the AST around in that case. But I think they are simplifying things in general and can stand on their own
2 parents 862852f + d9f9592 commit ab4cc44

File tree

2 files changed

+27
-71
lines changed

2 files changed

+27
-71
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+26-70
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,20 @@ use std::mem;
3838
use std::ops::{Deref, DerefMut};
3939
use thin_vec::thin_vec;
4040

41-
use crate::errors;
41+
use crate::errors::{self, TildeConstReason};
4242

4343
/// Is `self` allowed semantically as the first parameter in an `FnDecl`?
4444
enum SelfSemantic {
4545
Yes,
4646
No,
4747
}
4848

49-
/// What is the context that prevents using `~const`?
50-
// FIXME(effects): Consider getting rid of this in favor of `errors::TildeConstReason`, they're
51-
// almost identical. This gets rid of an abstraction layer which might be considered bad.
52-
enum DisallowTildeConstContext<'a> {
53-
TraitObject,
54-
Fn(FnKind<'a>),
55-
Trait(Span),
56-
TraitImpl(Span),
57-
Impl(Span),
58-
TraitAssocTy(Span),
59-
TraitImplAssocTy(Span),
60-
InherentAssocTy(Span),
61-
Item,
62-
}
63-
64-
enum TraitOrTraitImpl<'a> {
49+
enum TraitOrTraitImpl {
6550
Trait { span: Span, constness: Option<Span> },
66-
TraitImpl { constness: Const, polarity: ImplPolarity, trait_ref: &'a TraitRef },
51+
TraitImpl { constness: Const, polarity: ImplPolarity, trait_ref: Span },
6752
}
6853

69-
impl<'a> TraitOrTraitImpl<'a> {
54+
impl TraitOrTraitImpl {
7055
fn constness(&self) -> Option<Span> {
7156
match self {
7257
Self::Trait { constness: Some(span), .. }
@@ -81,9 +66,9 @@ struct AstValidator<'a> {
8166
features: &'a Features,
8267

8368
/// The span of the `extern` in an `extern { ... }` block, if any.
84-
extern_mod: Option<&'a Item>,
69+
extern_mod: Option<Span>,
8570

86-
outer_trait_or_trait_impl: Option<TraitOrTraitImpl<'a>>,
71+
outer_trait_or_trait_impl: Option<TraitOrTraitImpl>,
8772

8873
has_proc_macro_decls: bool,
8974

@@ -92,7 +77,7 @@ struct AstValidator<'a> {
9277
/// e.g., `impl Iterator<Item = impl Debug>`.
9378
outer_impl_trait: Option<Span>,
9479

95-
disallow_tilde_const: Option<DisallowTildeConstContext<'a>>,
80+
disallow_tilde_const: Option<TildeConstReason>,
9681

9782
/// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
9883
/// or `Foo::Bar<impl Trait>`
@@ -115,7 +100,7 @@ impl<'a> AstValidator<'a> {
115100
trait_.map(|(constness, polarity, trait_ref)| TraitOrTraitImpl::TraitImpl {
116101
constness,
117102
polarity,
118-
trait_ref,
103+
trait_ref: trait_ref.path.span,
119104
}),
120105
);
121106
f(self);
@@ -145,7 +130,7 @@ impl<'a> AstValidator<'a> {
145130

146131
fn with_tilde_const(
147132
&mut self,
148-
disallowed: Option<DisallowTildeConstContext<'a>>,
133+
disallowed: Option<TildeConstReason>,
149134
f: impl FnOnce(&mut Self),
150135
) {
151136
let old = mem::replace(&mut self.disallow_tilde_const, disallowed);
@@ -224,7 +209,7 @@ impl<'a> AstValidator<'a> {
224209
}
225210
}
226211
TyKind::TraitObject(..) => self
227-
.with_tilde_const(Some(DisallowTildeConstContext::TraitObject), |this| {
212+
.with_tilde_const(Some(TildeConstReason::TraitObject), |this| {
228213
visit::walk_ty(this, t)
229214
}),
230215
TyKind::Path(qself, path) => {
@@ -354,7 +339,7 @@ impl<'a> AstValidator<'a> {
354339
}
355340
}
356341

357-
fn check_trait_fn_not_const(&self, constness: Const, parent: &TraitOrTraitImpl<'a>) {
342+
fn check_trait_fn_not_const(&self, constness: Const, parent: &TraitOrTraitImpl) {
358343
let Const::Yes(span) = constness else {
359344
return;
360345
};
@@ -367,7 +352,7 @@ impl<'a> AstValidator<'a> {
367352
..
368353
} = parent
369354
{
370-
Some(trait_ref.path.span.shrink_to_lo())
355+
Some(trait_ref.shrink_to_lo())
371356
} else {
372357
None
373358
};
@@ -579,7 +564,7 @@ impl<'a> AstValidator<'a> {
579564
}
580565

581566
fn current_extern_span(&self) -> Span {
582-
self.session.source_map().guess_head_span(self.extern_mod.unwrap().span)
567+
self.session.source_map().guess_head_span(self.extern_mod.unwrap())
583568
}
584569

585570
/// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
@@ -980,7 +965,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
980965
this.visit_vis(&item.vis);
981966
this.visit_ident(item.ident);
982967
let disallowed = matches!(constness, Const::No)
983-
.then(|| DisallowTildeConstContext::TraitImpl(item.span));
968+
.then(|| TildeConstReason::TraitImpl { span: item.span });
984969
this.with_tilde_const(disallowed, |this| this.visit_generics(generics));
985970
this.visit_trait_ref(t);
986971
this.visit_ty(self_ty);
@@ -1035,7 +1020,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10351020
this.visit_vis(&item.vis);
10361021
this.visit_ident(item.ident);
10371022
this.with_tilde_const(
1038-
Some(DisallowTildeConstContext::Impl(item.span)),
1023+
Some(TildeConstReason::Impl { span: item.span }),
10391024
|this| this.visit_generics(generics),
10401025
);
10411026
this.visit_ty(self_ty);
@@ -1080,7 +1065,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10801065
}
10811066
ItemKind::ForeignMod(ForeignMod { abi, safety, .. }) => {
10821067
self.with_in_extern_mod(*safety, |this| {
1083-
let old_item = mem::replace(&mut this.extern_mod, Some(item));
1068+
let old_item = mem::replace(&mut this.extern_mod, Some(item.span));
10841069
this.visibility_not_permitted(
10851070
&item.vis,
10861071
errors::VisibilityNotPermittedNote::IndividualForeignItems,
@@ -1154,7 +1139,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11541139
this.visit_ident(item.ident);
11551140
let disallowed = is_const_trait
11561141
.is_none()
1157-
.then(|| DisallowTildeConstContext::Trait(item.span));
1142+
.then(|| TildeConstReason::Trait { span: item.span });
11581143
this.with_tilde_const(disallowed, |this| {
11591144
this.visit_generics(generics);
11601145
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
@@ -1399,40 +1384,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13991384
self.dcx().emit_err(errors::ConstBoundTraitObject { span: trait_ref.span });
14001385
}
14011386
(_, BoundConstness::Maybe(span), BoundPolarity::Positive)
1402-
if let Some(reason) = &self.disallow_tilde_const =>
1387+
if let Some(reason) = self.disallow_tilde_const =>
14031388
{
1404-
let reason = match reason {
1405-
DisallowTildeConstContext::Fn(FnKind::Closure(..)) => {
1406-
errors::TildeConstReason::Closure
1407-
}
1408-
DisallowTildeConstContext::Fn(FnKind::Fn(_, ident, ..)) => {
1409-
errors::TildeConstReason::Function { ident: ident.span }
1410-
}
1411-
&DisallowTildeConstContext::Trait(span) => {
1412-
errors::TildeConstReason::Trait { span }
1413-
}
1414-
&DisallowTildeConstContext::TraitImpl(span) => {
1415-
errors::TildeConstReason::TraitImpl { span }
1416-
}
1417-
&DisallowTildeConstContext::Impl(span) => {
1418-
// FIXME(effects): Consider providing a help message or even a structured
1419-
// suggestion for moving such bounds to the assoc const fns if available.
1420-
errors::TildeConstReason::Impl { span }
1421-
}
1422-
&DisallowTildeConstContext::TraitAssocTy(span) => {
1423-
errors::TildeConstReason::TraitAssocTy { span }
1424-
}
1425-
&DisallowTildeConstContext::TraitImplAssocTy(span) => {
1426-
errors::TildeConstReason::TraitImplAssocTy { span }
1427-
}
1428-
&DisallowTildeConstContext::InherentAssocTy(span) => {
1429-
errors::TildeConstReason::InherentAssocTy { span }
1430-
}
1431-
DisallowTildeConstContext::TraitObject => {
1432-
errors::TildeConstReason::TraitObject
1433-
}
1434-
DisallowTildeConstContext::Item => errors::TildeConstReason::Item,
1435-
};
14361389
self.dcx().emit_err(errors::TildeConstDisallowed { span, reason });
14371390
}
14381391
(
@@ -1569,7 +1522,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15691522
.and_then(TraitOrTraitImpl::constness)
15701523
.is_some();
15711524

1572-
let disallowed = (!tilde_const_allowed).then(|| DisallowTildeConstContext::Fn(fk));
1525+
let disallowed = (!tilde_const_allowed).then(|| match fk {
1526+
FnKind::Fn(_, ident, _, _, _, _) => TildeConstReason::Function { ident: ident.span },
1527+
FnKind::Closure(_, _, _) => TildeConstReason::Closure,
1528+
});
15731529
self.with_tilde_const(disallowed, |this| visit::walk_fn(this, fk));
15741530
}
15751531

@@ -1664,12 +1620,12 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
16641620
AssocItemKind::Type(_) => {
16651621
let disallowed = (!parent_is_const).then(|| match self.outer_trait_or_trait_impl {
16661622
Some(TraitOrTraitImpl::Trait { .. }) => {
1667-
DisallowTildeConstContext::TraitAssocTy(item.span)
1623+
TildeConstReason::TraitAssocTy { span: item.span }
16681624
}
16691625
Some(TraitOrTraitImpl::TraitImpl { .. }) => {
1670-
DisallowTildeConstContext::TraitImplAssocTy(item.span)
1626+
TildeConstReason::TraitImplAssocTy { span: item.span }
16711627
}
1672-
None => DisallowTildeConstContext::InherentAssocTy(item.span),
1628+
None => TildeConstReason::InherentAssocTy { span: item.span },
16731629
});
16741630
self.with_tilde_const(disallowed, |this| {
16751631
this.with_in_trait_impl(None, |this| visit::walk_assoc_item(this, item, ctxt))
@@ -1852,7 +1808,7 @@ pub fn check_crate(
18521808
outer_trait_or_trait_impl: None,
18531809
has_proc_macro_decls: false,
18541810
outer_impl_trait: None,
1855-
disallow_tilde_const: Some(DisallowTildeConstContext::Item),
1811+
disallow_tilde_const: Some(TildeConstReason::Item),
18561812
is_impl_trait_banned: false,
18571813
extern_mod_safety: None,
18581814
lint_buffer: lints,

compiler/rustc_ast_passes/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ pub struct TildeConstDisallowed {
612612
pub reason: TildeConstReason,
613613
}
614614

615-
#[derive(Subdiagnostic)]
615+
#[derive(Subdiagnostic, Copy, Clone)]
616616
pub enum TildeConstReason {
617617
#[note(ast_passes_closure)]
618618
Closure,

0 commit comments

Comments
 (0)