From 9bf386f4c03571c58e6a38267f1008241ea723ef Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 20 Sep 2022 17:43:29 +0200 Subject: [PATCH] Fix default enum representation not being isize --- crates/hir-def/src/adt.rs | 2 +- crates/hir-ty/src/tests.rs | 12 +++++ crates/hir-ty/src/tests/simple.rs | 77 ++++++++++++++++++++++--------- 3 files changed, 69 insertions(+), 22 deletions(-) diff --git a/crates/hir-def/src/adt.rs b/crates/hir-def/src/adt.rs index b093669e6a7..14f8629056f 100644 --- a/crates/hir-def/src/adt.rs +++ b/crates/hir-def/src/adt.rs @@ -223,7 +223,7 @@ pub fn variant(&self, name: &Name) -> Option { pub fn variant_body_type(&self) -> Either { match self.repr { Some(ReprData { kind: ReprKind::BuiltinInt { builtin, .. }, .. }) => builtin, - _ => Either::Right(BuiltinUint::U32), + _ => Either::Left(BuiltinInt::Isize), } } } diff --git a/crates/hir-ty/src/tests.rs b/crates/hir-ty/src/tests.rs index be5ece9c5c5..ebbc5410147 100644 --- a/crates/hir-ty/src/tests.rs +++ b/crates/hir-ty/src/tests.rs @@ -461,6 +461,18 @@ fn visit_scope( let body = db.body(def); visit_body(db, &body, cb); } + ModuleDefId::AdtId(hir_def::AdtId::EnumId(it)) => { + db.enum_data(it) + .variants + .iter() + .map(|(id, _)| hir_def::EnumVariantId { parent: it, local_id: id }) + .for_each(|it| { + let def = it.into(); + cb(def); + let body = db.body(def); + visit_body(db, &body, cb); + }); + } ModuleDefId::TraitId(it) => { let trait_data = db.trait_data(it); for &(_, item) in trait_data.items.iter() { diff --git a/crates/hir-ty/src/tests/simple.rs b/crates/hir-ty/src/tests/simple.rs index 4ea103e5d9e..1757327b929 100644 --- a/crates/hir-ty/src/tests/simple.rs +++ b/crates/hir-ty/src/tests/simple.rs @@ -1693,16 +1693,16 @@ fn test() { fn infer_const() { check_infer( r#" - struct Foo; - impl Foo { const ASSOC_CONST: u32 = 0; } - const GLOBAL_CONST: u32 = 101; - fn test() { - const LOCAL_CONST: u32 = 99; - let x = LOCAL_CONST; - let z = GLOBAL_CONST; - let id = Foo::ASSOC_CONST; - } - "#, +struct Foo; +impl Foo { const ASSOC_CONST: u32 = 0; } +const GLOBAL_CONST: u32 = 101; +fn test() { + const LOCAL_CONST: u32 = 99; + let x = LOCAL_CONST; + let z = GLOBAL_CONST; + let id = Foo::ASSOC_CONST; +} +"#, expect![[r#" 48..49 '0': u32 79..82 '101': u32 @@ -1722,17 +1722,17 @@ fn test() { fn infer_static() { check_infer( r#" - static GLOBAL_STATIC: u32 = 101; - static mut GLOBAL_STATIC_MUT: u32 = 101; - fn test() { - static LOCAL_STATIC: u32 = 99; - static mut LOCAL_STATIC_MUT: u32 = 99; - let x = LOCAL_STATIC; - let y = LOCAL_STATIC_MUT; - let z = GLOBAL_STATIC; - let w = GLOBAL_STATIC_MUT; - } - "#, +static GLOBAL_STATIC: u32 = 101; +static mut GLOBAL_STATIC_MUT: u32 = 101; +fn test() { + static LOCAL_STATIC: u32 = 99; + static mut LOCAL_STATIC_MUT: u32 = 99; + let x = LOCAL_STATIC; + let y = LOCAL_STATIC_MUT; + let z = GLOBAL_STATIC; + let w = GLOBAL_STATIC_MUT; +} +"#, expect![[r#" 28..31 '101': u32 69..72 '101': u32 @@ -1751,6 +1751,41 @@ fn test() { ); } +#[test] +fn infer_enum_variant() { + check_infer( + r#" +enum Foo { + A = 15, + B = Foo::A as isize + 1 +} +"#, + expect![[r#" + 19..21 '15': isize + 31..37 'Foo::A': Foo + 31..46 'Foo::A as isize': isize + 31..50 'Foo::A...ze + 1': isize + 49..50 '1': isize + "#]], + ); + check_infer( + r#" +#[repr(u32)] +enum Foo { + A = 15, + B = Foo::A as u32 + 1 +} +"#, + expect![[r#" + 32..34 '15': u32 + 44..50 'Foo::A': Foo + 44..57 'Foo::A as u32': u32 + 44..61 'Foo::A...32 + 1': u32 + 60..61 '1': u32 + "#]], + ); +} + #[test] fn shadowing_primitive() { check_types(