Skip to content

Commit 82baffa

Browse files
committed
Add feature gates for f16 and f128
Includes related tests and documentation pages.
1 parent dd17e21 commit 82baffa

File tree

10 files changed

+275
-3
lines changed

10 files changed

+275
-3
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 feature `f16` is unstable")
390+
}
391+
Some(sym::f128) => {
392+
gate!(&self, f128, e.span, "the feature `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
@@ -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_resolve/src/ident.rs

+33-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

@@ -421,6 +423,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
421423
// to detect potential ambiguities.
422424
let mut innermost_result: Option<(NameBinding<'_>, Flags)> = None;
423425
let mut determinacy = Determinacy::Determined;
426+
let sess = self.tcx.sess;
427+
let feat_f16 = self.tcx.features().f16;
428+
let feat_f128 = self.tcx.features().f128;
424429

425430
// Go through all the scopes and try to resolve the name.
426431
let break_result = self.visit_scopes(
@@ -598,7 +603,34 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
598603
result
599604
}
600605
Scope::BuiltinTypes => match this.builtin_types_bindings.get(&ident.name) {
601-
Some(binding) => Ok((*binding, Flags::empty())),
606+
Some(binding) => {
607+
match ident.name {
608+
sym::f16 => {
609+
if !feat_f16 && !ident.span.allows_unstable(sym::f16) {
610+
feature_err(
611+
sess,
612+
sym::f16,
613+
ident.span,
614+
"the feature `f16` is unstable",
615+
)
616+
.emit();
617+
}
618+
}
619+
sym::f128 => {
620+
if !feat_f128 && !ident.span.allows_unstable(sym::f128) {
621+
feature_err(
622+
sess,
623+
sym::f128,
624+
ident.span,
625+
"the feature `f128` is unstable",
626+
)
627+
.emit();
628+
}
629+
}
630+
_ => (),
631+
}
632+
Ok((*binding, Flags::empty()))
633+
}
602634
None => Err(Determinacy::Determined),
603635
},
604636
};

compiler/rustc_resolve/src/late.rs

+30-1
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};
@@ -3923,7 +3924,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
39233924
self.r.trait_map.insert(node_id, traits);
39243925
}
39253926

3926-
if PrimTy::from_name(path[0].ident.name).is_some() {
3927+
if let Some(prim) = PrimTy::from_name(path[0].ident.name) {
3928+
println!("checking path at defer {path:?}");
3929+
self.check_prim_gate(prim, path_span);
3930+
39273931
let mut std_path = Vec::with_capacity(1 + path.len());
39283932

39293933
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
@@ -4123,6 +4127,8 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
41234127
&& PrimTy::from_name(path[0].ident.name).is_some() =>
41244128
{
41254129
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
4130+
println!("checking path at module, {:?}", path);
4131+
self.check_prim_gate(prim, path[0].ident.span);
41264132
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
41274133
}
41284134
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
@@ -4659,6 +4665,29 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
46594665
self.r.doc_link_traits_in_scope = doc_link_traits_in_scope;
46604666
}
46614667
}
4668+
4669+
/// If a primitive requires a feature, emit a diagnostic if that feature is not
4670+
/// enabled.
4671+
///
4672+
/// This recreates the logic of `gate!` without a visitor's methods.
4673+
fn check_prim_gate(&self, prim_ty: PrimTy, span: Span) {
4674+
let PrimTy::Float(float_ty) = prim_ty else {
4675+
return;
4676+
};
4677+
4678+
let tcx = self.r.tcx();
4679+
let (sym, msg) = match float_ty {
4680+
FloatTy::F16 if !tcx.features().f16 => (sym::f16, "the feature `f16` is unstable"),
4681+
FloatTy::F128 if !tcx.features().f128 => (sym::f128, "the feature `f128` is unstable"),
4682+
_ => return,
4683+
};
4684+
4685+
if span.allows_unstable(sym) {
4686+
return;
4687+
}
4688+
4689+
feature_err(tcx.sess, sym, span, msg).emit();
4690+
}
46624691
}
46634692

46644693
/// Walks the whole crate in DFS order, visiting each item, counting the declared number of
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 m2 {
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,53 @@
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(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 feature `f128` is unstable
12+
--> $DIR/feature-gate-f128.rs:8:16
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 feature `f128` is unstable
22+
--> $DIR/feature-gate-f128.rs:13:15
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 feature `f128` is unstable
32+
--> $DIR/feature-gate-f128.rs:16:12
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 feature `f128` is unstable
42+
--> $DIR/feature-gate-f128.rs:9:17
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`.
+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,53 @@
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:8:16
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 feature `f16` is unstable
22+
--> $DIR/feature-gate-f16.rs:13:15
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 feature `f16` is unstable
32+
--> $DIR/feature-gate-f16.rs:16:12
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 feature `f16` is unstable
42+
--> $DIR/feature-gate-f16.rs:9:17
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)