Skip to content

Commit 1f1fe81

Browse files
committed
Auto merge of #15036 - Veykril:const-param-intern-cleanup, r=Veykril
internal: Give ConstBlockId and InTypeConstId named Location types cc #14932
2 parents 6b3659d + abe2495 commit 1f1fe81

File tree

12 files changed

+90
-70
lines changed

12 files changed

+90
-70
lines changed

crates/base-db/src/input.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ impl CrateGraph {
473473
self.arena.iter().map(|(idx, _)| idx)
474474
}
475475

476+
// FIXME: used for `handle_hack_cargo_workspace`, should be removed later
477+
#[doc(hidden)]
476478
pub fn iter_mut(&mut self) -> impl Iterator<Item = (CrateId, &mut CrateData)> + '_ {
477479
self.arena.iter_mut()
478480
}

crates/hir-def/src/body.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ impl Body {
118118
let _p = profile::span("body_with_source_map_query");
119119
let mut params = None;
120120

121-
let (file_id, body, is_async_fn) = {
121+
let mut is_async_fn = false;
122+
let InFile { file_id, value: body } = {
122123
match def {
123124
DefWithBodyId::FunctionId(f) => {
124125
let data = db.function_data(f);
@@ -138,26 +139,24 @@ impl Body {
138139
}),
139140
)
140141
});
141-
(src.file_id, src.value.body().map(ast::Expr::from), data.has_async_kw())
142+
is_async_fn = data.has_async_kw();
143+
src.map(|it| it.body().map(ast::Expr::from))
142144
}
143145
DefWithBodyId::ConstId(c) => {
144146
let c = c.lookup(db);
145147
let src = c.source(db);
146-
(src.file_id, src.value.body(), false)
148+
src.map(|it| it.body())
147149
}
148150
DefWithBodyId::StaticId(s) => {
149151
let s = s.lookup(db);
150152
let src = s.source(db);
151-
(src.file_id, src.value.body(), false)
153+
src.map(|it| it.body())
152154
}
153155
DefWithBodyId::VariantId(v) => {
154156
let src = v.parent.child_source(db);
155-
let variant = &src.value[v.local_id];
156-
(src.file_id, variant.expr(), false)
157-
}
158-
DefWithBodyId::InTypeConstId(c) => {
159-
(c.lookup(db).0.file_id, c.source(db).expr(), false)
157+
src.map(|it| it[v.local_id].expr())
160158
}
159+
DefWithBodyId::InTypeConstId(c) => c.lookup(db).id.map(|_| c.source(db).expr()),
161160
}
162161
};
163162
let module = def.module(db);

crates/hir-def/src/body/lower.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::{
4040
nameres::{DefMap, MacroSubNs},
4141
path::{GenericArgs, Path},
4242
type_ref::{Mutability, Rawness, TypeRef},
43-
AdtId, BlockId, BlockLoc, DefWithBodyId, ModuleDefId, UnresolvedMacro,
43+
AdtId, BlockId, BlockLoc, ConstBlockLoc, DefWithBodyId, ModuleDefId, UnresolvedMacro,
4444
};
4545

4646
pub(super) fn lower(
@@ -297,7 +297,10 @@ impl ExprCollector<'_> {
297297
let (result_expr_id, prev_binding_owner) =
298298
this.initialize_binding_owner(syntax_ptr);
299299
let inner_expr = this.collect_block(e);
300-
let x = this.db.intern_anonymous_const((this.owner, inner_expr));
300+
let x = this.db.intern_anonymous_const(ConstBlockLoc {
301+
parent: this.owner,
302+
root: inner_expr,
303+
});
301304
this.body.exprs[result_expr_id] = Expr::Const(x);
302305
this.current_binding_owner = prev_binding_owner;
303306
result_expr_id

crates/hir-def/src/db.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Defines database & queries for name resolution.
22
use base_db::{salsa, CrateId, SourceDatabase, Upcast};
33
use either::Either;
4-
use hir_expand::{db::ExpandDatabase, AstId, HirFileId};
4+
use hir_expand::{db::ExpandDatabase, HirFileId};
55
use intern::Interned;
66
use la_arena::ArenaMap;
77
use syntax::{ast, AstPtr};
@@ -16,22 +16,22 @@ use crate::{
1616
TraitAliasData, TraitData, TypeAliasData,
1717
},
1818
generics::GenericParams,
19-
hir::ExprId,
2019
import_map::ImportMap,
2120
item_tree::{AttrOwner, ItemTree},
2221
lang_item::{LangItem, LangItemTarget, LangItems},
2322
nameres::{diagnostics::DefDiagnostic, DefMap},
2423
visibility::{self, Visibility},
25-
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc,
26-
ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc,
27-
InTypeConstId, LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId,
28-
MacroRulesLoc, OpaqueInternableThing, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
29-
StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc,
30-
TypeOwnerId, UnionId, UnionLoc, VariantId,
24+
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstBlockLoc, ConstId, ConstLoc, DefWithBodyId,
25+
EnumId, EnumLoc, ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId,
26+
ImplLoc, InTypeConstId, InTypeConstLoc, LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc,
27+
MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
28+
StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
29+
UnionLoc, VariantId,
3130
};
3231

3332
#[salsa::query_group(InternDatabaseStorage)]
3433
pub trait InternDatabase: SourceDatabase {
34+
// region: items
3535
#[salsa::interned]
3636
fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
3737
#[salsa::interned]
@@ -55,20 +55,19 @@ pub trait InternDatabase: SourceDatabase {
5555
#[salsa::interned]
5656
fn intern_extern_block(&self, loc: ExternBlockLoc) -> ExternBlockId;
5757
#[salsa::interned]
58-
fn intern_block(&self, loc: BlockLoc) -> BlockId;
59-
#[salsa::interned]
6058
fn intern_macro2(&self, loc: Macro2Loc) -> Macro2Id;
6159
#[salsa::interned]
6260
fn intern_proc_macro(&self, loc: ProcMacroLoc) -> ProcMacroId;
6361
#[salsa::interned]
6462
fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
63+
// endregion: items
64+
6565
#[salsa::interned]
66-
fn intern_anonymous_const(&self, id: (DefWithBodyId, ExprId)) -> ConstBlockId;
66+
fn intern_block(&self, loc: BlockLoc) -> BlockId;
6767
#[salsa::interned]
68-
fn intern_in_type_const(
69-
&self,
70-
id: (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>),
71-
) -> InTypeConstId;
68+
fn intern_anonymous_const(&self, id: ConstBlockLoc) -> ConstBlockId;
69+
#[salsa::interned]
70+
fn intern_in_type_const(&self, id: InTypeConstLoc) -> InTypeConstId;
7271
}
7372

7473
#[salsa::query_group(DefDatabaseStorage)]

crates/hir-def/src/lib.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ use std::{
6262
panic::{RefUnwindSafe, UnwindSafe},
6363
};
6464

65-
use base_db::{
66-
impl_intern_key,
67-
salsa::{self, InternId},
68-
CrateId, ProcMacroKind,
69-
};
65+
use base_db::{impl_intern_key, salsa, CrateId, ProcMacroKind};
7066
use hir_expand::{
7167
ast_id_map::FileAstId,
7268
attrs::{Attr, AttrId, AttrInput},
@@ -482,8 +478,16 @@ impl_from!(
482478
/// Id of the anonymous const block expression and patterns. This is very similar to `ClosureId` and
483479
/// shouldn't be a `DefWithBodyId` since its type inference is dependent on its parent.
484480
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
485-
pub struct ConstBlockId(InternId);
486-
impl_intern_key!(ConstBlockId);
481+
pub struct ConstBlockId(salsa::InternId);
482+
impl_intern!(ConstBlockId, ConstBlockLoc, intern_anonymous_const, lookup_intern_anonymous_const);
483+
484+
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
485+
pub struct ConstBlockLoc {
486+
/// The parent of the anonymous const block.
487+
pub parent: DefWithBodyId,
488+
/// The root expression of this const block in the parent body.
489+
pub root: hir::ExprId,
490+
}
487491

488492
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
489493
pub enum TypeOwnerId {
@@ -563,6 +567,7 @@ impl From<GenericDefId> for TypeOwnerId {
563567
}
564568
}
565569

570+
// FIXME: This should not be a thing
566571
/// A thing that we want to store in interned ids, but we don't know its type in `hir-def`. This is
567572
/// currently only used in `InTypeConstId` for storing the type (which has type `Ty` defined in
568573
/// 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> {
620625
/// length (like `[u8; 2 + 2]`). These constants are body owner and are a variant of `DefWithBodyId`. These
621626
/// are not called `AnonymousConstId` to prevent confusion with [`ConstBlockId`].
622627
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
623-
pub struct InTypeConstId(InternId);
624-
type InTypeConstLoc = (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
628+
pub struct InTypeConstId(salsa::InternId);
625629
impl_intern!(InTypeConstId, InTypeConstLoc, intern_in_type_const, lookup_intern_in_type_const);
626630

631+
#[derive(Debug, Hash, Eq, Clone)]
632+
pub struct InTypeConstLoc {
633+
pub id: AstId<ast::ConstArg>,
634+
/// The thing this const arg appears in
635+
pub owner: TypeOwnerId,
636+
pub thing: Box<dyn OpaqueInternableThing>,
637+
}
638+
639+
impl PartialEq for InTypeConstLoc {
640+
fn eq(&self, other: &Self) -> bool {
641+
self.id == other.id && self.owner == other.owner && &*self.thing == &*other.thing
642+
}
643+
}
644+
627645
impl InTypeConstId {
628646
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::ConstArg {
629-
let src = self.lookup(db).0;
647+
let src = self.lookup(db).id;
630648
let file_id = src.file_id;
631649
let root = &db.parse_or_expand(file_id);
632650
db.ast_id_map(file_id).get(src.value).to_node(root)
@@ -647,15 +665,9 @@ impl_from!(ConstId, ConstBlockId, InTypeConstId for GeneralConstId);
647665
impl GeneralConstId {
648666
pub fn generic_def(self, db: &dyn db::DefDatabase) -> Option<GenericDefId> {
649667
match self {
650-
GeneralConstId::ConstId(x) => Some(x.into()),
651-
GeneralConstId::ConstBlockId(x) => {
652-
let (parent, _) = db.lookup_intern_anonymous_const(x);
653-
parent.as_generic_def_id()
654-
}
655-
GeneralConstId::InTypeConstId(x) => {
656-
let (_, parent, _) = x.lookup(db);
657-
parent.as_generic_def_id()
658-
}
668+
GeneralConstId::ConstId(it) => Some(it.into()),
669+
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(),
670+
GeneralConstId::InTypeConstId(it) => it.lookup(db).owner.as_generic_def_id(),
659671
}
660672
}
661673

@@ -902,7 +914,7 @@ impl HasModule for TypeOwnerId {
902914
TypeOwnerId::FunctionId(x) => x.lookup(db).module(db),
903915
TypeOwnerId::StaticId(x) => x.lookup(db).module(db),
904916
TypeOwnerId::ConstId(x) => x.lookup(db).module(db),
905-
TypeOwnerId::InTypeConstId(x) => x.lookup(db).1.module(db),
917+
TypeOwnerId::InTypeConstId(x) => x.lookup(db).owner.module(db),
906918
TypeOwnerId::AdtId(x) => x.module(db),
907919
TypeOwnerId::TraitId(x) => x.lookup(db).container,
908920
TypeOwnerId::TraitAliasId(x) => x.lookup(db).container,
@@ -921,7 +933,7 @@ impl HasModule for DefWithBodyId {
921933
DefWithBodyId::StaticId(it) => it.lookup(db).module(db),
922934
DefWithBodyId::ConstId(it) => it.lookup(db).module(db),
923935
DefWithBodyId::VariantId(it) => it.parent.lookup(db).container,
924-
DefWithBodyId::InTypeConstId(it) => it.lookup(db).1.module(db),
936+
DefWithBodyId::InTypeConstId(it) => it.lookup(db).owner.module(db),
925937
}
926938
}
927939
}

crates/hir-def/src/resolver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ impl HasResolver for TypeOwnerId {
10151015
TypeOwnerId::FunctionId(x) => x.resolver(db),
10161016
TypeOwnerId::StaticId(x) => x.resolver(db),
10171017
TypeOwnerId::ConstId(x) => x.resolver(db),
1018-
TypeOwnerId::InTypeConstId(x) => x.lookup(db).1.resolver(db),
1018+
TypeOwnerId::InTypeConstId(x) => x.lookup(db).owner.resolver(db),
10191019
TypeOwnerId::AdtId(x) => x.resolver(db),
10201020
TypeOwnerId::TraitId(x) => x.resolver(db),
10211021
TypeOwnerId::TraitAliasId(x) => x.resolver(db),
@@ -1034,7 +1034,7 @@ impl HasResolver for DefWithBodyId {
10341034
DefWithBodyId::FunctionId(f) => f.resolver(db),
10351035
DefWithBodyId::StaticId(s) => s.resolver(db),
10361036
DefWithBodyId::VariantId(v) => v.parent.resolver(db),
1037-
DefWithBodyId::InTypeConstId(c) => c.lookup(db).1.resolver(db),
1037+
DefWithBodyId::InTypeConstId(c) => c.lookup(db).owner.resolver(db),
10381038
}
10391039
}
10401040
}

crates/hir-ty/src/consteval.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use hir_def::{
77
path::Path,
88
resolver::{Resolver, ValueNs},
99
type_ref::LiteralConstRef,
10-
EnumVariantId, GeneralConstId, StaticId,
10+
ConstBlockLoc, EnumVariantId, GeneralConstId, StaticId,
1111
};
1212
use la_arena::{Idx, RawIdx};
1313
use stdx::never;
@@ -216,14 +216,14 @@ pub(crate) fn const_eval_query(
216216
db.monomorphized_mir_body(c.into(), subst, db.trait_environment(c.into()))?
217217
}
218218
GeneralConstId::ConstBlockId(c) => {
219-
let (def, root) = db.lookup_intern_anonymous_const(c);
220-
let body = db.body(def);
221-
let infer = db.infer(def);
219+
let ConstBlockLoc { parent, root } = db.lookup_intern_anonymous_const(c);
220+
let body = db.body(parent);
221+
let infer = db.infer(parent);
222222
Arc::new(monomorphize_mir_body_bad(
223223
db,
224-
lower_to_mir(db, def, &body, &infer, root)?,
224+
lower_to_mir(db, parent, &body, &infer, root)?,
225225
subst,
226-
db.trait_environment_for_body(def),
226+
db.trait_environment_for_body(parent),
227227
)?)
228228
}
229229
GeneralConstId::InTypeConstId(c) => db.mir_body(c.into())?,

crates/hir-ty/src/infer.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
109109
}
110110
DefWithBodyId::InTypeConstId(c) => {
111111
// FIXME(const-generic-body): We should not get the return type in this way.
112-
ctx.return_ty =
113-
c.lookup(db.upcast()).2.box_any().downcast::<InTypeConstIdMetadata>().unwrap().0;
112+
ctx.return_ty = c
113+
.lookup(db.upcast())
114+
.thing
115+
.box_any()
116+
.downcast::<InTypeConstIdMetadata>()
117+
.unwrap()
118+
.0;
114119
}
115120
}
116121

crates/hir-ty/src/infer/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ impl<'a> InferenceContext<'a> {
173173
}
174174
Expr::Const(id) => {
175175
self.with_breakable_ctx(BreakableKind::Border, None, None, |this| {
176-
let (_, expr) = this.db.lookup_intern_anonymous_const(*id);
177-
this.infer_expr(expr, expected)
176+
let loc = this.db.lookup_intern_anonymous_const(*id);
177+
this.infer_expr(loc.root, expected)
178178
})
179179
.1
180180
}

crates/hir-ty/src/infer/mutability.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ impl<'a> InferenceContext<'a> {
4343
}
4444
}
4545
Expr::Const(id) => {
46-
let (_, expr) = self.db.lookup_intern_anonymous_const(*id);
47-
self.infer_mut_expr(expr, Mutability::Not);
46+
let loc = self.db.lookup_intern_anonymous_const(*id);
47+
self.infer_mut_expr(loc.root, Mutability::Not);
4848
}
4949
Expr::Let { pat, expr } => self.infer_mut_expr(*expr, self.pat_bound_mutability(*pat)),
5050
Expr::Block { id: _, statements, tail, label: _ }

crates/hir-ty/src/lower.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use hir_def::{
2929
resolver::{HasResolver, Resolver, TypeNs},
3030
type_ref::{ConstRef, TraitBoundModifier, TraitRef as HirTraitRef, TypeBound, TypeRef},
3131
AdtId, AssocItemId, ConstId, ConstParamId, DefWithBodyId, EnumId, EnumVariantId, FunctionId,
32-
GenericDefId, HasModule, ImplId, ItemContainerId, LocalFieldId, Lookup, ModuleDefId, StaticId,
33-
StructId, TraitId, TypeAliasId, TypeOrConstParamId, TypeOwnerId, TypeParamId, UnionId,
34-
VariantId,
32+
GenericDefId, HasModule, ImplId, InTypeConstLoc, ItemContainerId, LocalFieldId, Lookup,
33+
ModuleDefId, StaticId, StructId, TraitId, TypeAliasId, TypeOrConstParamId, TypeOwnerId,
34+
TypeParamId, UnionId, VariantId,
3535
};
3636
use hir_expand::{name::Name, ExpandResult};
3737
use intern::Interned;
@@ -2047,7 +2047,7 @@ pub(crate) fn const_or_path_to_chalk(
20472047
)
20482048
.unwrap_or_else(|| unknown_const(expected_ty))
20492049
}
2050-
&ConstRef::Complex(x) => {
2050+
&ConstRef::Complex(it) => {
20512051
let crate_data = &db.crate_graph()[owner.module(db.upcast()).krate()];
20522052
if crate_data.env.get("__ra_is_test_fixture").is_none() && crate_data.origin.is_local()
20532053
{
@@ -2056,11 +2056,11 @@ pub(crate) fn const_or_path_to_chalk(
20562056
return unknown_const(expected_ty);
20572057
}
20582058
let c = db
2059-
.intern_in_type_const((
2060-
x,
2059+
.intern_in_type_const(InTypeConstLoc {
2060+
id: it,
20612061
owner,
2062-
Box::new(InTypeConstIdMetadata(expected_ty.clone())),
2063-
))
2062+
thing: Box::new(InTypeConstIdMetadata(expected_ty.clone())),
2063+
})
20642064
.into();
20652065
intern_const_scalar(
20662066
ConstScalar::UnevaluatedConst(c, Substitution::empty(Interner)),

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2104,7 +2104,7 @@ pub struct InTypeConst {
21042104

21052105
impl InTypeConst {
21062106
pub fn module(self, db: &dyn HirDatabase) -> Module {
2107-
Module { id: self.id.lookup(db.upcast()).1.module(db.upcast()) }
2107+
Module { id: self.id.lookup(db.upcast()).owner.module(db.upcast()) }
21082108
}
21092109
}
21102110

0 commit comments

Comments
 (0)