Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly check never_type feature gating #120552

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,19 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}
}

fn visit_generic_args(&mut self, args: &'a ast::GenericArgs) {
// This check needs to happen here because the never type can be returned from a function,
// but cannot be used in any other context. If this check was in `visit_fn_ret_ty`, it
// include both functions and generics like `impl Fn() -> !`.
if let ast::GenericArgs::Parenthesized(generic_args) = args
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls leave a comment explaining why this is being checked here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

&& let ast::FnRetTy::Ty(ref ty) = generic_args.output
&& matches!(ty.kind, ast::TyKind::Never)
{
gate!(&self, never_type, ty.span, "the `!` type is experimental");
}
visit::walk_generic_args(self, args);
}

fn visit_expr(&mut self, e: &'a ast::Expr) {
match e.kind {
ast::ExprKind::TryBlock(_) => {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/feature-gates/feature-gate-never_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,14 @@ impl Foo for Meeshka {
type Wub = !; //~ ERROR type is experimental
}

fn look_ma_no_feature_gate<F: FnOnce() -> !>() {} //~ ERROR type is experimental
fn tadam(f: &dyn Fn() -> !) {} //~ ERROR type is experimental
fn panic() -> ! {
panic!();
}
fn toudoum() -> impl Fn() -> ! { //~ ERROR type is experimental
panic
}

fn main() {
}
32 changes: 31 additions & 1 deletion tests/ui/feature-gates/feature-gate-never_type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ LL | type Wub = !;
= help: add `#![feature(never_type)]` 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 is experimental
--> $DIR/feature-gate-never_type.rs:16:43
|
LL | fn look_ma_no_feature_gate<F: FnOnce() -> !>() {}
| ^
|
= note: see issue #35121 <https://github.com/rust-lang/rust/issues/35121> for more information
= help: add `#![feature(never_type)]` 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 is experimental
--> $DIR/feature-gate-never_type.rs:17:26
|
LL | fn tadam(f: &dyn Fn() -> !) {}
| ^
|
= note: see issue #35121 <https://github.com/rust-lang/rust/issues/35121> for more information
= help: add `#![feature(never_type)]` 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 is experimental
--> $DIR/feature-gate-never_type.rs:21:30
|
LL | fn toudoum() -> impl Fn() -> ! {
| ^
|
= note: see issue #35121 <https://github.com/rust-lang/rust/issues/35121> for more information
= help: add `#![feature(never_type)]` 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 8 previous errors

For more information about this error, try `rustc --explain E0658`.
7 changes: 7 additions & 0 deletions tests/ui/never_type/never-type-in-nested-fn-decl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// build-pass

trait X<const N: i32> {}

fn hello<T: X<{ fn hello() -> ! { loop {} } 1 }>>() {}

fn main() {}
Loading