Skip to content

Commit d9f9592

Browse files
committed
Remove a boilerplaty abstraction
1 parent b879e29 commit d9f9592

File tree

2 files changed

+17
-61
lines changed

2 files changed

+17
-61
lines changed

Diff for: compiler/rustc_ast_passes/src/ast_validation.rs

+16-60
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,14 @@ 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-
6449
enum TraitOrTraitImpl {
6550
Trait { span: Span, constness: Option<Span> },
6651
TraitImpl { constness: Const, polarity: ImplPolarity, trait_ref: Span },
@@ -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>`
@@ -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) => {
@@ -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);
@@ -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,

Diff for: 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)