Skip to content

Commit 9bf386f

Browse files
committed
Fix default enum representation not being isize
1 parent 2119c1f commit 9bf386f

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

crates/hir-def/src/adt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl EnumData {
223223
pub fn variant_body_type(&self) -> Either<BuiltinInt, BuiltinUint> {
224224
match self.repr {
225225
Some(ReprData { kind: ReprKind::BuiltinInt { builtin, .. }, .. }) => builtin,
226-
_ => Either::Right(BuiltinUint::U32),
226+
_ => Either::Left(BuiltinInt::Isize),
227227
}
228228
}
229229
}

crates/hir-ty/src/tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,18 @@ fn visit_module(
461461
let body = db.body(def);
462462
visit_body(db, &body, cb);
463463
}
464+
ModuleDefId::AdtId(hir_def::AdtId::EnumId(it)) => {
465+
db.enum_data(it)
466+
.variants
467+
.iter()
468+
.map(|(id, _)| hir_def::EnumVariantId { parent: it, local_id: id })
469+
.for_each(|it| {
470+
let def = it.into();
471+
cb(def);
472+
let body = db.body(def);
473+
visit_body(db, &body, cb);
474+
});
475+
}
464476
ModuleDefId::TraitId(it) => {
465477
let trait_data = db.trait_data(it);
466478
for &(_, item) in trait_data.items.iter() {

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

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,16 +1693,16 @@ fn infer_type_param() {
16931693
fn infer_const() {
16941694
check_infer(
16951695
r#"
1696-
struct Foo;
1697-
impl Foo { const ASSOC_CONST: u32 = 0; }
1698-
const GLOBAL_CONST: u32 = 101;
1699-
fn test() {
1700-
const LOCAL_CONST: u32 = 99;
1701-
let x = LOCAL_CONST;
1702-
let z = GLOBAL_CONST;
1703-
let id = Foo::ASSOC_CONST;
1704-
}
1705-
"#,
1696+
struct Foo;
1697+
impl Foo { const ASSOC_CONST: u32 = 0; }
1698+
const GLOBAL_CONST: u32 = 101;
1699+
fn test() {
1700+
const LOCAL_CONST: u32 = 99;
1701+
let x = LOCAL_CONST;
1702+
let z = GLOBAL_CONST;
1703+
let id = Foo::ASSOC_CONST;
1704+
}
1705+
"#,
17061706
expect![[r#"
17071707
48..49 '0': u32
17081708
79..82 '101': u32
@@ -1722,17 +1722,17 @@ fn infer_const() {
17221722
fn infer_static() {
17231723
check_infer(
17241724
r#"
1725-
static GLOBAL_STATIC: u32 = 101;
1726-
static mut GLOBAL_STATIC_MUT: u32 = 101;
1727-
fn test() {
1728-
static LOCAL_STATIC: u32 = 99;
1729-
static mut LOCAL_STATIC_MUT: u32 = 99;
1730-
let x = LOCAL_STATIC;
1731-
let y = LOCAL_STATIC_MUT;
1732-
let z = GLOBAL_STATIC;
1733-
let w = GLOBAL_STATIC_MUT;
1734-
}
1735-
"#,
1725+
static GLOBAL_STATIC: u32 = 101;
1726+
static mut GLOBAL_STATIC_MUT: u32 = 101;
1727+
fn test() {
1728+
static LOCAL_STATIC: u32 = 99;
1729+
static mut LOCAL_STATIC_MUT: u32 = 99;
1730+
let x = LOCAL_STATIC;
1731+
let y = LOCAL_STATIC_MUT;
1732+
let z = GLOBAL_STATIC;
1733+
let w = GLOBAL_STATIC_MUT;
1734+
}
1735+
"#,
17361736
expect![[r#"
17371737
28..31 '101': u32
17381738
69..72 '101': u32
@@ -1751,6 +1751,41 @@ fn infer_static() {
17511751
);
17521752
}
17531753

1754+
#[test]
1755+
fn infer_enum_variant() {
1756+
check_infer(
1757+
r#"
1758+
enum Foo {
1759+
A = 15,
1760+
B = Foo::A as isize + 1
1761+
}
1762+
"#,
1763+
expect![[r#"
1764+
19..21 '15': isize
1765+
31..37 'Foo::A': Foo
1766+
31..46 'Foo::A as isize': isize
1767+
31..50 'Foo::A...ze + 1': isize
1768+
49..50 '1': isize
1769+
"#]],
1770+
);
1771+
check_infer(
1772+
r#"
1773+
#[repr(u32)]
1774+
enum Foo {
1775+
A = 15,
1776+
B = Foo::A as u32 + 1
1777+
}
1778+
"#,
1779+
expect![[r#"
1780+
32..34 '15': u32
1781+
44..50 'Foo::A': Foo
1782+
44..57 'Foo::A as u32': u32
1783+
44..61 'Foo::A...32 + 1': u32
1784+
60..61 '1': u32
1785+
"#]],
1786+
);
1787+
}
1788+
17541789
#[test]
17551790
fn shadowing_primitive() {
17561791
check_types(

0 commit comments

Comments
 (0)