Skip to content

Commit 4267f07

Browse files
committed
Add feature gates for f16 and f128
Includes related tests and documentation pages.
1 parent 547dcc9 commit 4267f07

File tree

10 files changed

+222
-2
lines changed

10 files changed

+222
-2
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast as ast;
22
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
33
use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId};
4-
use rustc_ast::{PatKind, RangeEnd};
4+
use rustc_ast::{token, PatKind, RangeEnd};
55
use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP};
66
use rustc_session::parse::{feature_err, feature_err_issue, feature_warn};
77
use rustc_session::Session;
@@ -383,6 +383,17 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
383383
ast::ExprKind::TryBlock(_) => {
384384
gate!(&self, try_blocks, e.span, "`try` expression is experimental");
385385
}
386+
ast::ExprKind::Lit(token::Lit { kind: token::LitKind::Float, suffix, .. }) => {
387+
match suffix {
388+
Some(sym::f16) => {
389+
gate!(&self, f16, e.span, "the type `f16` is unstable")
390+
}
391+
Some(sym::f128) => {
392+
gate!(&self, f128, e.span, "the type `f128` is unstable")
393+
}
394+
_ => (),
395+
}
396+
}
386397
_ => {}
387398
}
388399
visit::walk_expr(self, e)

compiler/rustc_feature/src/unstable.rs

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

compiler/rustc_resolve/src/ident.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use rustc_middle::bug;
55
use rustc_middle::ty;
66
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
77
use rustc_session::lint::BuiltinLintDiagnostics;
8+
use rustc_session::parse::feature_err;
89
use rustc_span::def_id::LocalDefId;
910
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
11+
use rustc_span::sym;
1012
use rustc_span::symbol::{kw, Ident};
1113
use rustc_span::Span;
1214

@@ -598,7 +600,36 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
598600
result
599601
}
600602
Scope::BuiltinTypes => match this.builtin_types_bindings.get(&ident.name) {
601-
Some(binding) => Ok((*binding, Flags::empty())),
603+
Some(binding) => {
604+
match ident.name {
605+
sym::f16
606+
if !this.tcx.features().f16
607+
&& !ident.span.allows_unstable(sym::f16) =>
608+
{
609+
feature_err(
610+
this.tcx.sess,
611+
sym::f16,
612+
ident.span,
613+
"the type `f16` is unstable",
614+
)
615+
.emit();
616+
}
617+
sym::f128
618+
if !this.tcx.features().f128
619+
&& !ident.span.allows_unstable(sym::f128) =>
620+
{
621+
feature_err(
622+
this.tcx.sess,
623+
sym::f128,
624+
ident.span,
625+
"the type `f128` is unstable",
626+
)
627+
.emit();
628+
}
629+
_ => (),
630+
}
631+
Ok((*binding, Flags::empty()))
632+
}
602633
None => Err(Determinacy::Determined),
603634
},
604635
};

compiler/rustc_resolve/src/late.rs

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc_middle::middle::resolve_bound_vars::Set1;
2727
use rustc_middle::{bug, span_bug};
2828
use rustc_session::config::{CrateType, ResolveDocLinks};
2929
use rustc_session::lint;
30+
use rustc_session::parse::feature_err;
3031
use rustc_span::source_map::{respan, Spanned};
3132
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3233
use rustc_span::{BytePos, Span, SyntaxContext};
@@ -4123,6 +4124,25 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
41234124
&& PrimTy::from_name(path[0].ident.name).is_some() =>
41244125
{
41254126
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
4127+
let tcx = self.r.tcx();
4128+
4129+
let gate_err_sym_msg = match prim {
4130+
PrimTy::Float(FloatTy::F16) if !tcx.features().f16 => {
4131+
Some((sym::f16, "the type `f16` is unstable"))
4132+
}
4133+
PrimTy::Float(FloatTy::F128) if !tcx.features().f128 => {
4134+
Some((sym::f128, "the type `f128` is unstable"))
4135+
}
4136+
_ => None,
4137+
};
4138+
4139+
if let Some((sym, msg)) = gate_err_sym_msg {
4140+
let span = path[0].ident.span;
4141+
if !span.allows_unstable(sym) {
4142+
feature_err(tcx.sess, sym, span, msg).emit();
4143+
}
4144+
};
4145+
41264146
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
41274147
}
41284148
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
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,15 @@
1+
#![allow(unused)]
2+
3+
const A: f128 = 10.0; //~ ERROR the type `f128` is unstable
4+
5+
pub fn main() {
6+
let a: f128 = 100.0; //~ ERROR the type `f128` is unstable
7+
let b = 0.0f128; //~ ERROR the type `f128` is unstable
8+
foo(1.23);
9+
}
10+
11+
fn foo(a: f128) {} //~ ERROR the type `f128` is unstable
12+
13+
struct Bar {
14+
a: f128, //~ ERROR the type `f128` is unstable
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error[E0658]: the type `f128` is unstable
2+
--> $DIR/feature-gate-f128.rs:3:10
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(f128)]` 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 type `f128` is unstable
12+
--> $DIR/feature-gate-f128.rs:6:12
13+
|
14+
LL | let a: f128 = 100.0;
15+
| ^^^^
16+
|
17+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
18+
= help: add `#![feature(f128)]` 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 type `f128` is unstable
22+
--> $DIR/feature-gate-f128.rs:11:11
23+
|
24+
LL | fn foo(a: f128) {}
25+
| ^^^^
26+
|
27+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
28+
= help: add `#![feature(f128)]` 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 type `f128` is unstable
32+
--> $DIR/feature-gate-f128.rs:14:8
33+
|
34+
LL | a: f128,
35+
| ^^^^
36+
|
37+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
38+
= help: add `#![feature(f128)]` 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[E0658]: the type `f128` is unstable
42+
--> $DIR/feature-gate-f128.rs:7:13
43+
|
44+
LL | let b = 0.0f128;
45+
| ^^^^^^^
46+
|
47+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
48+
= help: add `#![feature(f128)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error: aborting due to 5 previous errors
52+
53+
For more information about this error, try `rustc --explain E0658`.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![allow(unused)]
2+
3+
const A: f16 = 10.0; //~ ERROR the type `f16` is unstable
4+
5+
pub fn main() {
6+
let a: f16 = 100.0; //~ ERROR the type `f16` is unstable
7+
let b = 0.0f16; //~ ERROR the type `f16` is unstable
8+
foo(1.23);
9+
}
10+
11+
fn foo(a: f16) {} //~ ERROR the type `f16` is unstable
12+
13+
struct Bar {
14+
a: f16, //~ ERROR the type `f16` is unstable
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
error[E0658]: the type `f16` is unstable
2+
--> $DIR/feature-gate-f16.rs:3:10
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 type `f16` is unstable
12+
--> $DIR/feature-gate-f16.rs:6:12
13+
|
14+
LL | let a: f16 = 100.0;
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 type `f16` is unstable
22+
--> $DIR/feature-gate-f16.rs:11:11
23+
|
24+
LL | fn foo(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 type `f16` is unstable
32+
--> $DIR/feature-gate-f16.rs:14:8
33+
|
34+
LL | a: f16,
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[E0658]: the type `f16` is unstable
42+
--> $DIR/feature-gate-f16.rs:7:13
43+
|
44+
LL | let b = 0.0f16;
45+
| ^^^^^^
46+
|
47+
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
48+
= help: add `#![feature(f16)]` to the crate attributes to enable
49+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
50+
51+
error: aborting due to 5 previous errors
52+
53+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)