diff options
author | Matty Kuhn <matty.kuhn.1@gmail.com> | 2025-04-04 18:09:41 -0600 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2025-04-08 10:17:20 +0200 |
commit | a75a697ff0a5bafd23c468940943b80082088dba (patch) | |
tree | 32426793754657450a48695ac93b20083f3d33d6 /gcc | |
parent | 8885e9e109b664977479b23d8b976fdfaa576665 (diff) | |
download | gcc-a75a697ff0a5bafd23c468940943b80082088dba.zip gcc-a75a697ff0a5bafd23c468940943b80082088dba.tar.gz gcc-a75a697ff0a5bafd23c468940943b80082088dba.tar.bz2 |
gccrs: fix ICE segfault with empty feature gate
This patch fixes an issue where an empty feature gate would segfault,
instead of reporting a syntax error to the user.
gcc/rust/ChangeLog:
* ast/rust-ast.h: (AST::Attribute): add empty_input function
* checks/errors/rust-feature-gate.cc: (FeatureGate::visit): check for empty feature gate
gcc/testsuite/ChangeLog:
* rust/compile/feature.rs: add an invalid empty feature to produce an error
Signed-off-by: Matty Kuhn <matty.kuhn.1@gmail.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast.h | 3 | ||||
-rw-r--r-- | gcc/rust/checks/errors/rust-feature-gate.cc | 7 | ||||
-rw-r--r-- | gcc/testsuite/rust/compile/feature.rs | 2 |
3 files changed, 12 insertions, 0 deletions
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 1091ba0..09e0fce 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -657,6 +657,9 @@ public: // Returns whether the attribute is considered an "empty" attribute. bool is_empty () const { return attr_input == nullptr && path.is_empty (); } + // Returns whether the attribute has no input + bool empty_input () const { return !attr_input; } + location_t get_locus () const { return locus; } AttrInput &get_attr_input () const { return *attr_input; } diff --git a/gcc/rust/checks/errors/rust-feature-gate.cc b/gcc/rust/checks/errors/rust-feature-gate.cc index f3daa61..44007f9 100644 --- a/gcc/rust/checks/errors/rust-feature-gate.cc +++ b/gcc/rust/checks/errors/rust-feature-gate.cc @@ -40,6 +40,13 @@ FeatureGate::visit (AST::Crate &crate) { if (attr.get_path ().as_string () == "feature") { + // check for empty feature, such as `#![feature], this is an error + if (attr.empty_input ()) + { + rust_error_at (attr.get_locus (), ErrorCode::E0556, + "malformed %<feature%> attribute input"); + continue; + } const auto &attr_input = attr.get_attr_input (); auto type = attr_input.get_attr_input_type (); if (type == AST::AttrInput::AttrInputType::TOKEN_TREE) diff --git a/gcc/testsuite/rust/compile/feature.rs b/gcc/testsuite/rust/compile/feature.rs index f743f92..6f428f0 100644 --- a/gcc/testsuite/rust/compile/feature.rs +++ b/gcc/testsuite/rust/compile/feature.rs @@ -2,5 +2,7 @@ #![feature(AA)] //{ dg-error "unknown feature .AA." } #![feature(iamcrabby)] // { dg-error "unknown feature .iamcrabby." } #![feature(nonexistent_gccrs_feature)] // { dg-error "unknown feature .nonexistent_gccrs_feature." } +// ErrorCode - E0556 +#![feature] // { dg-error "malformed .feature. attribute input" } fn main() {} |