From: Enes Cevik <[email protected]>
This patch introduces the 'range_inclusive_new' lang item to the compiler.
When the compiler encounters an inclusive range expression, it now
correctly desugars the operation into a function call targeting this
lang item instead of lowering it directly into a static struct.
gcc/rust/ChangeLog:
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Add
desugaring for range_inclusive_new lang item.
* util/rust-lang-item.cc (Rust::LangItem::lang_items): Add
range_inclusive_new to the BiMap.
* util/rust-lang-item.h (class LangItem): Add RANGE_INCLUSIVE_NEW
to the Kind enum.
gcc/testsuite/ChangeLog:
* rust/compile/torture/range-lang-item1.rs: Update test to use
the new lang item.
Signed-off-by: Enes Cevik <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github:
https://github.com/Rust-GCC/gccrs/commit/3dd24d7628c65ff3252091aa8b47a56a07a6c5eb
The commit has NOT been mentioned in any issue.
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4704
gcc/rust/hir/rust-ast-lower-expr.cc | 26 ++++++++++++++-----
gcc/rust/util/rust-lang-item.cc | 1 +
gcc/rust/util/rust-lang-item.h | 1 +
.../rust/compile/torture/range-lang-item1.rs | 12 +++++++++
4 files changed, 33 insertions(+), 7 deletions(-)
diff --git a/gcc/rust/hir/rust-ast-lower-expr.cc
b/gcc/rust/hir/rust-ast-lower-expr.cc
index f41ba8bcf..0e9343077 100644
--- a/gcc/rust/hir/rust-ast-lower-expr.cc
+++ b/gcc/rust/hir/rust-ast-lower-expr.cc
@@ -796,18 +796,30 @@ void
ASTLoweringExpr::visit (AST::RangeFromToInclExpr &expr)
{
auto crate_num = mappings.get_current_crate ();
- Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
- mappings.get_next_hir_id (crate_num),
- UNKNOWN_LOCAL_DEFID);
+ Analysis::NodeMapping path_mapping (crate_num, mappings.get_next_node_id (),
+ mappings.get_next_hir_id (crate_num),
+ UNKNOWN_LOCAL_DEFID);
+ Analysis::NodeMapping call_mapping (crate_num, expr.get_node_id (),
+ mappings.get_next_hir_id (crate_num),
+ UNKNOWN_LOCAL_DEFID);
+
+ HIR::Expr *func
+ = new HIR::PathInExpression (path_mapping,
+ LangItem::Kind::RANGE_INCLUSIVE_NEW,
+ expr.get_locus (), false);
HIR::Expr *range_from = ASTLoweringExpr::translate (expr.get_from_expr ());
HIR::Expr *range_to = ASTLoweringExpr::translate (expr.get_to_expr ());
+ std::vector<std::unique_ptr<HIR::Expr>> params;
+ params.reserve (2);
+ params.emplace_back (std::unique_ptr<HIR::Expr> (range_from));
+ params.emplace_back (std::unique_ptr<HIR::Expr> (range_to));
+
translated
- = new HIR::RangeFromToInclExpr (mapping,
- std::unique_ptr<HIR::Expr> (range_from),
- std::unique_ptr<HIR::Expr> (range_to),
- expr.get_locus ());
+ = new HIR::CallExpr (call_mapping, std::unique_ptr<HIR::Expr> (func),
+ std::move (params), expr.get_outer_attrs (),
+ expr.get_locus ());
}
void
diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index 91284e27b..731fd23f3 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -55,6 +55,7 @@ const BiMap<std::string, LangItem::Kind>
Rust::LangItem::lang_items = {{
{"RangeTo", Kind::RANGE_TO},
{"RangeInclusive", Kind::RANGE_INCLUSIVE},
{"RangeToInclusive", Kind::RANGE_TO_INCLUSIVE},
+ {"range_inclusive_new", Kind::RANGE_INCLUSIVE_NEW},
{"phantom_data", Kind::PHANTOM_DATA},
{"fn", Kind::FN},
{"fn_mut", Kind::FN_MUT},
diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h
index d96fa9e6a..9684148f7 100644
--- a/gcc/rust/util/rust-lang-item.h
+++ b/gcc/rust/util/rust-lang-item.h
@@ -77,6 +77,7 @@ public:
RANGE_TO,
RANGE_INCLUSIVE,
RANGE_TO_INCLUSIVE,
+ RANGE_INCLUSIVE_NEW,
// https://github.com/rust-lang/rust/blob/master/library/core/src/marker.rs
PHANTOM_DATA,
diff --git a/gcc/testsuite/rust/compile/torture/range-lang-item1.rs
b/gcc/testsuite/rust/compile/torture/range-lang-item1.rs
index 8682e5a55..328f0cfa7 100644
--- a/gcc/testsuite/rust/compile/torture/range-lang-item1.rs
+++ b/gcc/testsuite/rust/compile/torture/range-lang-item1.rs
@@ -29,6 +29,18 @@ pub struct RangeTo<Idx> {
pub struct RangeInclusive<Idx> {
pub start: Idx,
pub end: Idx,
+ pub exhausted: bool,
+}
+
+impl<Idx> RangeInclusive<Idx> {
+ #[lang = "range_inclusive_new"]
+ pub const fn new(start: Idx, end: Idx) -> Self {
+ Self {
+ start,
+ end,
+ exhausted: false,
+ }
+ }
}
fn test() {
base-commit: adde96a15ced7011e432dadee73c19fc59eb9178
--
2.54.0