Skip to content

Commit 76726f1

Browse files
committed
MIR episode 5
1 parent 9b33874 commit 76726f1

39 files changed

+1566
-384
lines changed

Cargo.lock

Lines changed: 29 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir-def/src/body.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl Body {
165165
};
166166
let expander = Expander::new(db, file_id, module);
167167
let (mut body, source_map) =
168-
Body::new(db, expander, params, body, module.krate, is_async_fn);
168+
Body::new(db, def, expander, params, body, module.krate, is_async_fn);
169169
body.shrink_to_fit();
170170

171171
(Arc::new(body), Arc::new(source_map))
@@ -189,13 +189,14 @@ impl Body {
189189

190190
fn new(
191191
db: &dyn DefDatabase,
192+
owner: DefWithBodyId,
192193
expander: Expander,
193194
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
194195
body: Option<ast::Expr>,
195196
krate: CrateId,
196197
is_async_fn: bool,
197198
) -> (Body, BodySourceMap) {
198-
lower::lower(db, expander, params, body, krate, is_async_fn)
199+
lower::lower(db, owner, expander, params, body, krate, is_async_fn)
199200
}
200201

201202
fn shrink_to_fit(&mut self) {

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

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

4646
pub(super) fn lower(
4747
db: &dyn DefDatabase,
48+
owner: DefWithBodyId,
4849
expander: Expander,
4950
params: Option<(ast::ParamList, impl Iterator<Item = bool>)>,
5051
body: Option<ast::Expr>,
@@ -53,6 +54,7 @@ pub(super) fn lower(
5354
) -> (Body, BodySourceMap) {
5455
ExprCollector {
5556
db,
57+
owner,
5658
krate,
5759
def_map: expander.module.def_map(db),
5860
source_map: BodySourceMap::default(),
@@ -80,6 +82,7 @@ pub(super) fn lower(
8082
struct ExprCollector<'a> {
8183
db: &'a dyn DefDatabase,
8284
expander: Expander,
85+
owner: DefWithBodyId,
8386
def_map: Arc<DefMap>,
8487
ast_id_map: Arc<AstIdMap>,
8588
krate: CrateId,
@@ -269,16 +272,13 @@ impl ExprCollector<'_> {
269272
}
270273
Some(ast::BlockModifier::Const(_)) => {
271274
self.with_label_rib(RibKind::Constant, |this| {
272-
this.collect_as_a_binding_owner_bad(
273-
|this| {
274-
this.collect_block_(e, |id, statements, tail| Expr::Const {
275-
id,
276-
statements,
277-
tail,
278-
})
279-
},
280-
syntax_ptr,
281-
)
275+
let (result_expr_id, prev_binding_owner) =
276+
this.initialize_binding_owner(syntax_ptr);
277+
let inner_expr = this.collect_block(e);
278+
let x = this.db.intern_anonymous_const((this.owner, inner_expr));
279+
this.body.exprs[result_expr_id] = Expr::Const(x);
280+
this.current_binding_owner = prev_binding_owner;
281+
result_expr_id
282282
})
283283
}
284284
None => self.collect_block(e),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ impl<'a> Printer<'a> {
436436
Expr::Async { id: _, statements, tail } => {
437437
self.print_block(Some("async "), statements, tail);
438438
}
439-
Expr::Const { id: _, statements, tail } => {
440-
self.print_block(Some("const "), statements, tail);
439+
Expr::Const(id) => {
440+
w!(self, "const {{ /* {id:?} */ }}");
441441
}
442442
}
443443
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,10 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
218218
scopes.set_scope(expr, scope);
219219
compute_block_scopes(statements, *tail, body, scopes, &mut scope);
220220
}
221-
Expr::Unsafe { id, statements, tail }
222-
| Expr::Async { id, statements, tail }
223-
| Expr::Const { id, statements, tail } => {
221+
Expr::Const(_) => {
222+
// FIXME: This is broken.
223+
}
224+
Expr::Unsafe { id, statements, tail } | Expr::Async { id, statements, tail } => {
224225
let mut scope = scopes.new_block_scope(*scope, *id, None);
225226
// Overwrite the old scope for the block expr, so that every block scope can be found
226227
// via the block itself (important for blocks that only contain items, no expressions).

crates/hir-def/src/db.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ use crate::{
1616
TraitAliasData, TraitData, TypeAliasData,
1717
},
1818
generics::GenericParams,
19+
hir::ExprId,
1920
import_map::ImportMap,
2021
item_tree::{AttrOwner, ItemTree},
2122
lang_item::{LangItem, LangItemTarget, LangItems},
2223
nameres::{diagnostics::DefDiagnostic, DefMap},
2324
visibility::{self, Visibility},
24-
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, ExternBlockId,
25-
ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId,
26-
LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc,
27-
StaticId, StaticLoc, StructId, StructLoc, TraitAliasId, TraitAliasLoc, TraitId, TraitLoc,
28-
TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId,
25+
AnonymousConstId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId,
26+
EnumLoc, ExternBlockId, ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc,
27+
LocalEnumVariantId, LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc,
28+
ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitAliasId,
29+
TraitAliasLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, VariantId,
2930
};
3031

3132
#[salsa::query_group(InternDatabaseStorage)]
@@ -220,6 +221,9 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
220221
fn recursion_limit(&self, crate_id: CrateId) -> u32;
221222

222223
fn crate_supports_no_std(&self, crate_id: CrateId) -> bool;
224+
225+
#[salsa::interned]
226+
fn intern_anonymous_const(&self, id: (DefWithBodyId, ExprId)) -> AnonymousConstId;
223227
}
224228

225229
fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {

crates/hir-def/src/hir.rs

Lines changed: 4 additions & 8 deletions
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-
BlockId,
29+
AnonymousConstId, BlockId,
3030
};
3131

3232
pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp};
@@ -169,11 +169,7 @@ pub enum Expr {
169169
statements: Box<[Statement]>,
170170
tail: Option<ExprId>,
171171
},
172-
Const {
173-
id: Option<BlockId>,
174-
statements: Box<[Statement]>,
175-
tail: Option<ExprId>,
176-
},
172+
Const(AnonymousConstId),
177173
Unsafe {
178174
id: Option<BlockId>,
179175
statements: Box<[Statement]>,
@@ -355,10 +351,10 @@ impl Expr {
355351
Expr::Let { expr, .. } => {
356352
f(*expr);
357353
}
354+
Expr::Const(_) => (),
358355
Expr::Block { statements, tail, .. }
359356
| Expr::Unsafe { statements, tail, .. }
360-
| Expr::Async { statements, tail, .. }
361-
| Expr::Const { statements, tail, .. } => {
357+
| Expr::Async { statements, tail, .. } => {
362358
for stmt in statements.iter() {
363359
match stmt {
364360
Statement::Let { initializer, else_branch, .. } => {

crates/hir-def/src/lib.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub mod visibility;
4848
pub mod find_path;
4949
pub mod import_map;
5050

51+
use db::DefDatabase;
5152
pub use rustc_abi as layout;
5253
use triomphe::Arc;
5354

@@ -59,7 +60,11 @@ mod pretty;
5960

6061
use std::hash::{Hash, Hasher};
6162

62-
use base_db::{impl_intern_key, salsa, CrateId, ProcMacroKind};
63+
use base_db::{
64+
impl_intern_key,
65+
salsa::{self, InternId},
66+
CrateId, ProcMacroKind,
67+
};
6368
use hir_expand::{
6469
ast_id_map::FileAstId,
6570
attrs::{Attr, AttrId, AttrInput},
@@ -472,6 +477,46 @@ impl_from!(
472477
for ModuleDefId
473478
);
474479

480+
// FIXME: make this a DefWithBodyId
481+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
482+
pub struct AnonymousConstId(InternId);
483+
impl_intern_key!(AnonymousConstId);
484+
485+
/// A constant, which might appears as a const item, an annonymous const block in expressions
486+
/// or patterns, or as a constant in types with const generics.
487+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
488+
pub enum GeneralConstId {
489+
ConstId(ConstId),
490+
AnonymousConstId(AnonymousConstId),
491+
}
492+
493+
impl_from!(ConstId, AnonymousConstId for GeneralConstId);
494+
495+
impl GeneralConstId {
496+
pub fn generic_def(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
497+
match self {
498+
GeneralConstId::ConstId(x) => Some(x.into()),
499+
GeneralConstId::AnonymousConstId(x) => {
500+
let (parent, _) = db.lookup_intern_anonymous_const(x);
501+
parent.as_generic_def_id()
502+
}
503+
}
504+
}
505+
506+
pub fn name(self, db: &dyn DefDatabase) -> String {
507+
match self {
508+
GeneralConstId::ConstId(const_id) => db
509+
.const_data(const_id)
510+
.name
511+
.as_ref()
512+
.and_then(|x| x.as_str())
513+
.unwrap_or("_")
514+
.to_owned(),
515+
GeneralConstId::AnonymousConstId(id) => format!("{{anonymous const {id:?}}}"),
516+
}
517+
}
518+
}
519+
475520
/// The defs which have a body.
476521
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
477522
pub enum DefWithBodyId {

crates/hir-def/src/pretty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use crate::{
1212
};
1313

1414
pub(crate) fn print_path(path: &Path, buf: &mut dyn Write) -> fmt::Result {
15+
if let Path::LangItem(x) = path {
16+
return write!(buf, "$lang_item::{x:?}");
17+
}
1518
match path.type_anchor() {
1619
Some(anchor) => {
1720
write!(buf, "<")?;

crates/hir-expand/src/name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ pub mod known {
376376
deref,
377377
div_assign,
378378
div,
379+
drop,
379380
fn_mut,
380381
fn_once,
381382
future_trait,

0 commit comments

Comments
 (0)