Skip to content

Commit

Permalink
XXX: Const/Constness
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Aug 10, 2022
1 parent e71f89d commit 300d74d
Show file tree
Hide file tree
Showing 21 changed files with 173 additions and 133 deletions.
35 changes: 21 additions & 14 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2346,7 +2346,7 @@ impl Async {

#[derive(Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable, Debug)]
#[derive(HashStable_Generic)]
pub enum Const {
pub enum Constness {
Yes(Span),
No,
}
Expand Down Expand Up @@ -2713,7 +2713,7 @@ impl Extern {
pub struct FnHeader {
pub unsafety: Unsafe,
pub asyncness: Async,
pub constness: Const,
pub constness: Constness,
pub ext: Extern,
}

Expand All @@ -2723,7 +2723,7 @@ impl FnHeader {
let Self { unsafety, asyncness, constness, ext } = self;
matches!(unsafety, Unsafe::Yes(_))
|| asyncness.is_async()
|| matches!(constness, Const::Yes(_))
|| matches!(constness, Constness::Yes(_))
|| !matches!(ext, Extern::None)
}
}
Expand All @@ -2733,7 +2733,7 @@ impl Default for FnHeader {
FnHeader {
unsafety: Unsafe::No,
asyncness: Async::No,
constness: Const::No,
constness: Constness::No,
ext: Extern::None,
}
}
Expand Down Expand Up @@ -2788,7 +2788,7 @@ pub struct Impl {
pub defaultness: Defaultness,
pub unsafety: Unsafe,
pub generics: Generics,
pub constness: Const,
pub constness: Constness,
pub polarity: ImplPolarity,
/// The trait being implemented, if any.
pub of_trait: Option<TraitRef>,
Expand Down Expand Up @@ -2822,6 +2822,13 @@ pub struct TraitAlias {
pub bounds: GenericBounds,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Const {
pub defaultness: Defaultness,
pub ty: P<Ty>,
pub expr: Option<P<Expr>>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ItemKind {
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
Expand All @@ -2839,7 +2846,7 @@ pub enum ItemKind {
/// A constant item (`const`).
///
/// E.g., `const FOO: i32 = 42;`.
Const(Defaultness, P<Ty>, Option<P<Expr>>),
Const(Box<Const>),
/// A function declaration (`fn`).
///
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
Expand Down Expand Up @@ -2955,7 +2962,7 @@ pub type AssocItem = Item<AssocItemKind>;
pub enum AssocItemKind {
/// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
/// If `def` is parsed, then the constant is provided, and otherwise required.
Const(Defaultness, P<Ty>, Option<P<Expr>>),
Const(Box<Const>),
/// An associated function.
Fn(Box<Fn>),
/// An associated type.
Expand All @@ -2967,7 +2974,7 @@ pub enum AssocItemKind {
impl AssocItemKind {
pub fn defaultness(&self) -> Defaultness {
match *self {
Self::Const(defaultness, ..)
Self::Const(box Const { defaultness, .. })
| Self::Fn(box Fn { defaultness, .. })
| Self::TyAlias(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) => Defaultness::Final,
Expand All @@ -2978,7 +2985,7 @@ impl AssocItemKind {
impl From<AssocItemKind> for ItemKind {
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
match assoc_item_kind {
AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
AssocItemKind::Const(const_) => ItemKind::Const(const_),
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
AssocItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
Expand All @@ -2991,7 +2998,7 @@ impl TryFrom<ItemKind> for AssocItemKind {

fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
Ok(match item_kind {
ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
ItemKind::Const(const_) => AssocItemKind::Const(const_),
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_alias_kind) => AssocItemKind::TyAlias(ty_alias_kind),
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
Expand Down Expand Up @@ -3046,8 +3053,8 @@ mod size_asserts {
use super::*;
use rustc_data_structures::static_assert_size;
// These are in alphabetical order, which is easy to maintain.
static_assert_size!(AssocItem, 120);
static_assert_size!(AssocItemKind, 32);
static_assert_size!(AssocItem, 104);
static_assert_size!(AssocItemKind, 16);
static_assert_size!(Attribute, 152);
static_assert_size!(Block, 48);
static_assert_size!(Expr, 104);
Expand All @@ -3057,8 +3064,8 @@ mod size_asserts {
static_assert_size!(GenericBound, 88);
static_assert_size!(Generics, 72);
static_assert_size!(Impl, 200);
static_assert_size!(Item, 120);
static_assert_size!(ItemKind, 32);
static_assert_size!(Item, 112);
static_assert_size!(ItemKind, 24);
static_assert_size!(Lit, 48);
static_assert_size!(Pat, 120);
static_assert_size!(Path, 40);
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,10 +822,10 @@ pub fn visit_polarity<T: MutVisitor>(polarity: &mut ImplPolarity, vis: &mut T) {
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_constness<T: MutVisitor>(constness: &mut Const, vis: &mut T) {
pub fn visit_constness<T: MutVisitor>(constness: &mut Constness, vis: &mut T) {
match constness {
Const::Yes(span) => vis.visit_span(span),
Const::No => {}
Constness::Yes(span) => vis.visit_span(span),
Constness::No => {}
}
}

Expand Down Expand Up @@ -1014,7 +1014,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
ItemKind::Const(defaultness, ty, expr) => {
ItemKind::Const(box Const { defaultness, ty, expr }) => {
visit_defaultness(defaultness, vis);
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
Expand Down Expand Up @@ -1101,7 +1101,7 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
AssocItemKind::Const(defaultness, ty, expr) => {
AssocItemKind::Const(box Const { defaultness, ty, expr }) => {
visit_defaultness(defaultness, visitor);
visitor.visit_ty(ty);
visit_opt(expr, |expr| visitor.visit_expr(expr));
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
match item.kind {
ItemKind::ExternCrate(_) => {}
ItemKind::Use(ref use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
ItemKind::Static(ref typ, _, ref expr) | ItemKind::Const(_, ref typ, ref expr) => {
visitor.visit_ty(typ);
ItemKind::Static(ref ty, _, ref expr)
| ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
ItemKind::Fn(box Fn { defaultness: _, ref generics, ref sig, ref body }) => {
Expand Down Expand Up @@ -677,7 +678,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
AssocItemKind::Const(_, ty, expr) => {
AssocItemKind::Const(box Const { ty, expr, .. }) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
hir::ItemKind::Static(ty, m, body_id)
}
ItemKind::Const(_, ref t, ref e) => {
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
ItemKind::Const(box Const { ref ty, ref expr, .. }) => {
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
hir::ItemKind::Const(ty, body_id)
}
ItemKind::Fn(box Fn {
Expand Down Expand Up @@ -753,9 +753,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
let trait_item_def_id = hir_id.expect_owner();

let (generics, kind, has_default) = match i.kind {
AssocItemKind::Const(_, ref ty, ref default) => {
AssocItemKind::Const(box Const { ref ty, ref expr, .. }) => {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
}
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
Expand Down Expand Up @@ -849,7 +849,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);

let (generics, kind) = match &i.kind {
AssocItemKind::Const(_, ty, expr) => {
AssocItemKind::Const(box Const { ty, expr, .. }) => {
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
(
hir::Generics::empty(),
Expand Down Expand Up @@ -1271,10 +1271,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
}
}

fn lower_constness(&mut self, c: Const) -> hir::Constness {
fn lower_constness(&mut self, c: Constness) -> hir::Constness {
match c {
Const::Yes(_) => hir::Constness::Const,
Const::No => hir::Constness::NotConst,
Constness::Yes(_) => hir::Constness::Const,
Constness::No => hir::Constness::NotConst,
}
}

Expand Down
30 changes: 16 additions & 14 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ impl<'a> AstValidator<'a> {
fn with_in_trait_impl(
&mut self,
is_in: bool,
constness: Option<Const>,
constness: Option<Constness>,
f: impl FnOnce(&mut Self),
) {
let old = mem::replace(&mut self.in_trait_impl, is_in);
let old_const =
mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_))));
let old_const = mem::replace(
&mut self.in_const_trait_impl,
matches!(constness, Some(Constness::Yes(_))),
);
f(self);
self.in_trait_impl = old;
self.in_const_trait_impl = old_const;
Expand Down Expand Up @@ -324,8 +326,8 @@ impl<'a> AstValidator<'a> {
}
}

fn check_trait_fn_not_const(&self, constness: Const) {
if let Const::Yes(span) = constness {
fn check_trait_fn_not_const(&self, constness: Constness) {
if let Constness::Yes(span) = constness {
struct_span_err!(
self.session,
span,
Expand Down Expand Up @@ -1136,7 +1138,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

this.visit_vis(&item.vis);
this.visit_ident(item.ident);
if let Const::Yes(_) = constness {
if let Constness::Yes(_) = constness {
this.with_tilde_const_allowed(|this| this.visit_generics(generics));
} else {
this.visit_generics(generics);
Expand Down Expand Up @@ -1183,7 +1185,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
.note("only trait implementations may be annotated with `default`")
.emit();
}
if let Const::Yes(span) = constness {
if let Constness::Yes(span) = constness {
error(span, "`const`")
.note("only trait implementations may be annotated with `const`")
.emit();
Expand Down Expand Up @@ -1327,8 +1329,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
_ => {}
}
}
ItemKind::Const(def, .., None) => {
self.check_defaultness(item.span, def);
ItemKind::Const(box Const { defaultness, expr: None, .. }) => {
self.check_defaultness(item.span, defaultness);
let msg = "free constant item without body";
self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
}
Expand Down Expand Up @@ -1559,7 +1561,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

// Functions cannot both be `const async`
if let Some(FnHeader {
constness: Const::Yes(cspan),
constness: Constness::Yes(cspan),
asyncness: Async::Yes { span: aspan, .. },
..
}) = fk.header()
Expand Down Expand Up @@ -1628,7 +1630,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
}

let tilde_const_allowed =
matches!(fk.header(), Some(FnHeader { constness: Const::Yes(_), .. }))
matches!(fk.header(), Some(FnHeader { constness: Constness::Yes(_), .. }))
|| matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)));

self.with_tilde_const(tilde_const_allowed, |this| visit::walk_fn(this, fk, span));
Expand All @@ -1645,8 +1647,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

if ctxt == AssocCtxt::Impl {
match &item.kind {
AssocItemKind::Const(_, _, body) => {
self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
AssocItemKind::Const(box Const { expr, .. }) => {
self.check_impl_item_provided(item.span, expr, "constant", " = <expr>;");
}
AssocItemKind::Fn(box Fn { body, .. }) => {
self.check_impl_item_provided(item.span, body, "function", " { <body> }");
Expand Down Expand Up @@ -1701,7 +1703,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
AssocItemKind::Fn(box Fn { ref sig, ref generics, ref body, .. })
if self.in_const_trait_impl
|| ctxt == AssocCtxt::Trait
|| matches!(sig.header.constness, Const::Yes(_)) =>
|| matches!(sig.header.constness, Constness::Yes(_)) =>
{
self.visit_vis(&item.vis);
self.visit_ident(item.ident);
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ struct PostExpansionVisitor<'a> {
}

impl<'a> PostExpansionVisitor<'a> {
fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) {
fn check_abi(&self, abi: ast::StrLit, constness: ast::Constness) {
let ast::StrLit { symbol_unescaped, span, .. } = abi;

if let ast::Const::Yes(_) = constness {
if let ast::Constness::Yes(_) = constness {
match symbol_unescaped {
// Stable
sym::Rust | sym::C => {}
Expand Down Expand Up @@ -284,7 +284,7 @@ impl<'a> PostExpansionVisitor<'a> {
}
}

fn check_extern(&self, ext: ast::Extern, constness: ast::Const) {
fn check_extern(&self, ext: ast::Extern, constness: ast::Constness) {
if let ast::Extern::Explicit(abi, _) = ext {
self.check_abi(abi, constness);
}
Expand Down Expand Up @@ -433,7 +433,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
match i.kind {
ast::ItemKind::ForeignMod(ref foreign_module) => {
if let Some(abi) = foreign_module.abi {
self.check_abi(abi, ast::Const::No);
self.check_abi(abi, ast::Constness::No);
}
}

Expand Down Expand Up @@ -557,7 +557,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
match ty.kind {
ast::TyKind::BareFn(ref bare_fn_ty) => {
// Function pointers cannot be `const`
self.check_extern(bare_fn_ty.ext, ast::Const::No);
self.check_extern(bare_fn_ty.ext, ast::Constness::No);
}
ast::TyKind::Never => {
gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,10 +1754,10 @@ impl<'a> State<'a> {
}
}

pub(crate) fn print_constness(&mut self, s: ast::Const) {
pub(crate) fn print_constness(&mut self, s: ast::Constness) {
match s {
ast::Const::No => {}
ast::Const::Yes(_) => self.word_nbsp("const"),
ast::Constness::No => {}
ast::Constness::Yes(_) => self.word_nbsp("const"),
}
}

Expand Down
Loading

0 comments on commit 300d74d

Please sign in to comment.