[COMMITTED 60/77] gccrs: lang: Add range_inclusive_new
arthur.cohen@embecosm.com
arthur.cohen@embecosm.com
Fri Aug 7 12:39:51 GMT 2026
From: Enes Cevik <enes@nsvke.com>
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 <enes@nsvke.com>
---
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 f41ba8bcf8c..0e934307755 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 91284e27b08..731fd23f314 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 d96fa9e6a78..9684148f755 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 8682e5a55ae..328f0cfa72e 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() {
--
2.50.1
More information about the Gcc-rust
mailing list