1
Fork 0

Give inline const separate DefKind

This commit is contained in:
Gary Guo 2021-10-02 12:59:26 +01:00
parent 089a016919
commit 02c1774cd3
16 changed files with 46 additions and 15 deletions

View file

@ -42,6 +42,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
| DefKind::Static | DefKind::Static
| DefKind::ConstParam | DefKind::ConstParam
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocConst | DefKind::AssocConst
), ),
"Unexpected DefKind: {:?}", "Unexpected DefKind: {:?}",

View file

@ -104,8 +104,10 @@ pub enum DefKind {
Use, Use,
/// An `extern` block. /// An `extern` block.
ForeignMod, ForeignMod,
/// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`, or `const { 1 + 2}` /// Anonymous constant, e.g. the `1 + 2` in `[u8; 1 + 2]`
AnonConst, AnonConst,
/// An inline constant, e.g. `const { 1 + 2 }`
InlineConst,
/// Opaque type, aka `impl Trait`. /// Opaque type, aka `impl Trait`.
OpaqueTy, OpaqueTy,
Field, Field,
@ -155,6 +157,7 @@ impl DefKind {
DefKind::Use => "import", DefKind::Use => "import",
DefKind::ForeignMod => "foreign module", DefKind::ForeignMod => "foreign module",
DefKind::AnonConst => "constant expression", DefKind::AnonConst => "constant expression",
DefKind::InlineConst => "inline constant",
DefKind::Field => "field", DefKind::Field => "field",
DefKind::Impl => "implementation", DefKind::Impl => "implementation",
DefKind::Closure => "closure", DefKind::Closure => "closure",
@ -174,6 +177,7 @@ impl DefKind {
| DefKind::OpaqueTy | DefKind::OpaqueTy
| DefKind::Impl | DefKind::Impl
| DefKind::Use | DefKind::Use
| DefKind::InlineConst
| DefKind::ExternCrate => "an", | DefKind::ExternCrate => "an",
DefKind::Macro(macro_kind) => macro_kind.article(), DefKind::Macro(macro_kind) => macro_kind.article(),
_ => "a", _ => "a",
@ -207,6 +211,7 @@ impl DefKind {
// Not namespaced. // Not namespaced.
DefKind::AnonConst DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field | DefKind::Field
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::ExternCrate | DefKind::ExternCrate

View file

@ -797,6 +797,7 @@ fn should_encode_visibility(def_kind: DefKind) -> bool {
| DefKind::ConstParam | DefKind::ConstParam
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm | DefKind::GlobalAsm
| DefKind::Closure | DefKind::Closure
| DefKind::Generator | DefKind::Generator
@ -832,6 +833,7 @@ fn should_encode_stability(def_kind: DefKind) -> bool {
DefKind::Use DefKind::Use
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm | DefKind::GlobalAsm
| DefKind::Closure | DefKind::Closure
| DefKind::Generator | DefKind::Generator
@ -856,9 +858,11 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
(true, mir_opt_base) (true, mir_opt_base)
} }
// Constants // Constants
DefKind::AnonConst | DefKind::AssocConst | DefKind::Static | DefKind::Const => { DefKind::AnonConst
(true, false) | DefKind::InlineConst
} | DefKind::AssocConst
| DefKind::Static
| DefKind::Const => (true, false),
// Full-fledged functions // Full-fledged functions
DefKind::AssocFn | DefKind::Fn => { DefKind::AssocFn | DefKind::Fn => {
let generics = tcx.generics_of(def_id); let generics = tcx.generics_of(def_id);
@ -914,6 +918,7 @@ fn should_encode_variances(def_kind: DefKind) -> bool {
| DefKind::Use | DefKind::Use
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::GlobalAsm | DefKind::GlobalAsm
| DefKind::Closure | DefKind::Closure
| DefKind::Generator | DefKind::Generator
@ -939,6 +944,7 @@ fn should_encode_generics(def_kind: DefKind) -> bool {
| DefKind::AssocFn | DefKind::AssocFn
| DefKind::AssocConst | DefKind::AssocConst
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy | DefKind::OpaqueTy
| DefKind::Impl | DefKind::Impl
| DefKind::Field | DefKind::Field

View file

@ -266,7 +266,15 @@ impl<'hir> Map<'hir> {
}; };
DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data)) DefKind::Ctor(ctor_of, def::CtorKind::from_hir(variant_data))
} }
Node::AnonConst(_) => DefKind::AnonConst, Node::AnonConst(_) => {
let inline = match self.find(self.get_parent_node(hir_id)) {
Some(Node::Expr(&Expr {
kind: ExprKind::ConstBlock(ref anon_const), ..
})) if anon_const.hir_id == hir_id => true,
_ => false,
};
if inline { DefKind::InlineConst } else { DefKind::AnonConst }
}
Node::Field(_) => DefKind::Field, Node::Field(_) => DefKind::Field,
Node::Expr(expr) => match expr.kind { Node::Expr(expr) => match expr.kind {
ExprKind::Closure(.., None) => DefKind::Closure, ExprKind::Closure(.., None) => DefKind::Closure,

View file

@ -958,7 +958,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res
write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })? write!(w, "static {}", if tcx.is_mutable_static(def_id) { "mut " } else { "" })?
} }
(_, _) if is_function => write!(w, "fn ")?, (_, _) if is_function => write!(w, "fn ")?,
(DefKind::AnonConst, _) => {} // things like anon const, not an item (DefKind::AnonConst | DefKind::InlineConst, _) => {} // things like anon const, not an item
_ => bug!("Unexpected def kind {:?}", kind), _ => bug!("Unexpected def kind {:?}", kind),
} }

View file

@ -1927,7 +1927,8 @@ impl<'tcx> TyCtxt<'tcx> {
| DefKind::Static | DefKind::Static
| DefKind::AssocConst | DefKind::AssocConst
| DefKind::Ctor(..) | DefKind::Ctor(..)
| DefKind::AnonConst => self.mir_for_ctfe_opt_const_arg(def), | DefKind::AnonConst
| DefKind::InlineConst => self.mir_for_ctfe_opt_const_arg(def),
// If the caller wants `mir_for_ctfe` of a function they should not be using // If the caller wants `mir_for_ctfe` of a function they should not be using
// `instance_mir`, so we'll assume const fn also wants the optimized version. // `instance_mir`, so we'll assume const fn also wants the optimized version.
_ => { _ => {

View file

@ -167,6 +167,7 @@ fn mark_used_by_default_parameters<'tcx>(
| DefKind::Use | DefKind::Use
| DefKind::ForeignMod | DefKind::ForeignMod
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy | DefKind::OpaqueTy
| DefKind::Field | DefKind::Field
| DefKind::LifetimeParam | DefKind::LifetimeParam
@ -303,7 +304,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for MarkUsedGenericParams<'a, 'tcx> {
ControlFlow::CONTINUE ControlFlow::CONTINUE
} }
ty::ConstKind::Unevaluated(uv) ty::ConstKind::Unevaluated(uv)
if self.tcx.def_kind(uv.def.did) == DefKind::AnonConst => if matches!(self.tcx.def_kind(uv.def.did), DefKind::AnonConst | DefKind::InlineConst) =>
{ {
self.visit_child_body(uv.def.did, uv.substs(self.tcx)); self.visit_child_body(uv.def.did, uv.substs(self.tcx));
ControlFlow::CONTINUE ControlFlow::CONTINUE

View file

@ -618,6 +618,7 @@ impl EmbargoVisitor<'tcx> {
| DefKind::Use | DefKind::Use
| DefKind::ForeignMod | DefKind::ForeignMod
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field | DefKind::Field
| DefKind::GlobalAsm | DefKind::GlobalAsm
| DefKind::Impl | DefKind::Impl

View file

@ -967,6 +967,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
| DefKind::Use | DefKind::Use
| DefKind::ForeignMod | DefKind::ForeignMod
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field | DefKind::Field
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::GlobalAsm | DefKind::GlobalAsm

View file

@ -540,7 +540,7 @@ fn is_late_bound_map<'tcx>(
def_id: LocalDefId, def_id: LocalDefId,
) -> Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> { ) -> Option<(LocalDefId, &'tcx FxHashSet<ItemLocalId>)> {
match tcx.def_kind(def_id) { match tcx.def_kind(def_id) {
DefKind::AnonConst => { DefKind::AnonConst | DefKind::InlineConst => {
let mut def_id = tcx let mut def_id = tcx
.parent(def_id.to_def_id()) .parent(def_id.to_def_id())
.unwrap_or_else(|| bug!("anon const or closure without a parent")); .unwrap_or_else(|| bug!("anon const or closure without a parent"));

View file

@ -739,6 +739,7 @@ impl<'tcx> SaveContext<'tcx> {
| HirDefKind::ForeignMod | HirDefKind::ForeignMod
| HirDefKind::LifetimeParam | HirDefKind::LifetimeParam
| HirDefKind::AnonConst | HirDefKind::AnonConst
| HirDefKind::InlineConst
| HirDefKind::Use | HirDefKind::Use
| HirDefKind::Field | HirDefKind::Field
| HirDefKind::GlobalAsm | HirDefKind::GlobalAsm

View file

@ -151,7 +151,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
if concrete.is_ok() && uv.substs(infcx.tcx).definitely_has_param_types_or_consts(infcx.tcx) { if concrete.is_ok() && uv.substs(infcx.tcx).definitely_has_param_types_or_consts(infcx.tcx) {
match infcx.tcx.def_kind(uv.def.did) { match infcx.tcx.def_kind(uv.def.did) {
DefKind::AnonConst => { DefKind::AnonConst | DefKind::InlineConst => {
let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def); let mir_body = infcx.tcx.mir_for_ctfe_opt_const_arg(uv.def);
if mir_body.is_polymorphic { if mir_body.is_polymorphic {
@ -495,7 +495,7 @@ pub(super) fn thir_abstract_const<'tcx>(
// we want to look into them or treat them as opaque projections. // we want to look into them or treat them as opaque projections.
// //
// Right now we do neither of that and simply always fail to unify them. // Right now we do neither of that and simply always fail to unify them.
DefKind::AnonConst => (), DefKind::AnonConst | DefKind::InlineConst => (),
_ => return Ok(None), _ => return Ok(None),
} }

View file

@ -430,8 +430,9 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
| Res::NonMacroAttr(_) | Res::NonMacroAttr(_)
| Res::Err => return res.def_id(), | Res::Err => return res.def_id(),
Res::Def( Res::Def(
TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst | OpaqueTy TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst
| Field | LifetimeParam | GlobalAsm | Impl | Closure | Generator, | InlineConst | OpaqueTy | Field | LifetimeParam | GlobalAsm | Impl | Closure
| Generator,
id, id,
) => return id, ) => return id,
}; };

View file

@ -134,6 +134,7 @@ impl From<DefKind> for ItemType {
| DefKind::Use | DefKind::Use
| DefKind::ForeignMod | DefKind::ForeignMod
| DefKind::AnonConst | DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy | DefKind::OpaqueTy
| DefKind::Field | DefKind::Field
| DefKind::LifetimeParam | DefKind::LifetimeParam

View file

@ -1937,7 +1937,8 @@ fn resolution_failure(
| Use | Use
| LifetimeParam | LifetimeParam
| Ctor(_, _) | Ctor(_, _)
| AnonConst => { | AnonConst
| InlineConst => {
let note = assoc_item_not_allowed(res); let note = assoc_item_not_allowed(res);
if let Some(span) = sp { if let Some(span) = sp {
diag.span_label(span, &note); diag.span_label(span, &note);

View file

@ -1065,7 +1065,10 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])
PatKind::Path(path) => { PatKind::Path(path) => {
#[allow(clippy::match_same_arms)] #[allow(clippy::match_same_arms)]
let id = match cx.qpath_res(path, pat.hir_id) { let id = match cx.qpath_res(path, pat.hir_id) {
Res::Def(DefKind::Const | DefKind::ConstParam | DefKind::AnonConst, _) => return, Res::Def(
DefKind::Const | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst,
_,
) => return,
Res::Def(_, id) => id, Res::Def(_, id) => id,
_ => return, _ => return,
}; };