mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 15:54:15 +00:00
Auto merge of #15036 - Veykril:const-param-intern-cleanup, r=Veykril
internal: Give ConstBlockId and InTypeConstId named Location types cc https://github.com/rust-lang/rust-analyzer/pull/14932
This commit is contained in:
commit
1f1fe81f0d
@ -473,6 +473,8 @@ impl CrateGraph {
|
||||
self.arena.iter().map(|(idx, _)| idx)
|
||||
}
|
||||
|
||||
// FIXME: used for `handle_hack_cargo_workspace`, should be removed later
|
||||
#[doc(hidden)]
|
||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = (CrateId, &mut CrateData)> + '_ {
|
||||
self.arena.iter_mut()
|
||||
}
|
||||
|
@ -118,7 +118,8 @@ impl Body {
|
||||
let _p = profile::span("body_with_source_map_query");
|
||||
let mut params = None;
|
||||
|
||||
let (file_id, body, is_async_fn) = {
|
||||
let mut is_async_fn = false;
|
||||
let InFile { file_id, value: body } = {
|
||||
match def {
|
||||
DefWithBodyId::FunctionId(f) => {
|
||||
let data = db.function_data(f);
|
||||
@ -138,26 +139,24 @@ impl Body {
|
||||
}),
|
||||
)
|
||||
});
|
||||
(src.file_id, src.value.body().map(ast::Expr::from), data.has_async_kw())
|
||||
is_async_fn = data.has_async_kw();
|
||||
src.map(|it| it.body().map(ast::Expr::from))
|
||||
}
|
||||
DefWithBodyId::ConstId(c) => {
|
||||
let c = c.lookup(db);
|
||||
let src = c.source(db);
|
||||
(src.file_id, src.value.body(), false)
|
||||
src.map(|it| it.body())
|
||||
}
|
||||
DefWithBodyId::StaticId(s) => {
|
||||
let s = s.lookup(db);
|
||||
let src = s.source(db);
|
||||
(src.file_id, src.value.body(), false)
|
||||
src.map(|it| it.body())
|
||||
}
|
||||
DefWithBodyId::VariantId(v) => {
|
||||
let src = v.parent.child_source(db);
|
||||
let variant = &src.value[v.local_id];
|
||||
(src.file_id, variant.expr(), false)
|
||||
}
|
||||
DefWithBodyId::InTypeConstId(c) => {
|
||||
(c.lookup(db).0.file_id, c.source(db).expr(), false)
|
||||
src.map(|it| it[v.local_id].expr())
|
||||
}
|
||||
DefWithBodyId::InTypeConstId(c) => c.lookup(db).id.map(|_| c.source(db).expr()),
|
||||
}
|
||||
};
|
||||
let module = def.module(db);
|
||||
|
@ -40,7 +40,7 @@ use crate::{
|
||||
nameres::{DefMap, MacroSubNs},
|
||||
path::{GenericArgs, Path},
|
||||
type_ref::{Mutability, Rawness, TypeRef},
|
||||
AdtId, BlockId, BlockLoc, DefWithBodyId, ModuleDefId, UnresolvedMacro,
|
||||
AdtId, BlockId, BlockLoc, ConstBlockLoc, DefWithBodyId, ModuleDefId, UnresolvedMacro,
|
||||
};
|
||||
|
||||
pub(super) fn lower(
|
||||
@ -297,7 +297,10 @@ impl ExprCollector<'_> {
|
||||
let (result_expr_id, prev_binding_owner) =
|
||||
this.initialize_binding_owner(syntax_ptr);
|
||||
let inner_expr = this.collect_block(e);
|
||||
let x = this.db.intern_anonymous_const((this.owner, inner_expr));
|
||||
let x = this.db.intern_anonymous_const(ConstBlockLoc {
|
||||
parent: this.owner,
|
||||
root: inner_expr,
|
||||
});
|
||||
this.body.exprs[result_expr_id] = Expr::Const(x);
|
||||
this.current_binding_owner = prev_binding_owner;
|
||||
result_expr_id
|
||||
|
@ -1,7 +1,7 @@
|
||||
//! Defines database & queries for name resolution.
|
||||
use base_db::{salsa, CrateId, SourceDatabase, Upcast};
|
||||
use either::Either;
|
||||
use hir_expand::{db::ExpandDatabase, AstId, HirFileId};
|
||||
use hir_expand::{db::ExpandDatabase, HirFileId};
|
||||
use intern::Interned;
|
||||
use la_arena::ArenaMap;
|
||||
use syntax::{ast, AstPtr};
|
||||
@ -16,22 +16,22 @@ use crate::{
|
||||
TraitAliasData, TraitData, TypeAliasData,
|
||||
},
|
||||
generics::GenericParams,
|
||||
hir::ExprId,
|
||||
import_map::ImportMap,
|
||||
item_tree::{AttrOwner, ItemTree},
|
||||
lang_item::{LangItem, LangItemTarget, LangItems},
|
||||
nameres::{diagnostics::DefDiagnostic, DefMap},
|
||||
visibility::{self, Visibility},
|
||||
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc,
|
||||
ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc,
|
||||
InTypeConstId, LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId,
|
||||
MacroRulesLoc, OpaqueInternableThing, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
|
||||
StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc,
|
||||
TypeOwnerId, UnionId, UnionLoc, VariantId,
|
||||
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
|
||||
EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId,
|
||||
ImplLoc, InTypeConstId, InTypeConstLoc, LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc,
|
||||
MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
|
||||
StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
|
||||
UnionLoc, VariantId,
|
||||
};
|
||||
|
||||
#[salsa::query_group(InternDatabaseStorage)]
|
||||
pub trait InternDatabase: SourceDatabase {
|
||||
// region: items
|
||||
#[salsa::interned]
|
||||
fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
|
||||
#[salsa::interned]
|
||||
@ -55,20 +55,19 @@ pub trait InternDatabase: SourceDatabase {
|
||||
#[salsa::interned]
|
||||
fn intern_extern_block(&self, loc: ExternBlockLoc) -> ExternBlockId;
|
||||
#[salsa::interned]
|
||||
fn intern_block(&self, loc: BlockLoc) -> BlockId;
|
||||
#[salsa::interned]
|
||||
fn intern_macro2(&self, loc: Macro2Loc) -> Macro2Id;
|
||||
#[salsa::interned]
|
||||
fn intern_proc_macro(&self, loc: ProcMacroLoc) -> ProcMacroId;
|
||||
#[salsa::interned]
|
||||
fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
|
||||
// endregion: items
|
||||
|
||||
#[salsa::interned]
|
||||
fn intern_anonymous_const(&self, id: (DefWithBodyId, ExprId)) -> ConstBlockId;
|
||||
fn intern_block(&self, loc: BlockLoc) -> BlockId;
|
||||
#[salsa::interned]
|
||||
fn intern_in_type_const(
|
||||
&self,
|
||||
id: (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>),
|
||||
) -> InTypeConstId;
|
||||
fn intern_anonymous_const(&self, id: ConstBlockLoc) -> ConstBlockId;
|
||||
#[salsa::interned]
|
||||
fn intern_in_type_const(&self, id: InTypeConstLoc) -> InTypeConstId;
|
||||
}
|
||||
|
||||
#[salsa::query_group(DefDatabaseStorage)]
|
||||
|
@ -62,11 +62,7 @@ use std::{
|
||||
panic::{RefUnwindSafe, UnwindSafe},
|
||||
};
|
||||
|
||||
use base_db::{
|
||||
impl_intern_key,
|
||||
salsa::{self, InternId},
|
||||
CrateId, ProcMacroKind,
|
||||
};
|
||||
use base_db::{impl_intern_key, salsa, CrateId, ProcMacroKind};
|
||||
use hir_expand::{
|
||||
ast_id_map::FileAstId,
|
||||
attrs::{Attr, AttrId, AttrInput},
|
||||
@ -482,8 +478,16 @@ impl_from!(
|
||||
/// Id of the anonymous const block expression and patterns. This is very similar to `ClosureId` and
|
||||
/// shouldn't be a `DefWithBodyId` since its type inference is dependent on its parent.
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub struct ConstBlockId(InternId);
|
||||
impl_intern_key!(ConstBlockId);
|
||||
pub struct ConstBlockId(salsa::InternId);
|
||||
impl_intern!(ConstBlockId, ConstBlockLoc, intern_anonymous_const, lookup_intern_anonymous_const);
|
||||
|
||||
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
|
||||
pub struct ConstBlockLoc {
|
||||
/// The parent of the anonymous const block.
|
||||
pub parent: DefWithBodyId,
|
||||
/// The root expression of this const block in the parent body.
|
||||
pub root: hir::ExprId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub enum TypeOwnerId {
|
||||
@ -563,6 +567,7 @@ impl From<GenericDefId> for TypeOwnerId {
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: This should not be a thing
|
||||
/// A thing that we want to store in interned ids, but we don't know its type in `hir-def`. This is
|
||||
/// currently only used in `InTypeConstId` for storing the type (which has type `Ty` defined in
|
||||
/// the `hir-ty` crate) of the constant in its id, which is a temporary hack so we may want
|
||||
@ -620,13 +625,26 @@ impl Clone for Box<dyn OpaqueInternableThing> {
|
||||
/// length (like `[u8; 2 + 2]`). These constants are body owner and are a variant of `DefWithBodyId`. These
|
||||
/// are not called `AnonymousConstId` to prevent confusion with [`ConstBlockId`].
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub struct InTypeConstId(InternId);
|
||||
type InTypeConstLoc = (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
|
||||
pub struct InTypeConstId(salsa::InternId);
|
||||
impl_intern!(InTypeConstId, InTypeConstLoc, intern_in_type_const, lookup_intern_in_type_const);
|
||||
|
||||
#[derive(Debug, Hash, Eq, Clone)]
|
||||
pub struct InTypeConstLoc {
|
||||
pub id: AstId<ast::ConstArg>,
|
||||
/// The thing this const arg appears in
|
||||
pub owner: TypeOwnerId,
|
||||
pub thing: Box<dyn OpaqueInternableThing>,
|
||||
}
|
||||
|
||||
impl PartialEq for InTypeConstLoc {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id && self.owner == other.owner && &*self.thing == &*other.thing
|
||||
}
|
||||
}
|
||||
|
||||
impl InTypeConstId {
|
||||
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::ConstArg {
|
||||
let src = self.lookup(db).0;
|
||||
let src = self.lookup(db).id;
|
||||
let file_id = src.file_id;
|
||||
let root = &db.parse_or_expand(file_id);
|
||||
db.ast_id_map(file_id).get(src.value).to_node(root)
|
||||
@ -647,15 +665,9 @@ impl_from!(ConstId, ConstBlockId, InTypeConstId for GeneralConstId);
|
||||
impl GeneralConstId {
|
||||
pub fn generic_def(self, db: &dyn db::DefDatabase) -> Option<GenericDefId> {
|
||||
match self {
|
||||
GeneralConstId::ConstId(x) => Some(x.into()),
|
||||
GeneralConstId::ConstBlockId(x) => {
|
||||
let (parent, _) = db.lookup_intern_anonymous_const(x);
|
||||
parent.as_generic_def_id()
|
||||
}
|
||||
GeneralConstId::InTypeConstId(x) => {
|
||||
let (_, parent, _) = x.lookup(db);
|
||||
parent.as_generic_def_id()
|
||||
}
|
||||
GeneralConstId::ConstId(it) => Some(it.into()),
|
||||
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(),
|
||||
GeneralConstId::InTypeConstId(it) => it.lookup(db).owner.as_generic_def_id(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -902,7 +914,7 @@ impl HasModule for TypeOwnerId {
|
||||
TypeOwnerId::FunctionId(x) => x.lookup(db).module(db),
|
||||
TypeOwnerId::StaticId(x) => x.lookup(db).module(db),
|
||||
TypeOwnerId::ConstId(x) => x.lookup(db).module(db),
|
||||
TypeOwnerId::InTypeConstId(x) => x.lookup(db).1.module(db),
|
||||
TypeOwnerId::InTypeConstId(x) => x.lookup(db).owner.module(db),
|
||||
TypeOwnerId::AdtId(x) => x.module(db),
|
||||
TypeOwnerId::TraitId(x) => x.lookup(db).container,
|
||||
TypeOwnerId::TraitAliasId(x) => x.lookup(db).container,
|
||||
@ -921,7 +933,7 @@ impl HasModule for DefWithBodyId {
|
||||
DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
|
||||
DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
|
||||
DefWithBodyId::VariantId(it) => it.parent.lookup(db).container,
|
||||
DefWithBodyId::InTypeConstId(it) => it.lookup(db).1.module(db),
|
||||
DefWithBodyId::InTypeConstId(it) => it.lookup(db).owner.module(db),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1015,7 +1015,7 @@ impl HasResolver for TypeOwnerId {
|
||||
TypeOwnerId::FunctionId(x) => x.resolver(db),
|
||||
TypeOwnerId::StaticId(x) => x.resolver(db),
|
||||
TypeOwnerId::ConstId(x) => x.resolver(db),
|
||||
TypeOwnerId::InTypeConstId(x) => x.lookup(db).1.resolver(db),
|
||||
TypeOwnerId::InTypeConstId(x) => x.lookup(db).owner.resolver(db),
|
||||
TypeOwnerId::AdtId(x) => x.resolver(db),
|
||||
TypeOwnerId::TraitId(x) => x.resolver(db),
|
||||
TypeOwnerId::TraitAliasId(x) => x.resolver(db),
|
||||
@ -1034,7 +1034,7 @@ impl HasResolver for DefWithBodyId {
|
||||
DefWithBodyId::FunctionId(f) => f.resolver(db),
|
||||
DefWithBodyId::StaticId(s) => s.resolver(db),
|
||||
DefWithBodyId::VariantId(v) => v.parent.resolver(db),
|
||||
DefWithBodyId::InTypeConstId(c) => c.lookup(db).1.resolver(db),
|
||||
DefWithBodyId::InTypeConstId(c) => c.lookup(db).owner.resolver(db),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use hir_def::{
|
||||
path::Path,
|
||||
resolver::{Resolver, ValueNs},
|
||||
type_ref::LiteralConstRef,
|
||||
EnumVariantId, GeneralConstId, StaticId,
|
||||
ConstBlockLoc, EnumVariantId, GeneralConstId, StaticId,
|
||||
};
|
||||
use la_arena::{Idx, RawIdx};
|
||||
use stdx::never;
|
||||
@ -216,14 +216,14 @@ pub(crate) fn const_eval_query(
|
||||
db.monomorphized_mir_body(c.into(), subst, db.trait_environment(c.into()))?
|
||||
}
|
||||
GeneralConstId::ConstBlockId(c) => {
|
||||
let (def, root) = db.lookup_intern_anonymous_const(c);
|
||||
let body = db.body(def);
|
||||
let infer = db.infer(def);
|
||||
let ConstBlockLoc { parent, root } = db.lookup_intern_anonymous_const(c);
|
||||
let body = db.body(parent);
|
||||
let infer = db.infer(parent);
|
||||
Arc::new(monomorphize_mir_body_bad(
|
||||
db,
|
||||
lower_to_mir(db, def, &body, &infer, root)?,
|
||||
lower_to_mir(db, parent, &body, &infer, root)?,
|
||||
subst,
|
||||
db.trait_environment_for_body(def),
|
||||
db.trait_environment_for_body(parent),
|
||||
)?)
|
||||
}
|
||||
GeneralConstId::InTypeConstId(c) => db.mir_body(c.into())?,
|
||||
|
@ -109,8 +109,13 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
|
||||
}
|
||||
DefWithBodyId::InTypeConstId(c) => {
|
||||
// FIXME(const-generic-body): We should not get the return type in this way.
|
||||
ctx.return_ty =
|
||||
c.lookup(db.upcast()).2.box_any().downcast::<InTypeConstIdMetadata>().unwrap().0;
|
||||
ctx.return_ty = c
|
||||
.lookup(db.upcast())
|
||||
.thing
|
||||
.box_any()
|
||||
.downcast::<InTypeConstIdMetadata>()
|
||||
.unwrap()
|
||||
.0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -173,8 +173,8 @@ impl<'a> InferenceContext<'a> {
|
||||
}
|
||||
Expr::Const(id) => {
|
||||
self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {
|
||||
let (_, expr) = this.db.lookup_intern_anonymous_const(*id);
|
||||
this.infer_expr(expr, expected)
|
||||
let loc = this.db.lookup_intern_anonymous_const(*id);
|
||||
this.infer_expr(loc.root, expected)
|
||||
})
|
||||
.1
|
||||
}
|
||||
|
@ -43,8 +43,8 @@ impl<'a> InferenceContext<'a> {
|
||||
}
|
||||
}
|
||||
Expr::Const(id) => {
|
||||
let (_, expr) = self.db.lookup_intern_anonymous_const(*id);
|
||||
self.infer_mut_expr(expr, Mutability::Not);
|
||||
let loc = self.db.lookup_intern_anonymous_const(*id);
|
||||
self.infer_mut_expr(loc.root, Mutability::Not);
|
||||
}
|
||||
Expr::Let { pat, expr } => self.infer_mut_expr(*expr, self.pat_bound_mutability(*pat)),
|
||||
Expr::Block { id: _, statements, tail, label: _ }
|
||||
|
@ -29,9 +29,9 @@ use hir_def::{
|
||||
resolver::{HasResolver, Resolver, TypeNs},
|
||||
type_ref::{ConstRef, TraitBoundModifier, TraitRef as HirTraitRef, TypeBound, TypeRef},
|
||||
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
|
||||
GenericDefId, HasModule, ImplId, ItemContainerId, LocalFieldId, Lookup, ModuleDefId, StaticId,
|
||||
StructId, TraitId, TypeAliasId, TypeOrConstParamId, TypeOwnerId, TypeParamId, UnionId,
|
||||
VariantId,
|
||||
GenericDefId, HasModule, ImplId, InTypeConstLoc, ItemContainerId, LocalFieldId, Lookup,
|
||||
ModuleDefId, StaticId, StructId, TraitId, TypeAliasId, TypeOrConstParamId, TypeOwnerId,
|
||||
TypeParamId, UnionId, VariantId,
|
||||
};
|
||||
use hir_expand::{name::Name, ExpandResult};
|
||||
use intern::Interned;
|
||||
@ -2047,7 +2047,7 @@ pub(crate) fn const_or_path_to_chalk(
|
||||
)
|
||||
.unwrap_or_else(|| unknown_const(expected_ty))
|
||||
}
|
||||
&ConstRef::Complex(x) => {
|
||||
&ConstRef::Complex(it) => {
|
||||
let crate_data = &db.crate_graph()[owner.module(db.upcast()).krate()];
|
||||
if crate_data.env.get("__ra_is_test_fixture").is_none() && crate_data.origin.is_local()
|
||||
{
|
||||
@ -2056,11 +2056,11 @@ pub(crate) fn const_or_path_to_chalk(
|
||||
return unknown_const(expected_ty);
|
||||
}
|
||||
let c = db
|
||||
.intern_in_type_const((
|
||||
x,
|
||||
.intern_in_type_const(InTypeConstLoc {
|
||||
id: it,
|
||||
owner,
|
||||
Box::new(InTypeConstIdMetadata(expected_ty.clone())),
|
||||
))
|
||||
thing: Box::new(InTypeConstIdMetadata(expected_ty.clone())),
|
||||
})
|
||||
.into();
|
||||
intern_const_scalar(
|
||||
ConstScalar::UnevaluatedConst(c, Substitution::empty(Interner)),
|
||||
|
@ -2104,7 +2104,7 @@ pub struct InTypeConst {
|
||||
|
||||
impl InTypeConst {
|
||||
pub fn module(self, db: &dyn HirDatabase) -> Module {
|
||||
Module { id: self.id.lookup(db.upcast()).1.module(db.upcast()) }
|
||||
Module { id: self.id.lookup(db.upcast()).owner.module(db.upcast()) }
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user