Skip to content

Commit 776c818

Browse files
Lay the basics for non-Copy solver types with GC
That means stop using Salsa for interning solver types.
1 parent ea1d299 commit 776c818

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3772
-2508
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/src/nameres/attr_resolution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl DefMap {
6363
return Ok(ResolvedAttr::Other);
6464
}
6565
}
66-
None => return Err(UnresolvedMacro { path: ast_id.path.as_ref().clone() }),
66+
None => return Err(UnresolvedMacro { path: (*ast_id.path).clone() }),
6767
};
6868

6969
Ok(ResolvedAttr::Macro(attr_macro_as_call_id(
@@ -145,7 +145,7 @@ pub(super) fn derive_macro_as_call_id(
145145
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
146146
let (macro_id, def_id) = resolver(&item_attr.path)
147147
.filter(|(_, def_id)| def_id.is_derive())
148-
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.as_ref().clone() })?;
148+
.ok_or_else(|| UnresolvedMacro { path: (*item_attr.path).clone() })?;
149149
let call_id = def_id.make_call(
150150
db,
151151
krate,

crates/hir-def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1675,7 +1675,7 @@ impl<'db> DefCollector<'db> {
16751675
derive_index: *derive_pos as u32,
16761676
derive_macro_id: *derive_macro_id,
16771677
},
1678-
ast_id.path.as_ref().clone(),
1678+
(*ast_id.path).clone(),
16791679
));
16801680
}
16811681
// These are diagnosed by `reseed_with_unresolved_attribute`, as that function consumes them

crates/hir-ty/src/consteval.rs

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use crate::{
2323
mir::{MirEvalError, MirLowerError},
2424
next_solver::{
2525
Const, ConstBytes, ConstKind, DbInterner, ErrorGuaranteed, GenericArg, GenericArgs,
26-
ParamEnv, Ty, ValueConst,
26+
ParamEnv, StoredConst, StoredGenericArgs, Ty, ValueConst,
2727
},
28+
traits::StoredParamEnvAndCrate,
2829
};
2930

3031
use super::mir::{interpret_mir, lower_to_mir, pad16};
@@ -38,12 +39,12 @@ pub fn unknown_const_as_generic<'db>(ty: Ty<'db>) -> GenericArg<'db> {
3839
}
3940

4041
#[derive(Debug, Clone, PartialEq, Eq)]
41-
pub enum ConstEvalError<'db> {
42-
MirLowerError(MirLowerError<'db>),
43-
MirEvalError(MirEvalError<'db>),
42+
pub enum ConstEvalError {
43+
MirLowerError(MirLowerError),
44+
MirEvalError(MirEvalError),
4445
}
4546

46-
impl ConstEvalError<'_> {
47+
impl ConstEvalError {
4748
pub fn pretty_print(
4849
&self,
4950
f: &mut String,
@@ -62,17 +63,17 @@ impl ConstEvalError<'_> {
6263
}
6364
}
6465

65-
impl<'db> From<MirLowerError<'db>> for ConstEvalError<'db> {
66-
fn from(value: MirLowerError<'db>) -> Self {
66+
impl From<MirLowerError> for ConstEvalError {
67+
fn from(value: MirLowerError) -> Self {
6768
match value {
6869
MirLowerError::ConstEvalError(_, e) => *e,
6970
_ => ConstEvalError::MirLowerError(value),
7071
}
7172
}
7273
}
7374

74-
impl<'db> From<MirEvalError<'db>> for ConstEvalError<'db> {
75-
fn from(value: MirEvalError<'db>) -> Self {
75+
impl From<MirEvalError> for ConstEvalError {
76+
fn from(value: MirEvalError) -> Self {
7677
ConstEvalError::MirEvalError(value)
7778
}
7879
}
@@ -85,7 +86,8 @@ pub fn intern_const_ref<'a>(
8586
krate: Crate,
8687
) -> Const<'a> {
8788
let interner = DbInterner::new_no_crate(db);
88-
let layout = db.layout_of_ty(ty, ParamEnvAndCrate { param_env: ParamEnv::empty(), krate });
89+
let layout = db
90+
.layout_of_ty(ty.store(), ParamEnvAndCrate { param_env: ParamEnv::empty(), krate }.store());
8991
let kind = match value {
9092
LiteralConstRef::Int(i) => {
9193
// FIXME: We should handle failure of layout better.
@@ -180,10 +182,10 @@ pub fn try_const_isize<'db>(db: &'db dyn HirDatabase, c: &Const<'db>) -> Option<
180182
}
181183
}
182184

183-
pub(crate) fn const_eval_discriminant_variant<'db>(
184-
db: &'db dyn HirDatabase,
185+
pub(crate) fn const_eval_discriminant_variant(
186+
db: &dyn HirDatabase,
185187
variant_id: EnumVariantId,
186-
) -> Result<i128, ConstEvalError<'db>> {
188+
) -> Result<i128, ConstEvalError> {
187189
let interner = DbInterner::new_no_crate(db);
188190
let def = variant_id.into();
189191
let body = db.body(def);
@@ -206,8 +208,9 @@ pub(crate) fn const_eval_discriminant_variant<'db>(
206208

207209
let mir_body = db.monomorphized_mir_body(
208210
def,
209-
GenericArgs::new_from_iter(interner, []),
210-
ParamEnvAndCrate { param_env: db.trait_environment_for_body(def), krate: def.krate(db) },
211+
GenericArgs::empty(interner).store(),
212+
ParamEnvAndCrate { param_env: db.trait_environment_for_body(def), krate: def.krate(db) }
213+
.store(),
211214
)?;
212215
let c = interpret_mir(db, mir_body, false, None)?.0?;
213216
let c = if is_signed {
@@ -233,7 +236,7 @@ pub(crate) fn eval_to_const<'db>(expr: ExprId, ctx: &mut InferenceContext<'_, 'd
233236
}
234237
if has_closure(ctx.body, expr) {
235238
// Type checking clousres need an isolated body (See the above FIXME). Bail out early to prevent panic.
236-
return unknown_const(infer[expr]);
239+
return Const::error(ctx.interner());
237240
}
238241
if let Expr::Path(p) = &ctx.body[expr] {
239242
let mut ctx = TyLoweringContext::new(
@@ -252,63 +255,89 @@ pub(crate) fn eval_to_const<'db>(expr: ExprId, ctx: &mut InferenceContext<'_, 'd
252255
{
253256
return result;
254257
}
255-
unknown_const(infer[expr])
258+
Const::error(ctx.interner())
256259
}
257260

258-
pub(crate) fn const_eval_cycle_result<'db>(
259-
_: &'db dyn HirDatabase,
260-
_: salsa::Id,
261-
_: ConstId,
262-
_: GenericArgs<'db>,
263-
_: Option<ParamEnvAndCrate<'db>>,
264-
) -> Result<Const<'db>, ConstEvalError<'db>> {
265-
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
266-
}
267-
268-
pub(crate) fn const_eval_static_cycle_result<'db>(
269-
_: &'db dyn HirDatabase,
270-
_: salsa::Id,
271-
_: StaticId,
272-
) -> Result<Const<'db>, ConstEvalError<'db>> {
273-
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
274-
}
275-
276-
pub(crate) fn const_eval_discriminant_cycle_result<'db>(
277-
_: &'db dyn HirDatabase,
261+
pub(crate) fn const_eval_discriminant_cycle_result(
262+
_: &dyn HirDatabase,
278263
_: salsa::Id,
279264
_: EnumVariantId,
280-
) -> Result<i128, ConstEvalError<'db>> {
265+
) -> Result<i128, ConstEvalError> {
281266
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
282267
}
283268

284-
pub(crate) fn const_eval_query<'db>(
269+
pub(crate) fn const_eval<'db>(
285270
db: &'db dyn HirDatabase,
286271
def: ConstId,
287272
subst: GenericArgs<'db>,
288273
trait_env: Option<ParamEnvAndCrate<'db>>,
289-
) -> Result<Const<'db>, ConstEvalError<'db>> {
290-
let body = db.monomorphized_mir_body(
291-
def.into(),
292-
subst,
293-
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) },
294-
)?;
295-
let c = interpret_mir(db, body, false, trait_env)?.0?;
296-
Ok(c)
274+
) -> Result<Const<'db>, ConstEvalError> {
275+
return match const_eval_query(db, def, subst.store(), trait_env.map(|env| env.store())) {
276+
Ok(konst) => Ok(konst.as_ref()),
277+
Err(err) => Err(err.clone()),
278+
};
279+
280+
#[salsa::tracked(returns(ref), cycle_result = const_eval_cycle_result)]
281+
pub(crate) fn const_eval_query<'db>(
282+
db: &'db dyn HirDatabase,
283+
def: ConstId,
284+
subst: StoredGenericArgs,
285+
trait_env: Option<StoredParamEnvAndCrate>,
286+
) -> Result<StoredConst, ConstEvalError> {
287+
let body = db.monomorphized_mir_body(
288+
def.into(),
289+
subst,
290+
ParamEnvAndCrate { param_env: db.trait_environment(def.into()), krate: def.krate(db) }
291+
.store(),
292+
)?;
293+
let c = interpret_mir(db, body, false, trait_env.as_ref().map(|env| env.as_ref()))?.0?;
294+
Ok(c.store())
295+
}
296+
297+
pub(crate) fn const_eval_cycle_result(
298+
_: &dyn HirDatabase,
299+
_: salsa::Id,
300+
_: ConstId,
301+
_: StoredGenericArgs,
302+
_: Option<StoredParamEnvAndCrate>,
303+
) -> Result<StoredConst, ConstEvalError> {
304+
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
305+
}
297306
}
298307

299-
pub(crate) fn const_eval_static_query<'db>(
308+
pub(crate) fn const_eval_static<'db>(
300309
db: &'db dyn HirDatabase,
301310
def: StaticId,
302-
) -> Result<Const<'db>, ConstEvalError<'db>> {
303-
let interner = DbInterner::new_no_crate(db);
304-
let body = db.monomorphized_mir_body(
305-
def.into(),
306-
GenericArgs::new_from_iter(interner, []),
307-
ParamEnvAndCrate {
308-
param_env: db.trait_environment_for_body(def.into()),
309-
krate: def.krate(db),
310-
},
311-
)?;
312-
let c = interpret_mir(db, body, false, None)?.0?;
313-
Ok(c)
311+
) -> Result<Const<'db>, ConstEvalError> {
312+
return match const_eval_static_query(db, def) {
313+
Ok(konst) => Ok(konst.as_ref()),
314+
Err(err) => Err(err.clone()),
315+
};
316+
317+
#[salsa::tracked(returns(ref), cycle_result = const_eval_static_cycle_result)]
318+
pub(crate) fn const_eval_static_query<'db>(
319+
db: &'db dyn HirDatabase,
320+
def: StaticId,
321+
) -> Result<StoredConst, ConstEvalError> {
322+
let interner = DbInterner::new_no_crate(db);
323+
let body = db.monomorphized_mir_body(
324+
def.into(),
325+
GenericArgs::empty(interner).store(),
326+
ParamEnvAndCrate {
327+
param_env: db.trait_environment_for_body(def.into()),
328+
krate: def.krate(db),
329+
}
330+
.store(),
331+
)?;
332+
let c = interpret_mir(db, body, false, None)?.0?;
333+
Ok(c.store())
334+
}
335+
336+
pub(crate) fn const_eval_static_cycle_result(
337+
_: &dyn HirDatabase,
338+
_: salsa::Id,
339+
_: StaticId,
340+
) -> Result<StoredConst, ConstEvalError> {
341+
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
342+
}
314343
}

crates/hir-ty/src/consteval/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::{
2727

2828
mod intrinsics;
2929

30-
fn simplify(e: ConstEvalError<'_>) -> ConstEvalError<'_> {
30+
fn simplify(e: ConstEvalError) -> ConstEvalError {
3131
match e {
3232
ConstEvalError::MirEvalError(MirEvalError::InFunction(e, _)) => {
3333
simplify(ConstEvalError::MirEvalError(*e))
@@ -39,7 +39,7 @@ fn simplify(e: ConstEvalError<'_>) -> ConstEvalError<'_> {
3939
#[track_caller]
4040
fn check_fail(
4141
#[rust_analyzer::rust_fixture] ra_fixture: &str,
42-
error: impl FnOnce(ConstEvalError<'_>) -> bool,
42+
error: impl FnOnce(ConstEvalError) -> bool,
4343
) {
4444
let (db, file_id) = TestDB::with_single_file(ra_fixture);
4545
crate::attach_db(&db, || match eval_goal(&db, file_id) {
@@ -104,7 +104,7 @@ fn check_answer(
104104
});
105105
}
106106

107-
fn pretty_print_err(e: ConstEvalError<'_>, db: &TestDB) -> String {
107+
fn pretty_print_err(e: ConstEvalError, db: &TestDB) -> String {
108108
let mut err = String::new();
109109
let span_formatter = |file, range| format!("{file:?} {range:?}");
110110
let display_target =
@@ -121,7 +121,7 @@ fn pretty_print_err(e: ConstEvalError<'_>, db: &TestDB) -> String {
121121
err
122122
}
123123

124-
fn eval_goal(db: &TestDB, file_id: EditionedFileId) -> Result<Const<'_>, ConstEvalError<'_>> {
124+
fn eval_goal(db: &TestDB, file_id: EditionedFileId) -> Result<Const<'_>, ConstEvalError> {
125125
let _tracing = setup_tracing();
126126
let interner = DbInterner::new_no_crate(db);
127127
let module_id = db.module_for_file(file_id.file_id(db));
@@ -142,7 +142,7 @@ fn eval_goal(db: &TestDB, file_id: EditionedFileId) -> Result<Const<'_>, ConstEv
142142
_ => None,
143143
})
144144
.expect("No const named GOAL found in the test");
145-
db.const_eval(const_id, GenericArgs::new_from_iter(interner, []), None)
145+
db.const_eval(const_id, GenericArgs::empty(interner), None)
146146
}
147147

148148
#[test]

0 commit comments

Comments
 (0)