Skip to content

Commit

Permalink
Add feature gates for f16 and f128
Browse files Browse the repository at this point in the history
Includes related tests and documentation pages.
  • Loading branch information
tgross35 committed Mar 3, 2024
1 parent 61a9928 commit 72b387e
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,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.
Expand Down
51 changes: 50 additions & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::collect::HirPlaceholderCollector;
use crate::errors::AmbiguousLifetimeBound;
use crate::middle::resolve_bound_vars as rbv;
use crate::require_c_abi_if_c_variadic;
use rustc_ast::TraitObjectSyntax;
use rustc_ast::{FloatTy, TraitObjectSyntax};
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_errors::{
codes::*, struct_span_code_err, Applicability, Diag, ErrorGuaranteed, FatalError, MultiSpan,
Expand All @@ -33,6 +33,7 @@ use rustc_middle::ty::{
TypeVisitableExt,
};
use rustc_session::lint::builtin::AMBIGUOUS_ASSOCIATED_ITEMS;
use rustc_session::parse::feature_err;
use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_span::{sym, BytePos, Span, DUMMY_SP};
Expand Down Expand Up @@ -2174,6 +2175,29 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
}
});

if let Some(e) = self.check_float_gate(
prim_ty,
FloatTy::F16,
self.tcx().features().f16,
sym::f16,
path.span,
"the feature `f16` is unstable",
) {
return e;
}

if let Some(e) = self.check_float_gate(
prim_ty,
FloatTy::F128,
self.tcx().features().f128,
sym::f128,
path.span,
"the feature `f128` is unstable",
) {
return e;
}

match prim_ty {
hir::PrimTy::Bool => tcx.types.bool,
hir::PrimTy::Char => tcx.types.char,
Expand Down Expand Up @@ -2803,6 +2827,31 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
Some(r)
}

/// If the float type is not enabled by the feature, return `Some(error)`. `None` if there are
/// no problems.
fn check_float_gate(
&self,
prim_ty: hir::PrimTy,
gated_ty: FloatTy,
feat_enabled: bool,
feat_sym: Symbol,
span: Span,
msg: &'static str,
) -> Option<Ty<'tcx>> {
let hir::PrimTy::Float(float_ty) = prim_ty else {
return None;
};

if float_ty == gated_ty && !feat_enabled && !span.allows_unstable(feat_sym) {
let sess = self.tcx().sess;
let guar = feature_err(sess, sym::f16, span, msg).emit();
self.set_tainted_by_errors(guar);
Some(Ty::new_error(self.tcx(), guar))
} else {
None
}
}
}

fn assoc_kind_str(kind: ty::AssocKind) -> &'static str {
Expand Down
9 changes: 9 additions & 0 deletions src/doc/unstable-book/src/language-features/f128.md
Original file line number Diff line number Diff line change
@@ -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).
9 changes: 9 additions & 0 deletions src/doc/unstable-book/src/language-features/f16.md
Original file line number Diff line number Diff line change
@@ -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).
36 changes: 36 additions & 0 deletions tests/ui/feature-gates/feature-gate-f128.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![allow(unused)]

// ensure gating for primitive use
mod m1 {
const A: f128 = 10.0; //~ ERROR the feature `f128` is unstable

pub fn main() {
let a: f128 = 100.0; //~ ERROR the feature `f128` is unstable
let b = 0.0f128; //~ ERROR the feature `f128` is unstable
foo(1.23);
}

fn foo(a: f128) {} //~ ERROR the feature `f128` is unstable

struct Bar {
a: f128, //~ ERROR the feature `f128` is unstable
}
}

// ensure we don't restrict name as an identifier or custom types
mod mod1 {
#[allow(non_camel_case_types)]
struct f128 {
a: i32,
}

fn foo() {
let f128 = ();
}

fn bar(a: f128) -> i32 {
a.a
}
}

fn main() {}
43 changes: 43 additions & 0 deletions tests/ui/feature-gates/feature-gate-f128.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0658]: the feature `f128` is unstable
--> $DIR/feature-gate-f128.rs:5:14
|
LL | const A: f128 = 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 feature `f128` is unstable
--> $DIR/feature-gate-f128.rs:13:15
|
LL | fn foo(a: f128) {}
| ^^^^
|
= 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 feature `f128` is unstable
--> $DIR/feature-gate-f128.rs:16:12
|
LL | a: f128,
| ^^^^
|
= 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 feature `f128` is unstable
--> $DIR/feature-gate-f128.rs:8:16
|
LL | let a: f128 = 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: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0658`.
36 changes: 36 additions & 0 deletions tests/ui/feature-gates/feature-gate-f16.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![allow(unused)]

// ensure gating for primitive use
mod m1 {
const A: f16 = 10.0; //~ ERROR the feature `f16` is unstable

pub fn main() {
let a: f16 = 100.0; //~ ERROR the feature `f16` is unstable
let b = 0.0f16; //~ ERROR the feature `f16` is unstable
foo(1.23);
}

fn foo(a: f16) {} //~ ERROR the feature `f16` is unstable

struct Bar {
a: f16, //~ ERROR the feature `f16` is unstable
}
}

// ensure we don't restrict name as an identifier or custom types
mod m2 {
#[allow(non_camel_case_types)]
struct f16 {
a: i32,
}

fn foo() {
let f16 = ();
}

fn bar(a: f16) -> i32 {
a.a
}
}

fn main() {}
43 changes: 43 additions & 0 deletions tests/ui/feature-gates/feature-gate-f16.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0658]: the feature `f16` is unstable
--> $DIR/feature-gate-f16.rs:5:14
|
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 feature `f16` is unstable
--> $DIR/feature-gate-f16.rs:13:15
|
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 feature `f16` is unstable
--> $DIR/feature-gate-f16.rs:16:12
|
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 feature `f16` is unstable
--> $DIR/feature-gate-f16.rs:8:16
|
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: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0658`.

0 comments on commit 72b387e

Please sign in to comment.