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

Emit error when range pattern is empty #2795

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 38 additions & 0 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
#include "rust-pattern.h"
#include "safe-ctype.h"

namespace Rust {

Expand Down Expand Up @@ -180,4 +182,40 @@ ASTValidation::visit (AST::Module &module)
AST::ContextualASTVisitor::visit (module);
}

void
ASTValidation::visit (AST::RangePattern &pattern)
{
if (pattern.get_has_lower_bound () && pattern.get_has_upper_bound ())
{
auto &lower = pattern.get_lower_bound ();
auto &upper = pattern.get_upper_bound ();

if (lower->get_bound_type () == AST::RangePatternBound::LITERAL
&& upper->get_bound_type () == AST::RangePatternBound::LITERAL)
{
int lower_value{};
int upper_value{};
// if numeric range, get lower and upper numeric values
if (ISDIGIT (lower->as_string ()[0]))
{
lower_value = std::stoi (lower->as_string ());
upper_value = std::stoi (upper->as_string ());
}
// else if character range, get lower and upper ASCII values
else
{
lower_value = lower->as_string ()[0];
upper_value = upper->as_string ()[0];
}
// raise error if range is empty
if (lower_value > upper_value)
{
rust_error_at (
pattern.get_locus (), ErrorCode::E0030,
"lower range bound must be less than or equal to upper");
}
}
}
}

} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ASTValidation : public AST::ContextualASTVisitor
virtual void visit (AST::Function &function);
virtual void visit (AST::Trait &trait);
virtual void visit (AST::TraitFunctionDecl &decl);
virtual void visit (AST::RangePattern &pattern);
};

} // namespace Rust
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10435,7 +10435,7 @@ Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()

return std::unique_ptr<AST::RangePattern> (
new AST::RangePattern (std::move (lower), std::move (upper),
range_lower->get_locus ()));
range_lower->get_locus (), next->get_id () == ELLIPSIS));
}
else
{
Expand Down
15 changes: 15 additions & 0 deletions gcc/testsuite/rust/compile/issue-2794.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fn main() {
let mut x = 11;
match x {
2..=1 => x = 6,
// { dg-error "lower range bound must be less than or equal to upper" "" { target *-*-* } .-1 }
_ => x = 7,
}

let mut y = 'a';
match y {
'z'..='y' => y = 'b',
// { dg-error "lower range bound must be less than or equal to upper" "" { target *-*-* } .-1 }
_ => y = 'c',
}
}