Skip to content

Commit 5313bd1

Browse files
committed
Cleaned up code based on feedback
1 parent 301b889 commit 5313bd1

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

crates/hir-ty/src/consteval.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,11 @@ fn get_name(variant: EnumVariantId, ctx: &mut ConstEvalCtx<'_>) -> String {
166166
pub fn eval_const(
167167
expr_id: ExprId,
168168
ctx: &mut ConstEvalCtx<'_>,
169-
variant: Option<EnumVariantId>,
170169
) -> Result<ComputedExpr, ConstEvalError> {
171170
let expr = &ctx.exprs[expr_id];
172171
match expr {
173-
Expr::Missing => match variant {
174-
Some(variant) => {
172+
Expr::Missing => match ctx.owner {
173+
DefWithBodyId::VariantId(variant) => {
175174
let prev_idx: u32 = variant.local_id.into_raw().into();
176175
let prev_idx = prev_idx.checked_sub(1).map(|idx| Idx::from_raw(RawIdx::from(idx)));
177176
let value = match prev_idx {
@@ -198,7 +197,7 @@ pub fn eval_const(
198197
Expr::Literal(l) => Ok(ComputedExpr::Literal(l.clone())),
199198
&Expr::UnaryOp { expr, op } => {
200199
let ty = &ctx.expr_ty(expr);
201-
let ev = eval_const(expr, ctx, None)?;
200+
let ev = eval_const(expr, ctx)?;
202201
match op {
203202
hir_def::expr::UnaryOp::Deref => Err(ConstEvalError::NotSupported("deref")),
204203
hir_def::expr::UnaryOp::Not => {
@@ -254,8 +253,8 @@ pub fn eval_const(
254253
}
255254
&Expr::BinaryOp { lhs, rhs, op } => {
256255
let ty = &ctx.expr_ty(lhs);
257-
let lhs = eval_const(lhs, ctx, None)?;
258-
let rhs = eval_const(rhs, ctx, None)?;
256+
let lhs = eval_const(lhs, ctx)?;
257+
let rhs = eval_const(rhs, ctx)?;
259258
let op = op.ok_or(ConstEvalError::IncompleteExpr)?;
260259
let v1 = match lhs {
261260
ComputedExpr::Literal(Literal::Int(v, _)) => v,
@@ -316,7 +315,7 @@ pub fn eval_const(
316315
}
317316
};
318317
let value = match initializer {
319-
Some(x) => eval_const(x, ctx, None)?,
318+
Some(x) => eval_const(x, ctx)?,
320319
None => continue,
321320
};
322321
if !prev_values.contains_key(&pat_id) {
@@ -332,7 +331,7 @@ pub fn eval_const(
332331
}
333332
}
334333
let r = match tail {
335-
&Some(x) => eval_const(x, ctx, None),
334+
&Some(x) => eval_const(x, ctx),
336335
None => Ok(ComputedExpr::Tuple(Box::new([]))),
337336
};
338337
// clean up local data, so caller will receive the exact map that passed to us
@@ -390,7 +389,7 @@ pub fn eval_const(
390389
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
391390
}
392391
}
393-
&Expr::Cast { expr, .. } => match eval_const(expr, ctx, None)? {
392+
&Expr::Cast { expr, .. } => match eval_const(expr, ctx)? {
394393
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
395394
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
396395
},
@@ -489,7 +488,6 @@ pub(crate) fn const_eval_query(
489488
local_data: HashMap::default(),
490489
infer,
491490
},
492-
None,
493491
);
494492
result
495493
}
@@ -511,7 +509,6 @@ pub(crate) fn const_eval_query_variant(
511509
local_data: HashMap::default(),
512510
infer,
513511
},
514-
Some(variant_id),
515512
)
516513
}
517514

@@ -538,7 +535,7 @@ pub(crate) fn eval_to_const<'a>(
538535
local_data: HashMap::default(),
539536
infer: &ctx.result,
540537
};
541-
let computed_expr = eval_const(expr, &mut ctx, None);
538+
let computed_expr = eval_const(expr, &mut ctx);
542539
let const_scalar = match computed_expr {
543540
Ok(ComputedExpr::Literal(literal)) => literal.into(),
544541
_ => ConstScalar::Unknown,

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl Enum {
954954
}
955955

956956
pub fn is_data_carrying(self, db: &dyn HirDatabase) -> bool {
957-
self.variants(db).iter().all(|v| matches!(v.kind(db), StructKind::Unit))
957+
self.variants(db).iter().any(|v| !matches!(v.kind(db), StructKind::Unit))
958958
}
959959
}
960960

@@ -966,8 +966,8 @@ impl HasVisibility for Enum {
966966

967967
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
968968
pub struct Variant {
969-
pub parent: Enum,
970-
pub id: LocalEnumVariantId,
969+
pub(crate) parent: Enum,
970+
pub(crate) id: LocalEnumVariantId,
971971
}
972972

973973
impl Variant {

crates/ide/src/hover/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ pub(super) fn definition(
349349
Definition::Function(it) => label_and_docs(db, it),
350350
Definition::Adt(it) => label_and_docs(db, it),
351351
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
352-
if it.parent.is_data_carrying(db) {
352+
if !it.parent_enum(db).is_data_carrying(db) {
353353
match it.eval(db) {
354354
Ok(x) => Some(format!("{}", x)),
355355
Err(_) => it.value(db).map(|x| format!("{:?}", x)),

0 commit comments

Comments
 (0)