Skip to content
Open
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
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tokenid_to_rangekind (TokenId id)
std::string
LiteralPattern::as_string () const
{
return lit.as_string ();
return (has_minus ? "-" : "") + lit.as_string ();
}

std::string
Expand Down
22 changes: 20 additions & 2 deletions gcc/rust/ast/rust-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,42 @@ class LiteralPattern : public Pattern
Literal lit;
location_t locus;
NodeId node_id;
bool has_minus;

public:
std::string as_string () const override;

// Constructor for a literal pattern
LiteralPattern (Literal lit, location_t locus)
: lit (std::move (lit)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ())
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (false)
{}

LiteralPattern (Literal lit, location_t locus, bool has_minus)
: lit (std::move (lit)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (has_minus)
{}

LiteralPattern (std::string val, Literal::LitType type, location_t locus,
PrimitiveCoreType type_hint)
: lit (Literal (std::move (val), type, type_hint)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ())
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (false)
{}

LiteralPattern (std::string val, Literal::LitType type, location_t locus,
PrimitiveCoreType type_hint, bool has_minus)
: lit (Literal (std::move (val), type, type_hint)), locus (locus),
node_id (Analysis::Mappings::get ().get_next_node_id ()),
has_minus (has_minus)
{}

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

bool get_has_minus () const { return has_minus; }

void accept_vis (ASTVisitor &vis) override;

NodeId get_node_id () const override { return node_id; }
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/backend/rust-compile-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern)
auto litexpr = std::make_unique<HIR::LiteralExpr> (
HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (),
pattern.get_locus (), std::vector<AST::Attribute> ()));
if (pattern.get_has_minus ())
litexpr->set_negative ();

// Note: Floating point literals are currently accepted but will likely be
// forbidden in LiteralPatterns in a future version of Rust.
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/hir/rust-ast-lower-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ ASTLoweringPattern::visit (AST::LiteralPattern &pattern)

HIR::Literal l = lower_literal (pattern.get_literal ());
translated
= new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus ());
= new HIR::LiteralPattern (mapping, std::move (l), pattern.get_locus (),
pattern.get_has_minus ());
}

void
Expand Down
14 changes: 12 additions & 2 deletions gcc/rust/hir/tree/rust-hir-pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,27 @@ class LiteralPattern : public Pattern
Literal lit;
location_t locus;
Analysis::NodeMapping mappings;
bool has_minus;

public:
std::string as_string () const override;

// Constructor for a literal pattern
LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus)
: lit (std::move (lit)), locus (locus), mappings (mappings)
: lit (std::move (lit)), locus (locus), mappings (mappings),
has_minus (false)
{}

LiteralPattern (Analysis::NodeMapping mappings, Literal lit, location_t locus,
bool has_minus)
: lit (std::move (lit)), locus (locus), mappings (mappings),
has_minus (has_minus)
{}

LiteralPattern (Analysis::NodeMapping mappings, std::string val,
Literal::LitType type, location_t locus)
: lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)),
locus (locus), mappings (mappings)
locus (locus), mappings (mappings), has_minus (false)
{}

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

bool get_has_minus () const { return has_minus; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/hir/tree/rust-hir.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2634,7 +2634,7 @@ StructPattern::as_string () const
std::string
LiteralPattern::as_string () const
{
return lit.as_string ();
return (has_minus ? "-" : "") + lit.as_string ();
}

std::string
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 @@ -10345,7 +10345,7 @@ Parser<ManagedTokenSource>::parse_literal_or_range_pattern ()
return std::unique_ptr<AST::LiteralPattern> (
new AST::LiteralPattern (range_lower->get_str (), type,
range_lower->get_locus (),
range_lower->get_type_hint ()));
range_lower->get_type_hint (), has_minus));
}
}

Expand Down
9 changes: 9 additions & 0 deletions gcc/testsuite/rust/execute/torture/literalpattern_neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn main() -> i32 {
let x = -55;

match x {
55 => 1,
-55 => 0, // correct case
_ => 1
}
}
Loading