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::ConstParam
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::AssocConst
),
"Unexpected DefKind: {:?}",

View File

@ -104,8 +104,10 @@ pub enum DefKind {
Use,
/// An `extern` block.
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,
/// An inline constant, e.g. `const { 1 + 2 }`
InlineConst,
/// Opaque type, aka `impl Trait`.
OpaqueTy,
Field,
@ -155,6 +157,7 @@ pub fn descr(self, def_id: DefId) -> &'static str {
DefKind::Use => "import",
DefKind::ForeignMod => "foreign module",
DefKind::AnonConst => "constant expression",
DefKind::InlineConst => "inline constant",
DefKind::Field => "field",
DefKind::Impl => "implementation",
DefKind::Closure => "closure",
@ -174,6 +177,7 @@ pub fn article(&self) -> &'static str {
| DefKind::OpaqueTy
| DefKind::Impl
| DefKind::Use
| DefKind::InlineConst
| DefKind::ExternCrate => "an",
DefKind::Macro(macro_kind) => macro_kind.article(),
_ => "a",
@ -207,6 +211,7 @@ pub fn ns(&self) -> Option<Namespace> {
// Not namespaced.
DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::ExternCrate

View File

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

View File

@ -266,7 +266,15 @@ pub fn opt_def_kind(&self, local_def_id: LocalDefId) -> Option<DefKind> {
};
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::Expr(expr) => match expr.kind {
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 { "" })?
}
(_, _) 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),
}

View File

@ -1927,7 +1927,8 @@ pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
| DefKind::Static
| DefKind::AssocConst
| 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
// `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::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::LifetimeParam
@ -303,7 +304,7 @@ fn visit_const(&mut self, c: &'tcx Const<'tcx>) -> ControlFlow<Self::BreakTy> {
ControlFlow::CONTINUE
}
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));
ControlFlow::CONTINUE

View File

@ -618,6 +618,7 @@ fn update_macro_reachable_def(
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field
| DefKind::GlobalAsm
| DefKind::Impl

View File

@ -967,6 +967,7 @@ fn build_reduced_graph_for_external_crate_res(&mut self, child: Export) {
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::GlobalAsm

View File

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

View File

@ -739,6 +739,7 @@ fn fn_type(seg: &hir::PathSegment<'_>) -> bool {
| HirDefKind::ForeignMod
| HirDefKind::LifetimeParam
| HirDefKind::AnonConst
| HirDefKind::InlineConst
| HirDefKind::Use
| HirDefKind::Field
| HirDefKind::GlobalAsm

View File

@ -151,7 +151,7 @@ enum FailureKind {
if concrete.is_ok() && uv.substs(infcx.tcx).definitely_has_param_types_or_consts(infcx.tcx) {
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);
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.
//
// Right now we do neither of that and simply always fail to unify them.
DefKind::AnonConst => (),
DefKind::AnonConst | DefKind::InlineConst => (),
_ => return Ok(None),
}

View File

@ -430,8 +430,9 @@ fn print_const_with_custom_print_scalar(tcx: TyCtxt<'_>, ct: &'tcx ty::Const<'tc
| Res::NonMacroAttr(_)
| Res::Err => return res.def_id(),
Res::Def(
TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst | OpaqueTy
| Field | LifetimeParam | GlobalAsm | Impl | Closure | Generator,
TyParam | ConstParam | Ctor(..) | ExternCrate | Use | ForeignMod | AnonConst
| InlineConst | OpaqueTy | Field | LifetimeParam | GlobalAsm | Impl | Closure
| Generator,
id,
) => return id,
};

View File

@ -134,6 +134,7 @@ fn from(other: DefKind) -> Self {
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::InlineConst
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::LifetimeParam

View File

@ -1937,7 +1937,8 @@ fn split(path: &str) -> Option<(&str, &str)> {
| Use
| LifetimeParam
| Ctor(_, _)
| AnonConst => {
| AnonConst
| InlineConst => {
let note = assoc_item_not_allowed(res);
if let Some(span) = sp {
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) => {
#[allow(clippy::match_same_arms)]
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,
_ => return,
};