Skip to content

Commit 9a22aa8

Browse files
committed
Cleaned up code based on feedback
1 parent 985b48b commit 9a22aa8

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
@@ -167,12 +167,11 @@ fn get_name(variant: EnumVariantId, ctx: &mut ConstEvalCtx<'_>) -> String {
167167
pub fn eval_const(
168168
expr_id: ExprId,
169169
ctx: &mut ConstEvalCtx<'_>,
170-
variant: Option<EnumVariantId>,
171170
) -> Result<ComputedExpr, ConstEvalError> {
172171
let expr = &ctx.exprs[expr_id];
173172
match expr {
174-
Expr::Missing => match variant {
175-
Some(variant) => {
173+
Expr::Missing => match ctx.owner {
174+
DefWithBodyId::VariantId(variant) => {
176175
let prev_idx: u32 = variant.local_id.into_raw().into();
177176
let prev_idx = prev_idx.checked_sub(1).map(|idx| Idx::from_raw(RawIdx::from(idx)));
178177
let value = match prev_idx {
@@ -199,7 +198,7 @@ pub fn eval_const(
199198
Expr::Literal(l) => Ok(ComputedExpr::Literal(l.clone())),
200199
&Expr::UnaryOp { expr, op } => {
201200
let ty = &ctx.expr_ty(expr);
202-
let ev = eval_const(expr, ctx, None)?;
201+
let ev = eval_const(expr, ctx)?;
203202
match op {
204203
hir_def::expr::UnaryOp::Deref => Err(ConstEvalError::NotSupported("deref")),
205204
hir_def::expr::UnaryOp::Not => {
@@ -255,8 +254,8 @@ pub fn eval_const(
255254
}
256255
&Expr::BinaryOp { lhs, rhs, op } => {
257256
let ty = &ctx.expr_ty(lhs);
258-
let lhs = eval_const(lhs, ctx, None)?;
259-
let rhs = eval_const(rhs, ctx, None)?;
257+
let lhs = eval_const(lhs, ctx)?;
258+
let rhs = eval_const(rhs, ctx)?;
260259
let op = op.ok_or(ConstEvalError::IncompleteExpr)?;
261260
let v1 = match lhs {
262261
ComputedExpr::Literal(Literal::Int(v, _)) => v,
@@ -317,7 +316,7 @@ pub fn eval_const(
317316
}
318317
};
319318
let value = match initializer {
320-
Some(x) => eval_const(x, ctx, None)?,
319+
Some(x) => eval_const(x, ctx)?,
321320
None => continue,
322321
};
323322
if !prev_values.contains_key(&pat_id) {
@@ -333,7 +332,7 @@ pub fn eval_const(
333332
}
334333
}
335334
let r = match tail {
336-
&Some(x) => eval_const(x, ctx, None),
335+
&Some(x) => eval_const(x, ctx),
337336
None => Ok(ComputedExpr::Tuple(Box::new([]))),
338337
};
339338
// clean up local data, so caller will receive the exact map that passed to us
@@ -391,7 +390,7 @@ pub fn eval_const(
391390
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
392391
}
393392
}
394-
&Expr::Cast { expr, .. } => match eval_const(expr, ctx, None)? {
393+
&Expr::Cast { expr, .. } => match eval_const(expr, ctx)? {
395394
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
396395
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
397396
},
@@ -490,7 +489,6 @@ pub(crate) fn const_eval_query(
490489
local_data: HashMap::default(),
491490
infer,
492491
},
493-
None,
494492
);
495493
result
496494
}
@@ -512,7 +510,6 @@ pub(crate) fn const_eval_query_variant(
512510
local_data: HashMap::default(),
513511
infer,
514512
},
515-
Some(variant_id),
516513
)
517514
}
518515

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

crates/hir/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ impl Enum {
932932
}
933933

934934
pub fn is_data_carrying(self, db: &dyn HirDatabase) -> bool {
935-
self.variants(db).iter().all(|v| matches!(v.kind(db), StructKind::Unit))
935+
self.variants(db).iter().any(|v| !matches!(v.kind(db), StructKind::Unit))
936936
}
937937
}
938938

@@ -944,8 +944,8 @@ impl HasVisibility for Enum {
944944

945945
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
946946
pub struct Variant {
947-
pub parent: Enum,
948-
pub id: LocalEnumVariantId,
947+
pub(crate) parent: Enum,
948+
pub(crate) id: LocalEnumVariantId,
949949
}
950950

951951
impl Variant {

crates/ide/src/hover/render.rs

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

0 commit comments

Comments
 (0)