Skip to content

Commit ad0a6bf

Browse files
committed
Added consteval tests
1 parent 2f84b6e commit ad0a6bf

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

crates/hir-def/src/body.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use crate::{
2828
nameres::DefMap,
2929
path::{ModPath, Path},
3030
src::{HasChildSource, HasSource},
31-
AsMacroCall, BlockId, DefWithBodyId, HasModule, LocalModuleId, Lookup, MacroId,
32-
ModuleId, UnresolvedMacro,
31+
AsMacroCall, BlockId, DefWithBodyId, HasModule, LocalModuleId, Lookup, MacroId, ModuleId,
32+
UnresolvedMacro,
3333
};
3434

3535
pub use lower::LowerCtx;
@@ -328,7 +328,6 @@ impl Body {
328328
let e = v.parent.lookup(db);
329329
let src = v.parent.child_source(db);
330330
let variant = &src.value[v.local_id];
331-
// TODO(ole): Handle missing exprs (+1 to the prev)
332331
(src.file_id, e.container, variant.expr())
333332
}
334333
};

crates/hir-ty/src/consteval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ pub fn eval_const(
203203
Ok(ComputedExpr::Enum(
204204
get_name(variant, ctx),
205205
variant,
206-
Literal::Int(value + 1, Some(BuiltinInt::I128)),
206+
Literal::Int(value, Some(BuiltinInt::I128)),
207207
))
208208
}
209209
_ => Err(ConstEvalError::IncompleteExpr),

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,35 @@ fn consts() {
8787
);
8888
}
8989

90+
#[test]
91+
fn enums() {
92+
check_number(
93+
r#"
94+
enum E {
95+
F1 = 1,
96+
F2 = 2 * E::F1 as u8,
97+
F3 = 3 * E::F2 as u8,
98+
}
99+
const GOAL: i32 = E::F3 as u8;
100+
"#,
101+
6,
102+
);
103+
let r = eval_goal(
104+
r#"
105+
enum E { A = 1, }
106+
const GOAL: E = E::A;
107+
"#,
108+
)
109+
.unwrap();
110+
match r {
111+
ComputedExpr::Enum(name, _, Literal::Uint(val, _)) => {
112+
assert_eq!(name, "E::A");
113+
assert_eq!(val, 1);
114+
}
115+
x => panic!("Expected enum but found {:?}", x),
116+
}
117+
}
118+
90119
#[test]
91120
fn const_loop() {
92121
check_fail(

crates/hir/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,10 @@ impl Enum {
952952
pub fn ty(self, db: &dyn HirDatabase) -> Type {
953953
Type::from_def(db, self.id)
954954
}
955+
956+
pub fn is_data_carrying(self, db: &dyn HirDatabase) -> bool {
957+
self.variants(db).iter().all(|v| matches!(v.kind(db), StructKind::Unit))
958+
}
955959
}
956960

957961
impl HasVisibility for Enum {
@@ -996,7 +1000,6 @@ impl Variant {
9961000
}
9971001

9981002
pub fn value(self, db: &dyn HirDatabase) -> Option<Expr> {
999-
// TODO(ole): Handle missing exprs (+1 to the prev)
10001003
self.source(db)?.value.expr()
10011004
}
10021005

crates/ide/src/hover/render.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,15 @@ pub(super) fn definition(
348348
Definition::Module(it) => label_and_docs(db, it),
349349
Definition::Function(it) => label_and_docs(db, it),
350350
Definition::Adt(it) => label_and_docs(db, it),
351-
Definition::Variant(it) => label_value_and_docs(db, it, |&it| match it.kind(db) {
352-
StructKind::Unit => match it.eval(db) {
353-
Ok(x) => Some(format!("{}", x.enum_value().unwrap_or(x))),
354-
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
355-
},
356-
_ => None,
351+
Definition::Variant(it) => label_value_and_docs(db, it, |&it| {
352+
if it.parent.is_data_carrying(db) {
353+
match it.eval(db) {
354+
Ok(x) => Some(format!("{}", x.enum_value().unwrap_or(x))),
355+
Err(_) => it.value(db).map(|x| format!("{:?}", x)),
356+
}
357+
} else {
358+
None
359+
}
357360
}),
358361
Definition::Const(it) => label_value_and_docs(db, it, |it| {
359362
let body = it.eval(db);

crates/ide/src/hover/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ fn hover_enum_variant() {
698698
check(
699699
r#"
700700
enum Option<T> {
701+
Some(T)
701702
/// The None variant
702703
Non$0e
703704
}

0 commit comments

Comments
 (0)