mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-29 05:14:18 +00:00
Cleaned up code based on feedback
This commit is contained in:
parent
301b8894ea
commit
5313bd1984
3 changed files with 13 additions and 16 deletions
|
@ -166,12 +166,11 @@ fn get_name(variant: EnumVariantId, ctx: &mut ConstEvalCtx<'_>) -> String {
|
||||||
pub fn eval_const(
|
pub fn eval_const(
|
||||||
expr_id: ExprId,
|
expr_id: ExprId,
|
||||||
ctx: &mut ConstEvalCtx<'_>,
|
ctx: &mut ConstEvalCtx<'_>,
|
||||||
variant: Option<EnumVariantId>,
|
|
||||||
) -> Result<ComputedExpr, ConstEvalError> {
|
) -> Result<ComputedExpr, ConstEvalError> {
|
||||||
let expr = &ctx.exprs[expr_id];
|
let expr = &ctx.exprs[expr_id];
|
||||||
match expr {
|
match expr {
|
||||||
Expr::Missing => match variant {
|
Expr::Missing => match ctx.owner {
|
||||||
Some(variant) => {
|
DefWithBodyId::VariantId(variant) => {
|
||||||
let prev_idx: u32 = variant.local_id.into_raw().into();
|
let prev_idx: u32 = variant.local_id.into_raw().into();
|
||||||
let prev_idx = prev_idx.checked_sub(1).map(|idx| Idx::from_raw(RawIdx::from(idx)));
|
let prev_idx = prev_idx.checked_sub(1).map(|idx| Idx::from_raw(RawIdx::from(idx)));
|
||||||
let value = match prev_idx {
|
let value = match prev_idx {
|
||||||
|
@ -198,7 +197,7 @@ pub fn eval_const(
|
||||||
Expr::Literal(l) => Ok(ComputedExpr::Literal(l.clone())),
|
Expr::Literal(l) => Ok(ComputedExpr::Literal(l.clone())),
|
||||||
&Expr::UnaryOp { expr, op } => {
|
&Expr::UnaryOp { expr, op } => {
|
||||||
let ty = &ctx.expr_ty(expr);
|
let ty = &ctx.expr_ty(expr);
|
||||||
let ev = eval_const(expr, ctx, None)?;
|
let ev = eval_const(expr, ctx)?;
|
||||||
match op {
|
match op {
|
||||||
hir_def::expr::UnaryOp::Deref => Err(ConstEvalError::NotSupported("deref")),
|
hir_def::expr::UnaryOp::Deref => Err(ConstEvalError::NotSupported("deref")),
|
||||||
hir_def::expr::UnaryOp::Not => {
|
hir_def::expr::UnaryOp::Not => {
|
||||||
|
@ -254,8 +253,8 @@ pub fn eval_const(
|
||||||
}
|
}
|
||||||
&Expr::BinaryOp { lhs, rhs, op } => {
|
&Expr::BinaryOp { lhs, rhs, op } => {
|
||||||
let ty = &ctx.expr_ty(lhs);
|
let ty = &ctx.expr_ty(lhs);
|
||||||
let lhs = eval_const(lhs, ctx, None)?;
|
let lhs = eval_const(lhs, ctx)?;
|
||||||
let rhs = eval_const(rhs, ctx, None)?;
|
let rhs = eval_const(rhs, ctx)?;
|
||||||
let op = op.ok_or(ConstEvalError::IncompleteExpr)?;
|
let op = op.ok_or(ConstEvalError::IncompleteExpr)?;
|
||||||
let v1 = match lhs {
|
let v1 = match lhs {
|
||||||
ComputedExpr::Literal(Literal::Int(v, _)) => v,
|
ComputedExpr::Literal(Literal::Int(v, _)) => v,
|
||||||
|
@ -316,7 +315,7 @@ pub fn eval_const(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let value = match initializer {
|
let value = match initializer {
|
||||||
Some(x) => eval_const(x, ctx, None)?,
|
Some(x) => eval_const(x, ctx)?,
|
||||||
None => continue,
|
None => continue,
|
||||||
};
|
};
|
||||||
if !prev_values.contains_key(&pat_id) {
|
if !prev_values.contains_key(&pat_id) {
|
||||||
|
@ -332,7 +331,7 @@ pub fn eval_const(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let r = match tail {
|
let r = match tail {
|
||||||
&Some(x) => eval_const(x, ctx, None),
|
&Some(x) => eval_const(x, ctx),
|
||||||
None => Ok(ComputedExpr::Tuple(Box::new([]))),
|
None => Ok(ComputedExpr::Tuple(Box::new([]))),
|
||||||
};
|
};
|
||||||
// clean up local data, so caller will receive the exact map that passed to us
|
// clean up local data, so caller will receive the exact map that passed to us
|
||||||
|
@ -390,7 +389,7 @@ pub fn eval_const(
|
||||||
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
|
_ => Err(ConstEvalError::NotSupported("path that are not const or local")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
&Expr::Cast { expr, .. } => match eval_const(expr, ctx, None)? {
|
&Expr::Cast { expr, .. } => match eval_const(expr, ctx)? {
|
||||||
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
|
ComputedExpr::Enum(_, _, lit) => Ok(ComputedExpr::Literal(lit)),
|
||||||
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
|
_ => Err(ConstEvalError::NotSupported("Can't cast these types")),
|
||||||
},
|
},
|
||||||
|
@ -489,7 +488,6 @@ pub(crate) fn const_eval_query(
|
||||||
local_data: HashMap::default(),
|
local_data: HashMap::default(),
|
||||||
infer,
|
infer,
|
||||||
},
|
},
|
||||||
None,
|
|
||||||
);
|
);
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
@ -511,7 +509,6 @@ pub(crate) fn const_eval_query_variant(
|
||||||
local_data: HashMap::default(),
|
local_data: HashMap::default(),
|
||||||
infer,
|
infer,
|
||||||
},
|
},
|
||||||
Some(variant_id),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -538,7 +535,7 @@ pub(crate) fn eval_to_const<'a>(
|
||||||
local_data: HashMap::default(),
|
local_data: HashMap::default(),
|
||||||
infer: &ctx.result,
|
infer: &ctx.result,
|
||||||
};
|
};
|
||||||
let computed_expr = eval_const(expr, &mut ctx, None);
|
let computed_expr = eval_const(expr, &mut ctx);
|
||||||
let const_scalar = match computed_expr {
|
let const_scalar = match computed_expr {
|
||||||
Ok(ComputedExpr::Literal(literal)) => literal.into(),
|
Ok(ComputedExpr::Literal(literal)) => literal.into(),
|
||||||
_ => ConstScalar::Unknown,
|
_ => ConstScalar::Unknown,
|
||||||
|
|
|
@ -954,7 +954,7 @@ impl Enum {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_data_carrying(self, db: &dyn HirDatabase) -> bool {
|
pub fn is_data_carrying(self, db: &dyn HirDatabase) -> bool {
|
||||||
self.variants(db).iter().all(|v| matches!(v.kind(db), StructKind::Unit))
|
self.variants(db).iter().any(|v| !matches!(v.kind(db), StructKind::Unit))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -966,8 +966,8 @@ impl HasVisibility for Enum {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Variant {
|
pub struct Variant {
|
||||||
pub parent: Enum,
|
pub(crate) parent: Enum,
|
||||||
pub id: LocalEnumVariantId,
|
pub(crate) id: LocalEnumVariantId,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
|
|
|
@ -349,7 +349,7 @@ pub(super) fn definition(
|
||||||
Definition::Function(it) => label_and_docs(db, it),
|
Definition::Function(it) => label_and_docs(db, it),
|
||||||
Definition::Adt(it) => label_and_docs(db, it),
|
Definition::Adt(it) => label_and_docs(db, it),
|
||||||
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
|
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
|
||||||
if it.parent.is_data_carrying(db) {
|
if !it.parent_enum(db).is_data_carrying(db) {
|
||||||
match it.eval(db) {
|
match it.eval(db) {
|
||||||
Ok(x) => Some(format!("{}", x)),
|
Ok(x) => Some(format!("{}", x)),
|
||||||
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
|
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
|
||||||
|
|
Loading…
Reference in a new issue