Skip to content

Commit 83177a7

Browse files
committed
fix: address suggestions
1 parent 15f7300 commit 83177a7

File tree

12 files changed

+72
-65
lines changed

12 files changed

+72
-65
lines changed

crates/hir-def/src/body.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::{ast, AstNode, AstPtr, SyntaxNodePtr};
2424
use crate::{
2525
attr::{Attrs, RawAttrs},
2626
db::DefDatabase,
27-
expr::{Expr, ExprId, Label, LabelId, Pat, PatId},
27+
expr::{dummy_expr_id, Expr, ExprId, Label, LabelId, Pat, PatId},
2828
item_scope::BuiltinShadowMode,
2929
macro_id_to_def_id,
3030
nameres::DefMap,
@@ -238,7 +238,7 @@ pub struct Mark {
238238
}
239239

240240
/// The body of an item (function, const etc.).
241-
#[derive(Debug, Default, Eq, PartialEq)]
241+
#[derive(Debug, Eq, PartialEq)]
242242
pub struct Body {
243243
pub exprs: Arena<Expr>,
244244
pub pats: Arena<Pat>,
@@ -389,6 +389,21 @@ impl Body {
389389
}
390390
}
391391

392+
impl Default for Body {
393+
fn default() -> Self {
394+
Self {
395+
body_expr: dummy_expr_id(),
396+
exprs: Default::default(),
397+
pats: Default::default(),
398+
or_pats: Default::default(),
399+
labels: Default::default(),
400+
params: Default::default(),
401+
block_scopes: Default::default(),
402+
_c: Default::default(),
403+
}
404+
}
405+
}
406+
392407
impl Index<ExprId> for Body {
393408
type Output = Expr;
394409

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

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
22
//! representation.
33
4-
use std::{collections::HashMap, mem, sync::Arc};
4+
use std::{mem, sync::Arc};
55

66
use either::Either;
77
use hir_expand::{
@@ -10,6 +10,8 @@ use hir_expand::{
1010
name::{name, AsName, Name},
1111
ExpandError, HirFileId, InFile,
1212
};
13+
use la_arena::Arena;
14+
use profile::Count;
1315
use rustc_hash::FxHashMap;
1416
use syntax::{
1517
ast::{
@@ -26,8 +28,8 @@ use crate::{
2628
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
2729
db::DefDatabase,
2830
expr::{
29-
Array, BindingAnnotation, Expr, ExprId, FloatTypeWrapper, Label, LabelId, Literal,
30-
MatchArm, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
31+
dummy_expr_id, Array, BindingAnnotation, Expr, ExprId, FloatTypeWrapper, Label, LabelId,
32+
Literal, MatchArm, Pat, PatId, RecordFieldPat, RecordLitField, Statement,
3133
},
3234
intern::Interned,
3335
item_scope::BuiltinShadowMode,
@@ -80,7 +82,24 @@ pub(super) fn lower(
8082
params: Option<ast::ParamList>,
8183
body: Option<ast::Expr>,
8284
) -> (Body, BodySourceMap) {
83-
ExprCollector::new(db, expander).collect(params, body)
85+
ExprCollector {
86+
db,
87+
source_map: BodySourceMap::default(),
88+
body: Body {
89+
exprs: Arena::default(),
90+
pats: Arena::default(),
91+
labels: Arena::default(),
92+
params: Vec::new(),
93+
body_expr: dummy_expr_id(),
94+
block_scopes: Vec::new(),
95+
_c: Count::new(),
96+
or_pats: Default::default(),
97+
},
98+
expander,
99+
name_to_pat_grouping: Default::default(),
100+
is_lowering_inside_or_pat: false,
101+
}
102+
.collect(params, body)
84103
}
85104

86105
struct ExprCollector<'a> {
@@ -93,18 +112,7 @@ struct ExprCollector<'a> {
93112
is_lowering_inside_or_pat: bool,
94113
}
95114

96-
impl<'a> ExprCollector<'a> {
97-
pub(crate) fn new(db: &'a dyn DefDatabase, expander: Expander) -> Self {
98-
Self {
99-
db,
100-
expander,
101-
body: Body::default(),
102-
source_map: BodySourceMap::default(),
103-
name_to_pat_grouping: HashMap::default(),
104-
is_lowering_inside_or_pat: false,
105-
}
106-
}
107-
115+
impl ExprCollector<'_> {
108116
fn collect(
109117
mut self,
110118
param_list: Option<ast::ParamList>,
@@ -681,6 +689,7 @@ impl<'a> ExprCollector<'a> {
681689
};
682690
let prev_def_map = mem::replace(&mut self.expander.def_map, def_map);
683691
let prev_local_module = mem::replace(&mut self.expander.module, module);
692+
684693
let mut statements: Vec<_> =
685694
block.statements().filter_map(|s| self.collect_stmt(s)).collect();
686695
let tail = block.tail_expr().and_then(|e| self.maybe_collect_expr(e));

crates/hir-def/src/expr.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! See also a neighboring `body` module.
1414
1515
use hir_expand::name::Name;
16-
use la_arena::Idx;
16+
use la_arena::{Idx, RawIdx};
1717

1818
use crate::{
1919
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint},
@@ -27,6 +27,11 @@ pub use syntax::ast::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, Unar
2727

2828
pub type ExprId = Idx<Expr>;
2929

30+
/// FIXME: this is a hacky function which should be removed
31+
pub(crate) fn dummy_expr_id() -> ExprId {
32+
ExprId::from_raw(RawIdx::from(u32::MAX))
33+
}
34+
3035
pub type PatId = Idx<Pat>;
3136

3237
#[derive(Debug, Clone, Eq, PartialEq)]

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
22
3-
use crate::{
4-
intern::Interned,
5-
type_ref::{ConstScalar, ConstScalarOrPath},
6-
};
3+
use crate::{intern::Interned, type_ref::ConstScalarOrPath};
74

85
use either::Either;
96
use hir_expand::name::{name, AsName};
@@ -184,10 +181,7 @@ pub(super) fn lower_generic_args(
184181
}
185182
}
186183
ast::GenericArg::ConstArg(arg) => {
187-
let arg = arg.expr().map_or(
188-
ConstScalarOrPath::Scalar(ConstScalar::Unknown),
189-
ConstScalarOrPath::from_expr,
190-
);
184+
let arg = ConstScalarOrPath::from_expr_opt(arg.expr());
191185
args.push(GenericArg::Const(arg))
192186
}
193187
}

crates/hir-def/src/type_ref.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! HIR for references to types. Paths in these are not yet resolved. They can
22
//! be directly created from an ast::TypeRef, without further queries.
33
4+
use std::fmt::Write;
5+
46
use hir_expand::{
57
name::{AsName, Name},
68
AstId, InFile,
@@ -182,11 +184,7 @@ impl TypeRef {
182184
// `hir_def::body::lower` to lower this into an `Expr` and then evaluate it at the
183185
// `hir_ty` level, which would allow knowing the type of:
184186
// let v: [u8; 2 + 2] = [0u8; 4];
185-
let len = inner.expr().map_or(
186-
ConstScalarOrPath::Scalar(ConstScalar::Unknown),
187-
ConstScalarOrPath::from_expr,
188-
);
189-
187+
let len = ConstScalarOrPath::from_expr_opt(inner.expr());
190188
TypeRef::Array(Box::new(TypeRef::from_ast_opt(ctx, inner.ty())), len)
191189
}
192190
ast::Type::SliceType(inner) => {
@@ -394,9 +392,16 @@ impl std::fmt::Display for ConstScalarOrPath {
394392
}
395393

396394
impl ConstScalarOrPath {
395+
pub(crate) fn from_expr_opt(expr: Option<ast::Expr>) -> Self {
396+
match expr {
397+
Some(x) => Self::from_expr(x),
398+
None => Self::Scalar(ConstScalar::Unknown),
399+
}
400+
}
401+
397402
// FIXME: as per the comments on `TypeRef::Array`, this evaluation should not happen at this
398403
// parse stage.
399-
pub(crate) fn from_expr(expr: ast::Expr) -> Self {
404+
fn from_expr(expr: ast::Expr) -> Self {
400405
match expr {
401406
ast::Expr::PathExpr(p) => {
402407
match p.path().and_then(|x| x.segment()).and_then(|x| x.name_ref()) {
@@ -480,7 +485,7 @@ impl std::fmt::Display for ConstScalar {
480485
ConstScalar::UInt(num) => num.fmt(f),
481486
ConstScalar::Bool(flag) => flag.fmt(f),
482487
ConstScalar::Char(c) => write!(f, "'{c}'"),
483-
ConstScalar::Unknown => f.write_str("{unknown}"),
488+
ConstScalar::Unknown => f.write_char('_'),
484489
}
485490
}
486491
}

crates/hir-ty/src/consteval.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,14 @@ pub fn unknown_const_as_generic(ty: Ty) -> GenericArg {
395395
}
396396

397397
/// Interns a constant scalar with the given type
398-
pub fn intern_const_scalar_with_type(value: ConstScalar, ty: Ty) -> Const {
398+
pub fn intern_const_scalar(value: ConstScalar, ty: Ty) -> Const {
399399
ConstData { ty, value: ConstValue::Concrete(chalk_ir::ConcreteConst { interned: value }) }
400400
.intern(Interner)
401401
}
402402

403403
/// Interns a possibly-unknown target usize
404404
pub fn usize_const(value: Option<u128>) -> Const {
405-
intern_const_scalar_with_type(
406-
value.map(ConstScalar::UInt).unwrap_or(ConstScalar::Unknown),
407-
TyBuilder::usize(),
408-
)
409-
}
410-
411-
/// Interns a constant scalar with the default type
412-
pub fn intern_const_scalar(value: ConstScalar) -> Const {
413-
intern_const_scalar_with_type(value, TyBuilder::builtin(value.builtin_type()))
405+
intern_const_scalar(value.map_or(ConstScalar::Unknown, ConstScalar::UInt), TyBuilder::usize())
414406
}
415407

416408
pub(crate) fn const_eval_recover(
@@ -470,7 +462,7 @@ pub(crate) fn eval_to_const<'a>(
470462
Ok(ComputedExpr::Literal(literal)) => literal.into(),
471463
_ => ConstScalar::Unknown,
472464
};
473-
intern_const_scalar_with_type(const_scalar, TyBuilder::usize())
465+
intern_const_scalar(const_scalar, TyBuilder::usize())
474466
}
475467

476468
#[cfg(test)]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ impl<'a> InferenceContext<'a> {
273273
elem_ty.clone(),
274274
intern_const_scalar(
275275
len.map_or(ConstScalar::Unknown, |len| ConstScalar::UInt(len)),
276+
TyBuilder::usize(),
276277
),
277278
)
278279
}

crates/hir-ty/src/interner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl chalk_ir::interner::Interner for Interner {
257257
c1: &Self::InternedConcreteConst,
258258
c2: &Self::InternedConcreteConst,
259259
) -> bool {
260-
c1 == c2
260+
(c1 == &ConstScalar::Unknown) || (c2 == &ConstScalar::Unknown) || (c1 == c2)
261261
}
262262

263263
fn intern_generic_arg(

crates/hir-ty/src/lower.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ use syntax::{ast, SmolStr};
4444

4545
use crate::{
4646
all_super_traits,
47-
consteval::{
48-
intern_const_scalar_with_type, path_to_const, unknown_const, unknown_const_as_generic,
49-
},
47+
consteval::{intern_const_scalar, path_to_const, unknown_const, unknown_const_as_generic},
5048
db::HirDatabase,
5149
make_binders,
5250
mapping::ToChalk,
@@ -1744,7 +1742,7 @@ pub(crate) fn const_or_path_to_chalk(
17441742
debruijn: DebruijnIndex,
17451743
) -> Const {
17461744
match value {
1747-
ConstScalarOrPath::Scalar(s) => intern_const_scalar_with_type(s.clone(), expected_ty),
1745+
ConstScalarOrPath::Scalar(s) => intern_const_scalar(s.clone(), expected_ty),
17481746
ConstScalarOrPath::Path(n) => {
17491747
let path = ModPath::from_segments(PathKind::Plain, Some(n.clone()));
17501748
path_to_const(db, resolver, &path, mode, args, debruijn)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,14 +3011,14 @@ struct TS(usize);
30113011
fn main() {
30123012
let x;
30133013
[x,] = &[1,];
3014-
//^^^^expected &[i32; 1], got [{unknown}; {unknown}]
3014+
//^^^^expected &[i32; 1], got [{unknown}; _]
30153015
30163016
// FIXME we only want the outermost error, but this matches the current
30173017
// behavior of slice patterns
30183018
let x;
30193019
[(x,),] = &[(1,),];
30203020
// ^^^^expected {unknown}, got ({unknown},)
3021-
//^^^^^^^expected &[(i32,); 1], got [{unknown}; {unknown}]
3021+
//^^^^^^^expected &[(i32,); 1], got [{unknown}; _]
30223022
30233023
let x;
30243024
((x,),) = &((1,),);

crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn div(x: i32, y: i32) -> Option<i32> {
328328
}
329329
fn main() {
330330
run(f()) // FIXME: remove this error
331-
//^^^ error: expected Rate<5>, found Rate<{unknown}>
331+
//^^^ error: expected Rate<5>, found Rate<_>
332332
}
333333
"#,
334334
);

lib/la-arena/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ pub use map::ArenaMap;
1717
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1818
pub struct RawIdx(u32);
1919

20-
impl Default for RawIdx {
21-
fn default() -> Self {
22-
Self(u32::MAX)
23-
}
24-
}
25-
2620
impl From<RawIdx> for u32 {
2721
fn from(raw: RawIdx) -> u32 {
2822
raw.0
@@ -53,12 +47,6 @@ pub struct Idx<T> {
5347
_ty: PhantomData<fn() -> T>,
5448
}
5549

56-
impl<T> Default for Idx<T> {
57-
fn default() -> Self {
58-
Self::from_raw(RawIdx::default())
59-
}
60-
}
61-
6250
impl<T> Clone for Idx<T> {
6351
fn clone(&self) -> Self {
6452
*self

0 commit comments

Comments
 (0)