Skip to content

Commit 72b387e

Browse files
committed
Add feature gates for f16 and f128
Includes related tests and documentation pages.
1 parent 61a9928 commit 72b387e

File tree

8 files changed

+230
-1
lines changed

8 files changed

+230
-1
lines changed

compiler/rustc_feature/src/unstable.rs

+4
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,10 @@ declare_features! (
461461
(unstable, extended_varargs_abi_support, "1.65.0", Some(100189)),
462462
/// Allows defining `extern type`s.
463463
(unstable, extern_types, "1.23.0", Some(43467)),
464+
/// Allow using 128-bit (quad precision) floating point numbers.
465+
(unstable, f128, "CURRENT_RUSTC_VERSION", Some(116909)),
466+
/// Allow using 16-bit (half precision) floating point numbers.
467+
(unstable, f16, "CURRENT_RUSTC_VERSION", Some(116909)),
464468
/// Allows the use of `#[ffi_const]` on foreign functions.
465469
(unstable, ffi_const, "1.45.0", Some(58328)),
466470
/// Allows the use of `#[ffi_pure]` on foreign functions.

compiler/rustc_hir_analysis/src/astconv/mod.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::collect::HirPlaceholderCollector;
1515
use crate::errors::AmbiguousLifetimeBound;
1616
use crate::middle::resolve_bound_vars as rbv;
1717
use crate::require_c_abi_if_c_variadic;
18-
use rustc_ast::TraitObjectSyntax;
18+
use rustc_ast::{FloatTy, TraitObjectSyntax};
1919
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2020
use rustc_errors::{
2121
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, FatalError, MultiSpan,
@@ -33,6 +33,7 @@ use rustc_middle::ty::{
3333
TypeVisitableExt,
3434
};
3535
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
36+
use rustc_session::parse::feature_err;
3637
use rustc_span::edit_distance::find_best_match_for_name;
3738
use rustc_span::symbol::{kw, Ident, Symbol};
3839
use rustc_span::{sym, BytePos, Span, DUMMY_SP};
@@ -2174,6 +2175,29 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
21742175
}
21752176
}
21762177
});
2178+
2179+
if let Some(e) = self.check_float_gate(
2180+
prim_ty,
2181+
FloatTy::F16,
2182+
self.tcx().features().f16,
2183+
sym::f16,
2184+
path.span,
2185+
"the feature `f16` is unstable",
2186+
) {
2187+
return e;
2188+
}
2189+
2190+
if let Some(e) = self.check_float_gate(
2191+
prim_ty,
2192+
FloatTy::F128,
2193+
self.tcx().features().f128,
2194+
sym::f128,
2195+
path.span,
2196+
"the feature `f128` is unstable",
2197+
) {
2198+
return e;
2199+
}
2200+
21772201
match prim_ty {
21782202
hir::PrimTy::Bool => tcx.types.bool,
21792203
hir::PrimTy::Char => tcx.types.char,
@@ -2803,6 +2827,31 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
28032827
}
28042828
Some(r)
28052829
}
2830+
2831+
/// If the float type is not enabled by the feature, return `Some(error)`. `None` if there are
2832+
/// no problems.
2833+
fn check_float_gate(
2834+
&self,
2835+
prim_ty: hir::PrimTy,
2836+
gated_ty: FloatTy,
2837+
feat_enabled: bool,
2838+
feat_sym: Symbol,
2839+
span: Span,
2840+
msg: &'static str,
2841+
) -> Option<Ty<'tcx>> {
2842+
let hir::PrimTy::Float(float_ty) = prim_ty else {
2843+
return None;
2844+
};
2845+
2846+
if float_ty == gated_ty && !feat_enabled && !span.allows_unstable(feat_sym) {
2847+
let sess = self.tcx().sess;
2848+
let guar = feature_err(sess, sym::f16, span, msg).emit();
2849+
self.set_tainted_by_errors(guar);
2850+
Some(Ty::new_error(self.tcx(), guar))
2851+
} else {
2852+
None
2853+
}
2854+
}
28062855
}
28072856

28082857
fn assoc_kind_str(kind: ty::AssocKind) -> &'static str {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `f128`
2+
3+
The tracking issue for this feature is: [#116909]
4+
5+
[#116909]: https://github.com/rust-lang/rust/issues/116909
6+
7+
---
8+
9+
Enable the `f128` type for IEEE 128-bit floating numbers (quad precision).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# `f16`
2+
3+
The tracking issue for this feature is: [#116909]
4+
5+
[#116909]: https://github.com/rust-lang/rust/issues/116909
6+
7+
---
8+
9+
Enable the `f16` type for IEEE 16-bit floating numbers (half precision).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![allow(unused)]
2+
3+
// ensure gating for primitive use
4+
mod m1 {
5+
const A: f128 = 10.0; //~ ERROR the feature `f128` is unstable
6+
7+
pub fn main() {
8+
let a: f128 = 100.0; //~ ERROR the feature `f128` is unstable
9+
let b = 0.0f128; //~ ERROR the feature `f128` is unstable
10+
foo(1.23);
11+
}
12+
13+
fn foo(a: f128) {} //~ ERROR the feature `f128` is unstable
14+
15+
struct Bar {
16+
a: f128, //~ ERROR the feature `f128` is unstable
17+
}
18+
}
19+
20+
// ensure we don't restrict name as an identifier or custom types
21+
mod mod1 {
22+
#[allow(non_camel_case_types)]
23+
struct f128 {
24+
a: i32,
25+
}
26+
27+
fn foo() {
28+
let f128 = ();
29+
}
30+
31+
fn bar(a: f128) -> i32 {
32+
a.a
33+
}
34+
}
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0658]: the feature `f128` is unstable
2+
--> $DIR/feature-gate-f128.rs:5:14
3+
|
4+
LL | const A: f128 = 10.0;
5+
| ^^^^
6+
|
7+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
8+
= help: add `#![feature(f16)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: the feature `f128` is unstable
12+
--> $DIR/feature-gate-f128.rs:13:15
13+
|
14+
LL | fn foo(a: f128) {}
15+
| ^^^^
16+
|
17+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
18+
= help: add `#![feature(f16)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: the feature `f128` is unstable
22+
--> $DIR/feature-gate-f128.rs:16:12
23+
|
24+
LL | a: f128,
25+
| ^^^^
26+
|
27+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
28+
= help: add `#![feature(f16)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: the feature `f128` is unstable
32+
--> $DIR/feature-gate-f128.rs:8:16
33+
|
34+
LL | let a: f128 = 100.0;
35+
| ^^^^
36+
|
37+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
38+
= help: add `#![feature(f16)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0658`.
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![allow(unused)]
2+
3+
// ensure gating for primitive use
4+
mod m1 {
5+
const A: f16 = 10.0; //~ ERROR the feature `f16` is unstable
6+
7+
pub fn main() {
8+
let a: f16 = 100.0; //~ ERROR the feature `f16` is unstable
9+
let b = 0.0f16; //~ ERROR the feature `f16` is unstable
10+
foo(1.23);
11+
}
12+
13+
fn foo(a: f16) {} //~ ERROR the feature `f16` is unstable
14+
15+
struct Bar {
16+
a: f16, //~ ERROR the feature `f16` is unstable
17+
}
18+
}
19+
20+
// ensure we don't restrict name as an identifier or custom types
21+
mod m2 {
22+
#[allow(non_camel_case_types)]
23+
struct f16 {
24+
a: i32,
25+
}
26+
27+
fn foo() {
28+
let f16 = ();
29+
}
30+
31+
fn bar(a: f16) -> i32 {
32+
a.a
33+
}
34+
}
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0658]: the feature `f16` is unstable
2+
--> $DIR/feature-gate-f16.rs:5:14
3+
|
4+
LL | const A: f16 = 10.0;
5+
| ^^^
6+
|
7+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
8+
= help: add `#![feature(f16)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error[E0658]: the feature `f16` is unstable
12+
--> $DIR/feature-gate-f16.rs:13:15
13+
|
14+
LL | fn foo(a: f16) {}
15+
| ^^^
16+
|
17+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
18+
= help: add `#![feature(f16)]` to the crate attributes to enable
19+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20+
21+
error[E0658]: the feature `f16` is unstable
22+
--> $DIR/feature-gate-f16.rs:16:12
23+
|
24+
LL | a: f16,
25+
| ^^^
26+
|
27+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
28+
= help: add `#![feature(f16)]` to the crate attributes to enable
29+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
30+
31+
error[E0658]: the feature `f16` is unstable
32+
--> $DIR/feature-gate-f16.rs:8:16
33+
|
34+
LL | let a: f16 = 100.0;
35+
| ^^^
36+
|
37+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
38+
= help: add `#![feature(f16)]` to the crate attributes to enable
39+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)