Skip to content

Commit edd5bf4

Browse files
committed
gccrs: Fix RangePattern negative literal bounds being treated as positive.
This commit removes the need for a tracking `has_minus` boolean in `RangePattern` for both AST and HIR, and just parses the negatable literals as negative values. gcc/rust/ChangeLog: * parse/rust-parse-impl.h: * parse_literal_or_range_pattern: Parse literal range bounds as negative values, instead of positive values with `has_minus` bool. * parse_range_pattern_bound: Ditto. * ast/rust-pattern.h (RangePatternBoundLiteral): Remove `has_minus` boolean. * hir/tree/rust-hir-pattern.h (RangePatternBoundLiteral): Ditto. * ast/rust-ast-collector.cc (visit(RestPattern)): Ditto. * ast/rust-pattern.cc (RangePatternBoundLiteral::as_string): Ditto. * hir/rust-ast-lower-base.cc (lower_range_pattern_bound): Ditto. * hir/rust-hir-dump.cc(visit(RangePatternBoundLiteral)): Ditto. * hir/tree/rust-hir.cc(RangePatternBoundLiteral::as_string): Ditto. Signed-off-by: Yap Zhi Heng <yapzhhg@gmail.com>
1 parent ab68ca3 commit edd5bf4

File tree

10 files changed

+37
-36
lines changed

10 files changed

+37
-36
lines changed

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,10 +2509,6 @@ TokenCollector::visit (RestPattern &pattern)
25092509
void
25102510
TokenCollector::visit (RangePatternBoundLiteral &pattern)
25112511
{
2512-
if (pattern.get_has_minus ())
2513-
{
2514-
push (Rust::Token::make (MINUS, pattern.get_locus ()));
2515-
}
25162512
auto literal = pattern.get_literal ();
25172513
visit (literal);
25182514
}

gcc/rust/ast/rust-pattern.cc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ RangePatternBoundLiteral::as_string () const
7676
{
7777
std::string str;
7878

79-
if (has_minus)
80-
str += "-";
81-
8279
str += literal.as_string ();
8380

8481
return str;

gcc/rust/ast/rust-pattern.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,18 @@ class RangePatternBoundLiteral : public RangePatternBound
266266
/* Can only be a char, byte, int, or float literal - same impl here as
267267
* previously */
268268

269-
// Minus prefixed to literal (if integer or floating-point)
270-
bool has_minus;
271-
272269
location_t locus;
273270

274271
public:
275272
// Constructor
276-
RangePatternBoundLiteral (Literal literal, location_t locus,
277-
bool has_minus = false)
278-
: literal (literal), has_minus (has_minus), locus (locus)
273+
RangePatternBoundLiteral (Literal literal, location_t locus)
274+
: literal (literal), locus (locus)
279275
{}
280276

281277
std::string as_string () const override;
282278

283279
Literal get_literal () const { return literal; }
284280

285-
bool get_has_minus () const { return has_minus; }
286-
287281
location_t get_locus () const { return locus; }
288282

289283
void accept_vis (ASTVisitor &vis) override;

gcc/rust/hir/rust-ast-lower-base.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,7 @@ ASTLoweringBase::lower_range_pattern_bound (AST::RangePatternBound &bound)
952952
HIR::Literal literal = lower_literal (ref.get_literal ());
953953

954954
hir_bound = std::unique_ptr<HIR::RangePatternBound> (
955-
new HIR::RangePatternBoundLiteral (literal, ref.get_locus (),
956-
ref.get_has_minus ()));
955+
new HIR::RangePatternBoundLiteral (literal, ref.get_locus ()));
957956
}
958957
break;
959958
case AST::RangePatternBound::RangePatternBoundType::PATH:

gcc/rust/hir/rust-hir-dump.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2217,7 +2217,6 @@ Dump::visit (RangePatternBoundLiteral &e)
22172217
{
22182218
begin ("RangePatternBoundLiteral");
22192219
put_field ("literal", e.get_literal ().as_string ());
2220-
put_field ("has_minus", std::to_string (e.get_has_minus ()));
22212220
end ("RangePatternBoundLiteral");
22222221
}
22232222

gcc/rust/hir/tree/rust-hir-pattern.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,19 @@ class RangePatternBoundLiteral : public RangePatternBound
234234
/* Can only be a char, byte, int, or float literal - same impl here as
235235
* previously */
236236

237-
// Minus prefixed to literal (if integer or floating-point)
238-
bool has_minus;
239-
240237
location_t locus;
241238

242239
public:
243240
// Constructor
244-
RangePatternBoundLiteral (Literal literal, location_t locus,
245-
bool has_minus = false)
246-
: literal (literal), has_minus (has_minus), locus (locus)
241+
RangePatternBoundLiteral (Literal literal, location_t locus)
242+
: literal (literal), locus (locus)
247243
{}
248244

249245
std::string as_string () const override;
250246

251247
location_t get_locus () const { return locus; }
252248

253249
Literal get_literal () const { return literal; }
254-
bool get_has_minus () const { return has_minus; }
255250

256251
void accept_vis (HIRFullVisitor &vis) override;
257252

gcc/rust/hir/tree/rust-hir.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,11 +2375,6 @@ RangePatternBoundLiteral::as_string () const
23752375
{
23762376
std::string str;
23772377

2378-
if (has_minus)
2379-
{
2380-
str += "-";
2381-
}
2382-
23832378
str += literal.as_string ();
23842379

23852380
return str;

gcc/rust/parse/rust-parse-impl.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10318,11 +10318,16 @@ Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
1031810318
AST::RangeKind kind = AST::tokenid_to_rangekind (next->get_id ());
1031910319
// range pattern
1032010320
lexer.skip_token ();
10321+
std::string range_lower_str;
10322+
if (has_minus)
10323+
range_lower_str = "-" + range_lower->get_str ();
10324+
else
10325+
range_lower_str = range_lower->get_str ();
1032110326
std::unique_ptr<AST::RangePatternBound> lower (
1032210327
new AST::RangePatternBoundLiteral (
10323-
AST::Literal (range_lower->get_str (), type,
10328+
AST::Literal (range_lower_str, type,
1032410329
PrimitiveCoreType::CORETYPE_UNKNOWN),
10325-
range_lower->get_locus (), has_minus));
10330+
range_lower->get_locus ()));
1032610331

1032710332
std::unique_ptr<AST::RangePatternBound> upper
1032810333
= parse_range_pattern_bound ();
@@ -10398,17 +10403,17 @@ Parser<ManagedTokenSource>::parse_range_pattern_bound ()
1039810403
lexer.skip_token (1);
1039910404
return std::unique_ptr<AST::RangePatternBoundLiteral> (
1040010405
new AST::RangePatternBoundLiteral (
10401-
AST::Literal (range_lower->get_str (), AST::Literal::INT,
10406+
AST::Literal ("-" + range_lower->get_str (), AST::Literal::INT,
1040210407
range_lower->get_type_hint ()),
10403-
range_lower_locus, true));
10408+
range_lower_locus));
1040410409
case FLOAT_LITERAL:
1040510410
lexer.skip_token (1);
1040610411
rust_debug ("warning: used deprecated float range pattern bound");
1040710412
return std::unique_ptr<AST::RangePatternBoundLiteral> (
1040810413
new AST::RangePatternBoundLiteral (
10409-
AST::Literal (range_lower->get_str (), AST::Literal::FLOAT,
10414+
AST::Literal ("-" + range_lower->get_str (), AST::Literal::FLOAT,
1041010415
range_lower->get_type_hint ()),
10411-
range_lower_locus, true));
10416+
range_lower_locus));
1041210417
default:
1041310418
add_error (Error (range_lower->get_locus (),
1041410419
"token type %qs cannot be parsed as range pattern "
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(exclusive_range_pattern)]
2+
3+
fn main() {
4+
let x = 1;
5+
6+
match x {
7+
-55..0 => 2,
8+
-99..-55 => 3,
9+
};
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(exclusive_range_pattern)]
2+
3+
fn main() -> i32 {
4+
let x = -77;
5+
6+
match x {
7+
-55..99 => 1,
8+
-99..-55 => 0, // the correct case
9+
_ => 1,
10+
}
11+
}

0 commit comments

Comments
 (0)