Skip to content

Commit 39c9618

Browse files
committed
gccrs: Add minus sign compilation for LiteralPattern
GIMPLE output for literalpattern_neg.rs test case: ... x = -55; RUSTTMP.2 = x; if (RUSTTMP.2 == 55) goto <D.113>; else goto <D.114>; <D.113>: { RUSTTMP.1 = 1; goto <D.107>; } <D.114>: if (RUSTTMP.2 == -55) goto <D.115>; else goto <D.116>; <D.115>: { RUSTTMP.1 = 0; goto <D.107>; } <D.116>: if (1 != 0) goto <D.117>; else goto <D.118>; <D.117>: { RUSTTMP.1 = 1; goto <D.107>; } ... gcc/rust/ChangeLog: * parse/rust-parse-impl.h (parse_literal_or_range_pattern): Parse minus sign properly for LiteralPattern. * ast/rust-pattern.h (LiteralPattern): Add has_minus boolean for LiteralPattern. * hir/tree/rust-hir-pattern.h (LiteralPattern): Ditto. * ast/rust-pattern.cc (LiteralPattern::as_string): Update to include minus sign if present. * hir/tree/rust-hir.cc (LiteralPattern::as_string): Ditto. * hir/rust-ast-lower-pattern.cc (visit(LiteralPattern)): Pass has_minus boolean from AST to HIR. * backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit(LiteralPattern)): Compile litexpr as negative if minus sign is present. Signed-off-by: Yap Zhi Heng <[email protected]>
1 parent 0cad640 commit 39c9618

File tree

8 files changed

+48
-8
lines changed

8 files changed

+48
-8
lines changed

gcc/rust/ast/rust-pattern.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ tokenid_to_rangekind (TokenId id)
4848
std::string
4949
LiteralPattern::as_string () const
5050
{
51-
return lit.as_string ();
51+
return (has_minus ? "-" : "") + lit.as_string ();
5252
}
5353

5454
std::string

gcc/rust/ast/rust-pattern.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,42 @@ class LiteralPattern : public Pattern
3030
Literal lit;
3131
location_t locus;
3232
NodeId node_id;
33+
bool has_minus;
3334

3435
public:
3536
std::string as_string () const override;
3637

3738
// Constructor for a literal pattern
3839
LiteralPattern (Literal lit, location_t locus)
3940
: lit (std::move (lit)), locus (locus),
40-
node_id (Analysis::Mappings::get ().get_next_node_id ())
41+
node_id (Analysis::Mappings::get ().get_next_node_id ()),
42+
has_minus (false)
43+
{}
44+
45+
LiteralPattern (Literal lit, location_t locus, bool has_minus)
46+
: lit (std::move (lit)), locus (locus),
47+
node_id (Analysis::Mappings::get ().get_next_node_id ()),
48+
has_minus (has_minus)
4149
{}
4250

4351
LiteralPattern (std::string val, Literal::LitType type, location_t locus,
4452
PrimitiveCoreType type_hint)
4553
: lit (Literal (std::move (val), type, type_hint)), locus (locus),
46-
node_id (Analysis::Mappings::get ().get_next_node_id ())
54+
node_id (Analysis::Mappings::get ().get_next_node_id ()),
55+
has_minus (false)
56+
{}
57+
58+
LiteralPattern (std::string val, Literal::LitType type, location_t locus,
59+
PrimitiveCoreType type_hint, bool has_minus)
60+
: lit (Literal (std::move (val), type, type_hint)), locus (locus),
61+
node_id (Analysis::Mappings::get ().get_next_node_id ()),
62+
has_minus (has_minus)
4763
{}
4864

4965
location_t get_locus () const override final { return locus; }
5066

67+
bool get_has_minus () const { return has_minus; }
68+
5169
void accept_vis (ASTVisitor &vis) override;
5270

5371
NodeId get_node_id () const override { return node_id; }

gcc/rust/backend/rust-compile-pattern.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern)
8787
auto litexpr = std::make_unique<HIR::LiteralExpr> (
8888
HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (),
8989
pattern.get_locus (), std::vector<AST::Attribute> ()));
90+
if (pattern.get_has_minus ())
91+
litexpr->set_negative ();
9092

9193
// Note: Floating point literals are currently accepted but will likely be
9294
// forbidden in LiteralPatterns in a future version of Rust.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ ASTLoweringPattern::visit (AST::LiteralPattern &pattern)
280280

281281
HIR::Literal l = lower_literal (pattern.get_literal ());
282282
translated
283-
= new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus ());
283+
= new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus (),
284+
pattern.get_has_minus ());
284285
}
285286

286287
void

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,27 @@ class LiteralPattern : public Pattern
3232
Literal lit;
3333
location_t locus;
3434
Analysis::NodeMapping mappings;
35+
bool has_minus;
3536

3637
public:
3738
std::string as_string () const override;
3839

3940
// Constructor for a literal pattern
4041
LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus)
41-
: lit (std::move (lit)), locus (locus), mappings (mappings)
42+
: lit (std::move (lit)), locus (locus), mappings (mappings),
43+
has_minus (false)
44+
{}
45+
46+
LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus,
47+
bool has_minus)
48+
: lit (std::move (lit)), locus (locus), mappings (mappings),
49+
has_minus (has_minus)
4250
{}
4351

4452
LiteralPattern (Analysis::NodeMapping mappings, std::string val,
4553
Literal::LitType type, location_t locus)
4654
: lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)),
47-
locus (locus), mappings (mappings)
55+
locus (locus), mappings (mappings), has_minus (false)
4856
{}
4957

5058
location_t get_locus () const override { return locus; }
@@ -65,6 +73,8 @@ class LiteralPattern : public Pattern
6573
Literal &get_literal () { return lit; }
6674
const Literal &get_literal () const { return lit; }
6775

76+
bool get_has_minus () const { return has_minus; }
77+
6878
protected:
6979
/* Use covariance to implement clone function as returning this object rather
7080
* than base */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,7 @@ StructPattern::as_string () const
26342634
std::string
26352635
LiteralPattern::as_string () const
26362636
{
2637-
return lit.as_string ();
2637+
return (has_minus ? "-" : "") + lit.as_string ();
26382638
}
26392639

26402640
std::string

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10345,7 +10345,7 @@ Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
1034510345
return std::unique_ptr<AST::LiteralPattern> (
1034610346
new AST::LiteralPattern (range_lower->get_str (), type,
1034710347
range_lower->get_locus (),
10348-
range_lower->get_type_hint ()));
10348+
range_lower->get_type_hint (), has_minus));
1034910349
}
1035010350
}
1035110351

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() -> i32 {
2+
let x = -55;
3+
4+
match x {
5+
55 => 1,
6+
-55 => 0, // correct case
7+
_ => 1
8+
}
9+
}

0 commit comments

Comments
 (0)