Skip to content

Commit 56bee2d

Browse files
committed
Auto merge of #17135 - Veykril:inline-const-scope, r=Veykril
fix: Fix expression scopes not being calculated for inline consts
2 parents 935de9d + ac389ce commit 56bee2d

File tree

7 files changed

+69
-34
lines changed

7 files changed

+69
-34
lines changed

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

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
body::Body,
88
db::DefDatabase,
99
hir::{Binding, BindingId, Expr, ExprId, LabelId, Pat, PatId, Statement},
10-
BlockId, DefWithBodyId,
10+
BlockId, ConstBlockId, DefWithBodyId,
1111
};
1212

1313
pub type ScopeId = Idx<ScopeData>;
@@ -46,7 +46,9 @@ pub struct ScopeData {
4646
impl ExprScopes {
4747
pub(crate) fn expr_scopes_query(db: &dyn DefDatabase, def: DefWithBodyId) -> Arc<ExprScopes> {
4848
let body = db.body(def);
49-
let mut scopes = ExprScopes::new(&body);
49+
let mut scopes = ExprScopes::new(&body, |const_block| {
50+
db.lookup_intern_anonymous_const(const_block).root
51+
});
5052
scopes.shrink_to_fit();
5153
Arc::new(scopes)
5254
}
@@ -89,7 +91,10 @@ fn empty_entries(idx: usize) -> IdxRange<ScopeEntry> {
8991
}
9092

9193
impl ExprScopes {
92-
fn new(body: &Body) -> ExprScopes {
94+
fn new(
95+
body: &Body,
96+
resolve_const_block: impl (Fn(ConstBlockId) -> ExprId) + Copy,
97+
) -> ExprScopes {
9398
let mut scopes = ExprScopes {
9499
scopes: Arena::default(),
95100
scope_entries: Arena::default(),
@@ -100,7 +105,7 @@ impl ExprScopes {
100105
scopes.add_bindings(body, root, self_param);
101106
}
102107
scopes.add_params_bindings(body, root, &body.params);
103-
compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root);
108+
compute_expr_scopes(body.body_expr, body, &mut scopes, &mut root, resolve_const_block);
104109
scopes
105110
}
106111

@@ -183,89 +188,101 @@ fn compute_block_scopes(
183188
body: &Body,
184189
scopes: &mut ExprScopes,
185190
scope: &mut ScopeId,
191+
resolve_const_block: impl (Fn(ConstBlockId) -> ExprId) + Copy,
186192
) {
187193
for stmt in statements {
188194
match stmt {
189195
Statement::Let { pat, initializer, else_branch, .. } => {
190196
if let Some(expr) = initializer {
191-
compute_expr_scopes(*expr, body, scopes, scope);
197+
compute_expr_scopes(*expr, body, scopes, scope, resolve_const_block);
192198
}
193199
if let Some(expr) = else_branch {
194-
compute_expr_scopes(*expr, body, scopes, scope);
200+
compute_expr_scopes(*expr, body, scopes, scope, resolve_const_block);
195201
}
196202

197203
*scope = scopes.new_scope(*scope);
198204
scopes.add_pat_bindings(body, *scope, *pat);
199205
}
200206
Statement::Expr { expr, .. } => {
201-
compute_expr_scopes(*expr, body, scopes, scope);
207+
compute_expr_scopes(*expr, body, scopes, scope, resolve_const_block);
202208
}
203209
Statement::Item => (),
204210
}
205211
}
206212
if let Some(expr) = tail {
207-
compute_expr_scopes(expr, body, scopes, scope);
213+
compute_expr_scopes(expr, body, scopes, scope, resolve_const_block);
208214
}
209215
}
210216

211-
fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope: &mut ScopeId) {
217+
fn compute_expr_scopes(
218+
expr: ExprId,
219+
body: &Body,
220+
scopes: &mut ExprScopes,
221+
scope: &mut ScopeId,
222+
resolve_const_block: impl (Fn(ConstBlockId) -> ExprId) + Copy,
223+
) {
212224
let make_label =
213225
|label: &Option<LabelId>| label.map(|label| (label, body.labels[label].name.clone()));
214226

227+
let compute_expr_scopes = |scopes: &mut ExprScopes, expr: ExprId, scope: &mut ScopeId| {
228+
compute_expr_scopes(expr, body, scopes, scope, resolve_const_block)
229+
};
230+
215231
scopes.set_scope(expr, *scope);
216232
match &body[expr] {
217233
Expr::Block { statements, tail, id, label } => {
218234
let mut scope = scopes.new_block_scope(*scope, *id, make_label(label));
219235
// Overwrite the old scope for the block expr, so that every block scope can be found
220236
// via the block itself (important for blocks that only contain items, no expressions).
221237
scopes.set_scope(expr, scope);
222-
compute_block_scopes(statements, *tail, body, scopes, &mut scope);
238+
compute_block_scopes(statements, *tail, body, scopes, &mut scope, resolve_const_block);
223239
}
224-
Expr::Const(_) => {
225-
// FIXME: This is broken.
240+
Expr::Const(id) => {
241+
let mut scope = scopes.root_scope();
242+
compute_expr_scopes(scopes, resolve_const_block(*id), &mut scope);
226243
}
227244
Expr::Unsafe { id, statements, tail } | Expr::Async { id, statements, tail } => {
228245
let mut scope = scopes.new_block_scope(*scope, *id, None);
229246
// Overwrite the old scope for the block expr, so that every block scope can be found
230247
// via the block itself (important for blocks that only contain items, no expressions).
231248
scopes.set_scope(expr, scope);
232-
compute_block_scopes(statements, *tail, body, scopes, &mut scope);
249+
compute_block_scopes(statements, *tail, body, scopes, &mut scope, resolve_const_block);
233250
}
234251
Expr::Loop { body: body_expr, label } => {
235252
let mut scope = scopes.new_labeled_scope(*scope, make_label(label));
236-
compute_expr_scopes(*body_expr, body, scopes, &mut scope);
253+
compute_expr_scopes(scopes, *body_expr, &mut scope);
237254
}
238255
Expr::Closure { args, body: body_expr, .. } => {
239256
let mut scope = scopes.new_scope(*scope);
240257
scopes.add_params_bindings(body, scope, args);
241-
compute_expr_scopes(*body_expr, body, scopes, &mut scope);
258+
compute_expr_scopes(scopes, *body_expr, &mut scope);
242259
}
243260
Expr::Match { expr, arms } => {
244-
compute_expr_scopes(*expr, body, scopes, scope);
261+
compute_expr_scopes(scopes, *expr, scope);
245262
for arm in arms.iter() {
246263
let mut scope = scopes.new_scope(*scope);
247264
scopes.add_pat_bindings(body, scope, arm.pat);
248265
if let Some(guard) = arm.guard {
249266
scope = scopes.new_scope(scope);
250-
compute_expr_scopes(guard, body, scopes, &mut scope);
267+
compute_expr_scopes(scopes, guard, &mut scope);
251268
}
252-
compute_expr_scopes(arm.expr, body, scopes, &mut scope);
269+
compute_expr_scopes(scopes, arm.expr, &mut scope);
253270
}
254271
}
255272
&Expr::If { condition, then_branch, else_branch } => {
256273
let mut then_branch_scope = scopes.new_scope(*scope);
257-
compute_expr_scopes(condition, body, scopes, &mut then_branch_scope);
258-
compute_expr_scopes(then_branch, body, scopes, &mut then_branch_scope);
274+
compute_expr_scopes(scopes, condition, &mut then_branch_scope);
275+
compute_expr_scopes(scopes, then_branch, &mut then_branch_scope);
259276
if let Some(else_branch) = else_branch {
260-
compute_expr_scopes(else_branch, body, scopes, scope);
277+
compute_expr_scopes(scopes, else_branch, scope);
261278
}
262279
}
263280
&Expr::Let { pat, expr } => {
264-
compute_expr_scopes(expr, body, scopes, scope);
281+
compute_expr_scopes(scopes, expr, scope);
265282
*scope = scopes.new_scope(*scope);
266283
scopes.add_pat_bindings(body, *scope, pat);
267284
}
268-
e => e.walk_child_exprs(|e| compute_expr_scopes(e, body, scopes, scope)),
285+
e => e.walk_child_exprs(|e| compute_expr_scopes(scopes, e, scope)),
269286
};
270287
}
271288

crates/hir-def/src/item_tree.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ use crate::{
6868
pub struct RawVisibilityId(u32);
6969

7070
impl RawVisibilityId {
71-
pub const PUB: Self = RawVisibilityId(u32::max_value());
72-
pub const PRIV_IMPLICIT: Self = RawVisibilityId(u32::max_value() - 1);
73-
pub const PRIV_EXPLICIT: Self = RawVisibilityId(u32::max_value() - 2);
74-
pub const PUB_CRATE: Self = RawVisibilityId(u32::max_value() - 3);
71+
pub const PUB: Self = RawVisibilityId(u32::MAX);
72+
pub const PRIV_IMPLICIT: Self = RawVisibilityId(u32::MAX - 1);
73+
pub const PRIV_EXPLICIT: Self = RawVisibilityId(u32::MAX - 2);
74+
pub const PUB_CRATE: Self = RawVisibilityId(u32::MAX - 3);
7575
}
7676

7777
impl fmt::Debug for RawVisibilityId {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,3 +3664,21 @@ fn main() {
36643664
"#,
36653665
);
36663666
}
3667+
3668+
#[test]
3669+
fn inline_const_expression() {
3670+
check(
3671+
r#"
3672+
fn main() {
3673+
let foo = 0;
3674+
const {
3675+
let bar = 1;
3676+
let unresolved = foo;
3677+
// ^^^^^^^^^^ type: {unknown}
3678+
let resolved = bar;
3679+
// ^^^^^^^^ type: i32
3680+
}
3681+
}
3682+
"#,
3683+
);
3684+
}

crates/ide-assists/src/handlers/reorder_impl_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
7777
ast::AssocItem::MacroCall(_) => None,
7878
};
7979

80-
name.and_then(|n| ranks.get(&n.to_string()).copied()).unwrap_or(usize::max_value())
80+
name.and_then(|n| ranks.get(&n.to_string()).copied()).unwrap_or(usize::MAX)
8181
})
8282
.collect();
8383

crates/ide-db/src/symbol_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ impl SymbolIndex {
301301
}
302302

303303
fn range_to_map_value(start: usize, end: usize) -> u64 {
304-
debug_assert![start <= (std::u32::MAX as usize)];
305-
debug_assert![end <= (std::u32::MAX as usize)];
304+
debug_assert![start <= (u32::MAX as usize)];
305+
debug_assert![end <= (u32::MAX as usize)];
306306

307307
((start as u64) << 32) | end as u64
308308
}

crates/salsa/src/lru.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ where
285285

286286
impl Default for LruIndex {
287287
fn default() -> Self {
288-
Self { index: AtomicUsize::new(std::usize::MAX) }
288+
Self { index: AtomicUsize::new(usize::MAX) }
289289
}
290290
}
291291

@@ -299,11 +299,11 @@ impl LruIndex {
299299
}
300300

301301
fn clear(&self) {
302-
self.store(std::usize::MAX);
302+
self.store(usize::MAX);
303303
}
304304

305305
fn is_in_lru(&self) -> bool {
306-
self.load() != std::usize::MAX
306+
self.load() != usize::MAX
307307
}
308308
}
309309

crates/salsa/src/revision.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl AtomicRevision {
6060
/// Increment by 1, returning previous value.
6161
pub(crate) fn fetch_then_increment(&self) -> Revision {
6262
let v = self.data.fetch_add(1, Ordering::SeqCst);
63-
assert!(v != u32::max_value(), "revision overflow");
63+
assert!(v != u32::MAX, "revision overflow");
6464
Revision::from(v)
6565
}
6666
}

0 commit comments

Comments
 (0)