Skip to content

Commit a469578

Browse files
committed
Add a bunch of fixme comments
1 parent e83b567 commit a469578

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

crates/hir-def/src/db.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use crate::{
2222
lang_item::{LangItem, LangItemTarget, LangItems},
2323
nameres::{diagnostics::DefDiagnostic, DefMap},
2424
visibility::{self, Visibility},
25-
AnonymousConstId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId,
26-
EnumLoc, ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc,
25+
AttrDefId, BlockId, BlockLoc, ConstBlockId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc,
26+
ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc,
2727
InTypeConstId, LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId,
2828
MacroRulesLoc, OpaqueInternableThing, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
2929
StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc,
@@ -63,7 +63,7 @@ pub trait InternDatabase: SourceDatabase {
6363
#[salsa::interned]
6464
fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
6565
#[salsa::interned]
66-
fn intern_anonymous_const(&self, id: (DefWithBodyId, ExprId)) -> AnonymousConstId;
66+
fn intern_anonymous_const(&self, id: (DefWithBodyId, ExprId)) -> ConstBlockId;
6767
#[salsa::interned]
6868
fn intern_in_type_const(
6969
&self,

crates/hir-def/src/hir.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
2727
path::{GenericArgs, Path},
2828
type_ref::{Mutability, Rawness, TypeRef},
29-
AnonymousConstId, BlockId,
29+
BlockId, ConstBlockId,
3030
};
3131

3232
pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp};
@@ -181,7 +181,7 @@ pub enum Expr {
181181
statements: Box<[Statement]>,
182182
tail: Option<ExprId>,
183183
},
184-
Const(AnonymousConstId),
184+
Const(ConstBlockId),
185185
Unsafe {
186186
id: Option<BlockId>,
187187
statements: Box<[Statement]>,

crates/hir-def/src/lib.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ impl_from!(
482482
/// Id of the anonymous const block expression and patterns. This is very similar to `ClosureId` and
483483
/// shouldn't be a `DefWithBodyId` since its type inference is dependent on its parent.
484484
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
485-
pub struct AnonymousConstId(InternId);
486-
impl_intern_key!(AnonymousConstId);
485+
pub struct ConstBlockId(InternId);
486+
impl_intern_key!(ConstBlockId);
487487

488488
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
489489
pub enum TypeOwnerId {
@@ -563,7 +563,10 @@ impl From<GenericDefId> for TypeOwnerId {
563563
}
564564
}
565565

566-
/// A thing that we want to store in interned ids, but we don't know its type in `hir-def`
566+
/// A thing that we want to store in interned ids, but we don't know its type in `hir-def`. This is
567+
/// currently only used in `InTypeConstId` for storing the type (which has type `Ty` defined in
568+
/// the `hir-ty` crate) of the constant in its id, which is a temporary hack so we may want
569+
/// to remove this after removing that.
567570
pub trait OpaqueInternableThing:
568571
std::any::Any + std::fmt::Debug + Sync + Send + UnwindSafe + RefUnwindSafe
569572
{
@@ -594,6 +597,28 @@ impl Clone for Box<dyn OpaqueInternableThing> {
594597
}
595598
}
596599

600+
// FIXME(const-generic-body): Use an stable id for in type consts.
601+
//
602+
// The current id uses `AstId<ast::ConstArg>` which will be changed by every change in the code. Ideally
603+
// we should use an id which is relative to the type owner, so that every change will only invalidate the
604+
// id if it happens inside of the type owner.
605+
//
606+
// The solution probably is to have some query on `TypeOwnerId` to traverse its constant children and store
607+
// their `AstId` in a list (vector or arena), and use the index of that list in the id here. That query probably
608+
// needs name resolution, and might go far and handles the whole path lowering or type lowering for a `TypeOwnerId`.
609+
//
610+
// Whatever path the solution takes, it should answer 3 questions at the same time:
611+
// * Is the id stable enough?
612+
// * How to find a constant id using an ast node / position in the source code? This is needed when we want to
613+
// provide ide functionalities inside an in type const (which we currently don't support) e.g. go to definition
614+
// for a local defined there. A complex id might have some trouble in this reverse mapping.
615+
// * How to find the return type of a constant using its id? We have this data when we are doing type lowering
616+
// and the name of the struct that contains this constant is resolved, so a query that only traverses the
617+
// type owner by its syntax tree might have a hard time here.
618+
619+
/// A constant in a type as a substitution for const generics (like `Foo<{ 2 + 2 }>`) or as an array
620+
/// length (like `[u8; 2 + 2]`). These constants are body owner and are a variant of `DefWithBodyId`. These
621+
/// are not called `AnonymousConstId` to prevent confusion with [`ConstBlockId`].
597622
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
598623
pub struct InTypeConstId(InternId);
599624
type InTypeConstLoc = (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
@@ -613,17 +638,17 @@ impl InTypeConstId {
613638
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
614639
pub enum GeneralConstId {
615640
ConstId(ConstId),
616-
AnonymousConstId(AnonymousConstId),
641+
ConstBlockId(ConstBlockId),
617642
InTypeConstId(InTypeConstId),
618643
}
619644

620-
impl_from!(ConstId, AnonymousConstId, InTypeConstId for GeneralConstId);
645+
impl_from!(ConstId, ConstBlockId, InTypeConstId for GeneralConstId);
621646

622647
impl GeneralConstId {
623648
pub fn generic_def(self, db: &dyn db::DefDatabase) -> Option<GenericDefId> {
624649
match self {
625650
GeneralConstId::ConstId(x) => Some(x.into()),
626-
GeneralConstId::AnonymousConstId(x) => {
651+
GeneralConstId::ConstBlockId(x) => {
627652
let (parent, _) = db.lookup_intern_anonymous_const(x);
628653
parent.as_generic_def_id()
629654
}
@@ -643,7 +668,7 @@ impl GeneralConstId {
643668
.and_then(|x| x.as_str())
644669
.unwrap_or("_")
645670
.to_owned(),
646-
GeneralConstId::AnonymousConstId(id) => format!("{{anonymous const {id:?}}}"),
671+
GeneralConstId::ConstBlockId(id) => format!("{{anonymous const {id:?}}}"),
647672
GeneralConstId::InTypeConstId(id) => format!("{{in type const {id:?}}}"),
648673
}
649674
}

crates/hir-ty/src/consteval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub(crate) fn const_eval_query(
215215
GeneralConstId::ConstId(c) => {
216216
db.monomorphized_mir_body(c.into(), subst, db.trait_environment(c.into()))?
217217
}
218-
GeneralConstId::AnonymousConstId(c) => {
218+
GeneralConstId::ConstBlockId(c) => {
219219
let (def, root) = db.lookup_intern_anonymous_const(c);
220220
let body = db.body(def);
221221
let infer = db.infer(def);

crates/hir-ty/src/infer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<Infer
108108
});
109109
}
110110
DefWithBodyId::InTypeConstId(c) => {
111+
// FIXME(const-generic-body): We should not get the return type in this way.
111112
ctx.return_ty =
112113
c.lookup(db.upcast()).2.box_any().downcast::<InTypeConstIdMetadata>().unwrap().0;
113114
}

0 commit comments

Comments
 (0)