Add feature gates for f16 and f128

Includes related tests and documentation pages.

Michael Goulet: Don't issue feature error in resolver for f16/f128
unless finalize

Co-authored-by: Michael Goulet <michael@errs.io>
This commit is contained in:
Trevor Gross 2024-03-02 21:45:23 -05:00
parent dc65095298
commit e782d27ec6
11 changed files with 252 additions and 2 deletions

View File

@ -1,7 +1,7 @@
use rustc_ast as ast;
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
use rustc_ast::{attr, AssocConstraint, AssocConstraintKind, NodeId};
use rustc_ast::{PatKind, RangeEnd};
use rustc_ast::{token, PatKind, RangeEnd};
use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP};
use rustc_session::parse::{feature_err, feature_err_issue, feature_warn};
use rustc_session::Session;
@ -378,6 +378,17 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
ast::ExprKind::TryBlock(_) => {
gate!(&self, try_blocks, e.span, "`try` expression is experimental");
}
ast::ExprKind::Lit(token::Lit { kind: token::LitKind::Float, suffix, .. }) => {
match suffix {
Some(sym::f16) => {
gate!(&self, f16, e.span, "the type `f16` is unstable")
}
Some(sym::f128) => {
gate!(&self, f128, e.span, "the type `f128` is unstable")
}
_ => (),
}
}
_ => {}
}
visit::walk_expr(self, e)

View File

@ -463,6 +463,10 @@ declare_features! (
(unstable, extended_varargs_abi_support, "1.65.0", Some(100189)),
/// Allows defining `extern type`s.
(unstable, extern_types, "1.23.0", Some(43467)),
/// Allow using 128-bit (quad precision) floating point numbers.
(unstable, f128, "CURRENT_RUSTC_VERSION", Some(116909)),
/// Allow using 16-bit (half precision) floating point numbers.
(unstable, f16, "CURRENT_RUSTC_VERSION", Some(116909)),
/// Allows the use of `#[ffi_const]` on foreign functions.
(unstable, ffi_const, "1.45.0", Some(58328)),
/// Allows the use of `#[ffi_pure]` on foreign functions.

View File

@ -5,8 +5,10 @@ use rustc_middle::bug;
use rustc_middle::ty;
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
use rustc_session::lint::BuiltinLintDiag;
use rustc_session::parse::feature_err;
use rustc_span::def_id::LocalDefId;
use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext};
use rustc_span::sym;
use rustc_span::symbol::{kw, Ident};
use rustc_span::Span;
@ -598,7 +600,35 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
result
}
Scope::BuiltinTypes => match this.builtin_types_bindings.get(&ident.name) {
Some(binding) => Ok((*binding, Flags::empty())),
Some(binding) => {
if matches!(ident.name, sym::f16)
&& !this.tcx.features().f16
&& !ident.span.allows_unstable(sym::f16)
&& finalize.is_some()
{
feature_err(
this.tcx.sess,
sym::f16,
ident.span,
"the type `f16` is unstable",
)
.emit();
}
if matches!(ident.name, sym::f128)
&& !this.tcx.features().f128
&& !ident.span.allows_unstable(sym::f128)
&& finalize.is_some()
{
feature_err(
this.tcx.sess,
sym::f128,
ident.span,
"the type `f128` is unstable",
)
.emit();
}
Ok((*binding, Flags::empty()))
}
None => Err(Determinacy::Determined),
},
};

View File

@ -27,6 +27,7 @@ use rustc_middle::middle::resolve_bound_vars::Set1;
use rustc_middle::{bug, span_bug};
use rustc_session::config::{CrateType, ResolveDocLinks};
use rustc_session::lint;
use rustc_session::parse::feature_err;
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Span, SyntaxContext};
@ -4129,6 +4130,25 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
&& PrimTy::from_name(path[0].ident.name).is_some() =>
{
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
let tcx = self.r.tcx();
let gate_err_sym_msg = match prim {
PrimTy::Float(FloatTy::F16) if !tcx.features().f16 => {
Some((sym::f16, "the type `f16` is unstable"))
}
PrimTy::Float(FloatTy::F128) if !tcx.features().f128 => {
Some((sym::f128, "the type `f128` is unstable"))
}
_ => None,
};
if let Some((sym, msg)) = gate_err_sym_msg {
let span = path[0].ident.span;
if !span.allows_unstable(sym) {
feature_err(tcx.sess, sym, span, msg).emit();
}
};
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
}
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {

View File

@ -0,0 +1,9 @@
# `f128`
The tracking issue for this feature is: [#116909]
[#116909]: https://github.com/rust-lang/rust/issues/116909
---
Enable the `f128` type for IEEE 128-bit floating numbers (quad precision).

View File

@ -0,0 +1,9 @@
# `f16`
The tracking issue for this feature is: [#116909]
[#116909]: https://github.com/rust-lang/rust/issues/116909
---
Enable the `f16` type for IEEE 16-bit floating numbers (half precision).

View File

@ -0,0 +1,15 @@
#![allow(unused)]
const A: f128 = 10.0; //~ ERROR the type `f128` is unstable
pub fn main() {
let a: f128 = 100.0; //~ ERROR the type `f128` is unstable
let b = 0.0f128; //~ ERROR the type `f128` is unstable
foo(1.23);
}
fn foo(a: f128) {} //~ ERROR the type `f128` is unstable
struct Bar {
a: f128, //~ ERROR the type `f128` is unstable
}

View File

@ -0,0 +1,53 @@
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:3:10
|
LL | const A: f128 = 10.0;
| ^^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:6:12
|
LL | let a: f128 = 100.0;
| ^^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:11:11
|
LL | fn foo(a: f128) {}
| ^^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:14:8
|
LL | a: f128,
| ^^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:7:13
|
LL | let b = 0.0f128;
| ^^^^^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f128)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0658`.

View File

@ -0,0 +1,15 @@
#![allow(unused)]
const A: f16 = 10.0; //~ ERROR the type `f16` is unstable
pub fn main() {
let a: f16 = 100.0; //~ ERROR the type `f16` is unstable
let b = 0.0f16; //~ ERROR the type `f16` is unstable
foo(1.23);
}
fn foo(a: f16) {} //~ ERROR the type `f16` is unstable
struct Bar {
a: f16, //~ ERROR the type `f16` is unstable
}

View File

@ -0,0 +1,53 @@
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:3:10
|
LL | const A: f16 = 10.0;
| ^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f16)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:6:12
|
LL | let a: f16 = 100.0;
| ^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f16)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:11:11
|
LL | fn foo(a: f16) {}
| ^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f16)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:14:8
|
LL | a: f16,
| ^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f16)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:7:13
|
LL | let b = 0.0f16;
| ^^^^^^
|
= note: see issue #116909 <https://github.com/rust-lang/rust/issues/116909> for more information
= help: add `#![feature(f16)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0658`.

View File

@ -0,0 +1,31 @@
//@ compile-flags: --crate-type=lib
//@ check-pass
// Verify that gates for the `f16` and `f128` features do not apply to user types
mod binary16 {
#[allow(non_camel_case_types)]
pub struct f16(u16);
}
mod binary128 {
#[allow(non_camel_case_types)]
pub struct f128(u128);
}
pub use binary128::f128;
pub use binary16::f16;
mod private16 {
use crate::f16;
pub trait SealedHalf {}
impl SealedHalf for f16 {}
}
mod private128 {
use crate::f128;
pub trait SealedQuad {}
impl SealedQuad for f128 {}
}