Skip to content

Commit 124c33e

Browse files
committed
parse const trait Trait
1 parent 9c3064e commit 124c33e

File tree

94 files changed

+356
-290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+356
-290
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3637,6 +3637,7 @@ impl Default for FnHeader {
36373637

36383638
#[derive(Clone, Encodable, Decodable, Debug)]
36393639
pub struct Trait {
3640+
pub constness: Const,
36403641
pub safety: Safety,
36413642
pub is_auto: IsAuto,
36423643
pub ident: Ident,

compiler/rustc_ast/src/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,8 @@ macro_rules! common_visitor_and_walkers {
738738
try_visit!(vis.visit_ty(self_ty));
739739
visit_assoc_items(vis, items, AssocCtxt::Impl { of_trait: of_trait.is_some() })
740740
}
741-
ItemKind::Trait(box Trait { safety, is_auto: _, ident, generics, bounds, items }) => {
741+
ItemKind::Trait(box Trait { constness, safety, is_auto: _, ident, generics, bounds, items }) => {
742+
try_visit!(visit_constness(vis, constness));
742743
try_visit!(visit_safety(vis, safety));
743744
try_visit!(vis.visit_ident(ident));
744745
try_visit!(vis.visit_generics(generics));

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
417417
items: new_impl_items,
418418
}))
419419
}
420-
ItemKind::Trait(box Trait { is_auto, safety, ident, generics, bounds, items }) => {
420+
ItemKind::Trait(box Trait {
421+
constness,
422+
is_auto,
423+
safety,
424+
ident,
425+
generics,
426+
bounds,
427+
items,
428+
}) => {
429+
let constness = self.lower_constness(*constness);
421430
let ident = self.lower_ident(*ident);
422431
let (generics, (safety, items, bounds)) = self.lower_generics(
423432
generics,
@@ -435,7 +444,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
435444
(safety, items, bounds)
436445
},
437446
);
438-
hir::ItemKind::Trait(*is_auto, safety, ident, generics, bounds, items)
447+
hir::ItemKind::Trait(constness, *is_auto, safety, ident, generics, bounds, items)
439448
}
440449
ItemKind::TraitAlias(ident, generics, bounds) => {
441450
let ident = self.lower_ident(*ident);

compiler/rustc_ast_passes/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ ast_passes_static_without_body =
240240
ast_passes_tilde_const_disallowed = `[const]` is not allowed here
241241
.closure = closures cannot have `[const]` trait bounds
242242
.function = this function is not `const`, so it cannot have `[const]` trait bounds
243-
.trait = this trait is not a `#[const_trait]`, so it cannot have `[const]` trait bounds
243+
.trait = this trait is not `const`, so it cannot have `[const]` trait bounds
244244
.trait_impl = this impl is not `const`, so it cannot have `[const]` trait bounds
245245
.impl = inherent impls cannot have `[const]` trait bounds
246-
.trait_assoc_ty = associated types in non-`#[const_trait]` traits cannot have `[const]` trait bounds
246+
.trait_assoc_ty = associated types in non-`const` traits cannot have `[const]` trait bounds
247247
.trait_impl_assoc_ty = associated types in non-const impls cannot have `[const]` trait bounds
248248
.inherent_assoc_ty = inherent associated types cannot have `[const]` trait bounds
249249
.object = trait objects cannot have `[const]` trait bounds

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ enum SelfSemantic {
4949
}
5050

5151
enum TraitOrTraitImpl {
52-
Trait { span: Span, constness_span: Option<Span> },
52+
Trait { span: Span, constness: Const },
5353
TraitImpl { constness: Const, polarity: ImplPolarity, trait_ref_span: Span },
5454
}
5555

5656
impl TraitOrTraitImpl {
5757
fn constness(&self) -> Option<Span> {
5858
match self {
59-
Self::Trait { constness_span: Some(span), .. }
59+
Self::Trait { constness: Const::Yes(span), .. }
6060
| Self::TraitImpl { constness: Const::Yes(span), .. } => Some(*span),
6161
_ => None,
6262
}
@@ -110,15 +110,10 @@ impl<'a> AstValidator<'a> {
110110
self.outer_trait_or_trait_impl = old;
111111
}
112112

113-
fn with_in_trait(
114-
&mut self,
115-
span: Span,
116-
constness_span: Option<Span>,
117-
f: impl FnOnce(&mut Self),
118-
) {
113+
fn with_in_trait(&mut self, span: Span, constness: Const, f: impl FnOnce(&mut Self)) {
119114
let old = mem::replace(
120115
&mut self.outer_trait_or_trait_impl,
121-
Some(TraitOrTraitImpl::Trait { span, constness_span }),
116+
Some(TraitOrTraitImpl::Trait { span, constness }),
122117
);
123118
f(self);
124119
self.outer_trait_or_trait_impl = old;
@@ -273,7 +268,7 @@ impl<'a> AstValidator<'a> {
273268
};
274269

275270
let make_trait_const_sugg = if const_trait_impl
276-
&& let TraitOrTraitImpl::Trait { span, constness_span: None } = parent
271+
&& let TraitOrTraitImpl::Trait { span, constness: ast::Const::No } = parent
277272
{
278273
Some(span.shrink_to_lo())
279274
} else {
@@ -1131,10 +1126,23 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11311126
}
11321127
visit::walk_item(self, item)
11331128
}
1134-
ItemKind::Trait(box Trait { is_auto, generics, ident, bounds, items, .. }) => {
1129+
ItemKind::Trait(box Trait {
1130+
constness,
1131+
is_auto,
1132+
generics,
1133+
ident,
1134+
bounds,
1135+
items,
1136+
..
1137+
}) => {
11351138
self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
1136-
let is_const_trait =
1139+
// FIXME(const_trait_impl) remove this
1140+
let alt_const_trait_span =
11371141
attr::find_by_name(&item.attrs, sym::const_trait).map(|attr| attr.span);
1142+
let constness = match (*constness, alt_const_trait_span) {
1143+
(Const::Yes(span), _) | (Const::No, Some(span)) => Const::Yes(span),
1144+
(Const::No, None) => Const::No,
1145+
};
11381146
if *is_auto == IsAuto::Yes {
11391147
// Auto traits cannot have generics, super traits nor contain items.
11401148
self.deny_generic_params(generics, ident.span);
@@ -1145,13 +1153,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
11451153

11461154
// Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
11471155
// context for the supertraits.
1148-
let disallowed =
1149-
is_const_trait.is_none().then(|| TildeConstReason::Trait { span: item.span });
1156+
let disallowed = matches!(constness, ast::Const::No)
1157+
.then(|| TildeConstReason::Trait { span: item.span });
11501158
self.with_tilde_const(disallowed, |this| {
11511159
this.visit_generics(generics);
11521160
walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
11531161
});
1154-
self.with_in_trait(item.span, is_const_trait, |this| {
1162+
self.with_in_trait(item.span, constness, |this| {
11551163
walk_list!(this, visit_assoc_item, items, AssocCtxt::Trait);
11561164
});
11571165
}

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ pub(crate) struct ConstBoundTraitObject {
590590
}
591591

592592
// FIXME(const_trait_impl): Consider making the note/reason the message of the diagnostic.
593-
// FIXME(const_trait_impl): Provide structured suggestions (e.g., add `const` / `#[const_trait]` here).
593+
// FIXME(const_trait_impl): Provide structured suggestions (e.g., add `const` here).
594594
#[derive(Diagnostic)]
595595
#[diag(ast_passes_tilde_const_disallowed)]
596596
pub(crate) struct TildeConstDisallowed {

compiler/rustc_ast_pretty/src/pprust/state/item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ impl<'a> State<'a> {
357357
self.bclose(item.span, empty, cb);
358358
}
359359
ast::ItemKind::Trait(box ast::Trait {
360+
constness,
360361
safety,
361362
is_auto,
362363
ident,
@@ -366,6 +367,7 @@ impl<'a> State<'a> {
366367
}) => {
367368
let (cb, ib) = self.head("");
368369
self.print_visibility(&item.vis);
370+
self.print_constness(*constness);
369371
self.print_safety(*safety);
370372
self.print_is_auto(*is_auto);
371373
self.word_nbsp("trait");

compiler/rustc_attr_parsing/src/attributes/traits.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl<S: Stage> NoArgsAttributeParser<S> for DoNotImplementViaObjectParser {
9191
const CREATE: fn(Span) -> AttributeKind = AttributeKind::DoNotImplementViaObject;
9292
}
9393

94+
// FIXME(const_trait_impl): remove this
9495
// Const traits
9596

9697
pub(crate) struct ConstTraitParser;

compiler/rustc_const_eval/src/check_consts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub fn rustc_allow_const_fn_unstable(
9393
/// world into two functions: those that are safe to expose on stable (and hence may not use
9494
/// unstable features, not even recursively), and those that are not.
9595
pub fn is_fn_or_trait_safe_to_expose_on_stable(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
96-
// A default body in a `#[const_trait]` is const-stable when the trait is const-stable.
96+
// A default body in a `const trait` is const-stable when the trait is const-stable.
9797
if tcx.is_const_default_method(def_id) {
9898
return is_fn_or_trait_safe_to_expose_on_stable(tcx, tcx.parent(def_id));
9999
}

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
359359
if let ty::InstanceKind::Item(def) = instance.def {
360360
// Execution might have wandered off into other crates, so we cannot do a stability-
361361
// sensitive check here. But we can at least rule out functions that are not const at
362-
// all. That said, we have to allow calling functions inside a trait marked with
363-
// #[const_trait]. These *are* const-checked!
362+
// all. That said, we have to allow calling functions inside a `const trait`. These
363+
// *are* const-checked!
364364
if !ecx.tcx.is_const_fn(def) || ecx.tcx.has_attr(def, sym::rustc_do_not_const_check) {
365365
// We certainly do *not* want to actually call the fn
366366
// though, so be sure we return here.

0 commit comments

Comments
 (0)