Rollup merge of #139294 - beetrees:fix-f16-f128-literal-feature-gate, r=fmease

Fix the `f16`/`f128` feature gates on integer literals

The feature gating logic for `f16`/`f128` currently only checks float literals, meaning this code currently compiles with no feature gates on stable ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=b0c0e285ccb822fc7e2abc595557886b)):
```rust
fn main() {
    let a = 1f16;
    let b = 1f128;
    dbg!(a, b);
}
```
This PR fixes that.

Tracking issue: #116909
This commit is contained in:
Matthias Krüger 2025-04-03 07:39:08 +02:00 committed by GitHub
commit 29c0fe747a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 66 additions and 22 deletions

View file

@ -332,17 +332,19 @@ 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")
}
_ => (),
ast::ExprKind::Lit(token::Lit {
kind: token::LitKind::Float | token::LitKind::Integer,
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

@ -19,7 +19,7 @@ LL | let a: f128 = 100.0;
= 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:15:11
--> $DIR/feature-gate-f128.rs:16:11
|
LL | fn foo(a: f128) {}
| ^^^^
@ -29,7 +29,7 @@ LL | fn foo(a: f128) {}
= 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:18:8
--> $DIR/feature-gate-f128.rs:19:8
|
LL | a: f128,
| ^^^^
@ -48,6 +48,16 @@ LL | let b = 0.0f128;
= 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
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:12:13
|
LL | let c = 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 6 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -19,7 +19,7 @@ LL | let a: f128 = 100.0;
= 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:15:11
--> $DIR/feature-gate-f128.rs:16:11
|
LL | fn foo(a: f128) {}
| ^^^^
@ -29,7 +29,7 @@ LL | fn foo(a: f128) {}
= 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:18:8
--> $DIR/feature-gate-f128.rs:19:8
|
LL | a: f128,
| ^^^^
@ -48,6 +48,16 @@ LL | let b = 0.0f128;
= 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
error[E0658]: the type `f128` is unstable
--> $DIR/feature-gate-f128.rs:12:13
|
LL | let c = 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 6 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -9,6 +9,7 @@ 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
let c = 0f128; //~ ERROR the type `f128` is unstable
foo(1.23);
}

View file

@ -19,7 +19,7 @@ LL | let a: f16 = 100.0;
= 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:15:11
--> $DIR/feature-gate-f16.rs:16:11
|
LL | fn foo(a: f16) {}
| ^^^
@ -29,7 +29,7 @@ LL | fn foo(a: f16) {}
= 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:18:8
--> $DIR/feature-gate-f16.rs:19:8
|
LL | a: f16,
| ^^^
@ -48,6 +48,16 @@ LL | let b = 0.0f16;
= 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
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:12:13
|
LL | let c = 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 6 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -19,7 +19,7 @@ LL | let a: f16 = 100.0;
= 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:15:11
--> $DIR/feature-gate-f16.rs:16:11
|
LL | fn foo(a: f16) {}
| ^^^
@ -29,7 +29,7 @@ LL | fn foo(a: f16) {}
= 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:18:8
--> $DIR/feature-gate-f16.rs:19:8
|
LL | a: f16,
| ^^^
@ -48,6 +48,16 @@ LL | let b = 0.0f16;
= 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
error[E0658]: the type `f16` is unstable
--> $DIR/feature-gate-f16.rs:12:13
|
LL | let c = 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 6 previous errors
For more information about this error, try `rustc --explain E0658`.

View file

@ -9,6 +9,7 @@ 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
let c = 0f16; //~ ERROR the type `f16` is unstable
foo(1.23);
}