Skip to content

Commit 69424f7

Browse files
committed
Add f16 and f128 support
1 parent 5445aef commit 69424f7

File tree

28 files changed

+384
-73
lines changed

28 files changed

+384
-73
lines changed

src/tools/rust-analyzer/Cargo.lock

+12-8
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e"
167167

168168
[[package]]
169169
name = "chalk-derive"
170-
version = "0.97.0"
170+
version = "0.98.0"
171171
source = "registry+https://github.com/rust-lang/crates.io-index"
172-
checksum = "92a0aedc4ac2adc5c0b7dc9ec38c5c816284ad28da6d4ecd01873b9683f54972"
172+
checksum = "9426c8fd0fe61c3da880b801d3b510524df17843a8f9ec1f5b9cec24fb7412df"
173173
dependencies = [
174174
"proc-macro2",
175175
"quote",
@@ -179,19 +179,19 @@ dependencies = [
179179

180180
[[package]]
181181
name = "chalk-ir"
182-
version = "0.97.0"
182+
version = "0.98.0"
183183
source = "registry+https://github.com/rust-lang/crates.io-index"
184-
checksum = "db18493569b190f7266a04901e520fc3a5c00564475154287906f8a27302c119"
184+
checksum = "d5f2eb1cd6054da221bd1ac0197fb2fe5e2caf3dcb93619398fc1433f8f09093"
185185
dependencies = [
186186
"bitflags 2.5.0",
187187
"chalk-derive",
188188
]
189189

190190
[[package]]
191191
name = "chalk-recursive"
192-
version = "0.97.0"
192+
version = "0.98.0"
193193
source = "registry+https://github.com/rust-lang/crates.io-index"
194-
checksum = "ae4ba8ce5bd2e1b59f1f79495bc8704db09a8285e51cc5ddf01d9baee1bf447d"
194+
checksum = "129dc03458f71cfb9c3cd621c9c68166a94e87b85b16ccd29af015d7ff9a1c61"
195195
dependencies = [
196196
"chalk-derive",
197197
"chalk-ir",
@@ -202,9 +202,9 @@ dependencies = [
202202

203203
[[package]]
204204
name = "chalk-solve"
205-
version = "0.97.0"
205+
version = "0.98.0"
206206
source = "registry+https://github.com/rust-lang/crates.io-index"
207-
checksum = "b2ec1b3b7f7b1ec38f099ef39c2bc3ea29335be1b8316d114baff46d96d131e9"
207+
checksum = "d7e8a8c1e928f98cdf227b868416ef21dcd8cc3c61b347576d783713444d41c8"
208208
dependencies = [
209209
"chalk-derive",
210210
"chalk-ir",
@@ -546,6 +546,7 @@ dependencies = [
546546
"ra-ap-rustc_abi",
547547
"ra-ap-rustc_parse_format",
548548
"rustc-hash",
549+
"rustc_apfloat",
549550
"smallvec",
550551
"span",
551552
"stdx",
@@ -613,6 +614,7 @@ dependencies = [
613614
"ra-ap-rustc_index",
614615
"ra-ap-rustc_pattern_analysis",
615616
"rustc-hash",
617+
"rustc_apfloat",
616618
"scoped-tls",
617619
"smallvec",
618620
"span",
@@ -658,6 +660,7 @@ dependencies = [
658660
"profile",
659661
"pulldown-cmark",
660662
"pulldown-cmark-to-cmark",
663+
"rustc_apfloat",
661664
"smallvec",
662665
"span",
663666
"stdx",
@@ -1939,6 +1942,7 @@ dependencies = [
19391942
"rayon",
19401943
"rowan",
19411944
"rustc-hash",
1945+
"rustc_apfloat",
19421946
"smol_str",
19431947
"stdx",
19441948
"test-utils",

src/tools/rust-analyzer/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ arrayvec = "0.7.4"
104104
bitflags = "2.4.1"
105105
cargo_metadata = "0.18.1"
106106
camino = "1.1.6"
107-
chalk-solve = { version = "0.97.0", default-features = false }
108-
chalk-ir = "0.97.0"
109-
chalk-recursive = { version = "0.97.0", default-features = false }
110-
chalk-derive = "0.97.0"
107+
chalk-solve = { version = "0.98.0", default-features = false }
108+
chalk-ir = "0.98.0"
109+
chalk-recursive = { version = "0.98.0", default-features = false }
110+
chalk-derive = "0.98.0"
111111
crossbeam-channel = "0.5.8"
112112
dissimilar = "1.0.7"
113113
dot = "0.1.4"

src/tools/rust-analyzer/crates/hir-def/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tracing.workspace = true
2828
smallvec.workspace = true
2929
hashbrown.workspace = true
3030
triomphe.workspace = true
31+
rustc_apfloat = "0.2.0"
3132

3233
ra-ap-rustc_parse_format.workspace = true
3334
ra-ap-rustc_abi.workspace = true

src/tools/rust-analyzer/crates/hir-def/src/builtin_type.rs

+10
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ pub enum BuiltinUint {
3030

3131
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
3232
pub enum BuiltinFloat {
33+
F16,
3334
F32,
3435
F64,
36+
F128,
3537
}
3638

3739
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -65,8 +67,10 @@ impl BuiltinType {
6567
(name![u64], BuiltinType::Uint(BuiltinUint::U64)),
6668
(name![u128], BuiltinType::Uint(BuiltinUint::U128)),
6769

70+
(name![f16], BuiltinType::Float(BuiltinFloat::F16)),
6871
(name![f32], BuiltinType::Float(BuiltinFloat::F32)),
6972
(name![f64], BuiltinType::Float(BuiltinFloat::F64)),
73+
(name![f128], BuiltinType::Float(BuiltinFloat::F128)),
7074
];
7175

7276
pub fn by_name(name: &Name) -> Option<Self> {
@@ -97,8 +101,10 @@ impl AsName for BuiltinType {
97101
BuiltinUint::U128 => name![u128],
98102
},
99103
BuiltinType::Float(it) => match it {
104+
BuiltinFloat::F16 => name![f16],
100105
BuiltinFloat::F32 => name![f32],
101106
BuiltinFloat::F64 => name![f64],
107+
BuiltinFloat::F128 => name![f128],
102108
},
103109
}
104110
}
@@ -155,8 +161,10 @@ impl BuiltinUint {
155161
impl BuiltinFloat {
156162
pub fn from_suffix(suffix: &str) -> Option<BuiltinFloat> {
157163
let res = match suffix {
164+
"f16" => BuiltinFloat::F16,
158165
"f32" => BuiltinFloat::F32,
159166
"f64" => BuiltinFloat::F64,
167+
"f128" => BuiltinFloat::F128,
160168
_ => return None,
161169
};
162170
Some(res)
@@ -192,8 +200,10 @@ impl fmt::Display for BuiltinUint {
192200
impl fmt::Display for BuiltinFloat {
193201
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
194202
f.write_str(match self {
203+
BuiltinFloat::F16 => "f16",
195204
BuiltinFloat::F32 => "f32",
196205
BuiltinFloat::F64 => "f64",
206+
BuiltinFloat::F128 => "f128",
197207
})
198208
}
199209
}

src/tools/rust-analyzer/crates/hir-def/src/hir.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::fmt;
2020
use hir_expand::name::Name;
2121
use intern::Interned;
2222
use la_arena::{Idx, RawIdx};
23+
use rustc_apfloat::ieee::{Half as f16, Quad as f128};
2324
use smallvec::SmallVec;
2425
use syntax::ast;
2526

@@ -62,18 +63,27 @@ pub type LabelId = Idx<Label>;
6263
#[derive(Default, Debug, Clone, Eq, PartialEq)]
6364
pub struct FloatTypeWrapper(Box<str>);
6465

66+
// FIXME(#17451): Use builtin types once stabilised.
6567
impl FloatTypeWrapper {
6668
pub fn new(value: String) -> Self {
6769
Self(value.into())
6870
}
6971

72+
pub fn to_f128(&self) -> f128 {
73+
self.0.parse().unwrap_or_default()
74+
}
75+
7076
pub fn to_f64(&self) -> f64 {
7177
self.0.parse().unwrap_or_default()
7278
}
7379

7480
pub fn to_f32(&self) -> f32 {
7581
self.0.parse().unwrap_or_default()
7682
}
83+
84+
pub fn to_f16(&self) -> f16 {
85+
self.0.parse().unwrap_or_default()
86+
}
7787
}
7888

7989
impl fmt::Display for FloatTypeWrapper {
@@ -91,7 +101,7 @@ pub enum Literal {
91101
Bool(bool),
92102
Int(i128, Option<BuiltinInt>),
93103
Uint(u128, Option<BuiltinUint>),
94-
// Here we are using a wrapper around float because f32 and f64 do not implement Eq, so they
104+
// Here we are using a wrapper around float because float primitives do not implement Eq, so they
95105
// could not be used directly here, to understand how the wrapper works go to definition of
96106
// FloatTypeWrapper
97107
Float(FloatTypeWrapper, Option<BuiltinFloat>),

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mbe/tt_conversion.rs

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ macro_rules! m {
3636
let _ = 'c';
3737
let _ = 1000;
3838
let _ = 12E+99_f64;
39+
let _ = 45E+1234_f128;
3940
let _ = "rust1";
4041
let _ = -92;
4142
}
@@ -50,6 +51,7 @@ macro_rules! m {
5051
let _ = 'c';
5152
let _ = 1000;
5253
let _ = 12E+99_f64;
54+
let _ = 45E+1234_f128;
5355
let _ = "rust1";
5456
let _ = -92;
5557
}
@@ -58,6 +60,7 @@ fn f() {
5860
let _ = 'c';
5961
let _ = 1000;
6062
let _ = 12E+99_f64;
63+
let _ = 45E+1234_f128;
6164
let _ = "rust1";
6265
let _ = -92;
6366
}

src/tools/rust-analyzer/crates/hir-expand/src/name.rs

+2
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@ pub mod known {
275275
u32,
276276
u64,
277277
u128,
278+
f16,
278279
f32,
279280
f64,
281+
f128,
280282
bool,
281283
char,
282284
str,

src/tools/rust-analyzer/crates/hir-ty/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ triomphe.workspace = true
3333
nohash-hasher.workspace = true
3434
typed-arena = "2.0.1"
3535
indexmap.workspace = true
36+
rustc_apfloat = "0.2.0"
3637

3738
ra-ap-rustc_abi.workspace = true
3839
ra-ap-rustc_index.workspace = true

src/tools/rust-analyzer/crates/hir-ty/src/chalk_ext.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ impl TyExt for Ty {
119119
TyKind::Scalar(Scalar::Bool) => Some(BuiltinType::Bool),
120120
TyKind::Scalar(Scalar::Char) => Some(BuiltinType::Char),
121121
TyKind::Scalar(Scalar::Float(fty)) => Some(BuiltinType::Float(match fty {
122+
FloatTy::F128 => BuiltinFloat::F128,
122123
FloatTy::F64 => BuiltinFloat::F64,
123124
FloatTy::F32 => BuiltinFloat::F32,
125+
FloatTy::F16 => BuiltinFloat::F16,
124126
})),
125127
TyKind::Scalar(Scalar::Int(ity)) => Some(BuiltinType::Int(match ity {
126128
IntTy::Isize => BuiltinInt::Isize,

src/tools/rust-analyzer/crates/hir-ty/src/consteval/tests.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use base_db::FileId;
22
use chalk_ir::Substitution;
33
use hir_def::db::DefDatabase;
4+
use rustc_apfloat::{
5+
ieee::{Half as f16, Quad as f128},
6+
Float,
7+
};
48
use test_fixture::WithFixture;
59
use test_utils::skip_slow_tests;
610

@@ -140,6 +144,14 @@ fn bit_op() {
140144

141145
#[test]
142146
fn floating_point() {
147+
check_number(
148+
r#"const GOAL: f128 = 2.0 + 3.0 * 5.5 - 8.;"#,
149+
"10.5".parse::<f128>().unwrap().to_bits() as i128,
150+
);
151+
check_number(
152+
r#"const GOAL: f128 = -90.0 + 36.0;"#,
153+
"-54.0".parse::<f128>().unwrap().to_bits() as i128,
154+
);
143155
check_number(
144156
r#"const GOAL: f64 = 2.0 + 3.0 * 5.5 - 8.;"#,
145157
i128::from_le_bytes(pad16(&f64::to_le_bytes(10.5), true)),
@@ -152,6 +164,20 @@ fn floating_point() {
152164
r#"const GOAL: f32 = -90.0 + 36.0;"#,
153165
i128::from_le_bytes(pad16(&f32::to_le_bytes(-54.0), true)),
154166
);
167+
check_number(
168+
r#"const GOAL: f16 = 2.0 + 3.0 * 5.5 - 8.;"#,
169+
i128::from_le_bytes(pad16(
170+
&u16::try_from("10.5".parse::<f16>().unwrap().to_bits()).unwrap().to_le_bytes(),
171+
true,
172+
)),
173+
);
174+
check_number(
175+
r#"const GOAL: f16 = -90.0 + 36.0;"#,
176+
i128::from_le_bytes(pad16(
177+
&u16::try_from("-54.0".parse::<f16>().unwrap().to_bits()).unwrap().to_le_bytes(),
178+
true,
179+
)),
180+
);
155181
}
156182

157183
#[test]

src/tools/rust-analyzer/crates/hir-ty/src/consteval/tests/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ fn likely() {
411411

412412
#[test]
413413
fn floating_point() {
414+
// FIXME(#17451): Add `f16` and `f128` tests once intrinsics are added.
414415
check_number(
415416
r#"
416417
extern "rust-intrinsic" {

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

+26
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ use hir_expand::name::Name;
2828
use intern::{Internable, Interned};
2929
use itertools::Itertools;
3030
use la_arena::ArenaMap;
31+
use rustc_apfloat::{
32+
ieee::{Half as f16, Quad as f128},
33+
Float,
34+
};
3135
use smallvec::SmallVec;
3236
use stdx::{never, IsNoneOr};
3337
use triomphe::Arc;
@@ -545,6 +549,17 @@ fn render_const_scalar(
545549
write!(f, "{it}")
546550
}
547551
Scalar::Float(fl) => match fl {
552+
chalk_ir::FloatTy::F16 => {
553+
// FIXME(#17451): Replace with builtins once they are stabilised.
554+
let it = f16::from_bits(u16::from_le_bytes(b.try_into().unwrap()).into());
555+
let s = it.to_string();
556+
if s.strip_prefix('-').unwrap_or(&s).chars().all(|c| c.is_ascii_digit()) {
557+
// Match Rust debug formatting
558+
write!(f, "{s}.0")
559+
} else {
560+
write!(f, "{s}")
561+
}
562+
}
548563
chalk_ir::FloatTy::F32 => {
549564
let it = f32::from_le_bytes(b.try_into().unwrap());
550565
write!(f, "{it:?}")
@@ -553,6 +568,17 @@ fn render_const_scalar(
553568
let it = f64::from_le_bytes(b.try_into().unwrap());
554569
write!(f, "{it:?}")
555570
}
571+
chalk_ir::FloatTy::F128 => {
572+
// FIXME(#17451): Replace with builtins once they are stabilised.
573+
let it = f128::from_bits(u128::from_le_bytes(b.try_into().unwrap()));
574+
let s = it.to_string();
575+
if s.strip_prefix('-').unwrap_or(&s).chars().all(|c| c.is_ascii_digit()) {
576+
// Match Rust debug formatting
577+
write!(f, "{s}.0")
578+
} else {
579+
write!(f, "{s}")
580+
}
581+
}
556582
},
557583
},
558584
TyKind::Ref(_, _, t) => match t.kind(Interner) {

src/tools/rust-analyzer/crates/hir-ty/src/layout.rs

+2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ pub fn layout_of_ty_query(
265265
chalk_ir::Scalar::Float(f) => scalar(
266266
dl,
267267
Primitive::Float(match f {
268+
FloatTy::F16 => Float::F16,
268269
FloatTy::F32 => Float::F32,
269270
FloatTy::F64 => Float::F64,
271+
FloatTy::F128 => Float::F128,
270272
}),
271273
),
272274
},

src/tools/rust-analyzer/crates/hir-ty/src/layout/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ fn enums() {
426426

427427
#[test]
428428
fn primitives() {
429+
// FIXME(#17451): Add `f16` and `f128` once they are stabilised.
429430
size_and_align! {
430431
struct Goal(i32, i128, isize, usize, f32, f64, bool, char);
431432
}

src/tools/rust-analyzer/crates/hir-ty/src/method_resolution.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ pub(crate) const ALL_INT_FPS: [TyFingerprint; 12] = [
127127
TyFingerprint::Scalar(Scalar::Uint(UintTy::Usize)),
128128
];
129129

130-
pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 2] = [
130+
pub(crate) const ALL_FLOAT_FPS: [TyFingerprint; 4] = [
131+
TyFingerprint::Scalar(Scalar::Float(FloatTy::F16)),
131132
TyFingerprint::Scalar(Scalar::Float(FloatTy::F32)),
132133
TyFingerprint::Scalar(Scalar::Float(FloatTy::F64)),
134+
TyFingerprint::Scalar(Scalar::Float(FloatTy::F128)),
133135
];
134136

135137
type TraitFpMap = FxHashMap<TraitId, FxHashMap<Option<TyFingerprint>, Box<[ImplId]>>>;

0 commit comments

Comments
 (0)