[gcc r15-8925] libstdc++: Check presence of iterator_category for flat_sets insert_range [PR119415]

2025-03-26 Thread Tomasz Kaminski via Gcc-cvs
https://gcc.gnu.org/g:272d26d519f5abcca71f5b1d5acc07319e480ba3

commit r15-8925-g272d26d519f5abcca71f5b1d5acc07319e480ba3
Author: Tomasz Kamiński 
Date:   Wed Mar 26 07:34:37 2025 +0100

libstdc++: Check presence of iterator_category for flat_sets insert_range 
[PR119415]

As pointed out by Hewill Kang (reporter) in the issue, checking if iterator
of the incoming range satisfies __cpp17_input_iterator, may still lead
to hard errors inside of insert_range for iterators that satisfies
that concept, but specialize iterator_traits without iterator_category
typedef (std::common_iterator specialize iterator_traits without
iterator_category in some cases).

To address that we instead check if the 
iterator_traits::iterator_category
is present and denote at least input_iterator_tag, using existing 
__has_input_iter_cat.

PR libstdc++/119415

libstdc++-v3/ChangeLog:

* include/std/flat_set (_Flat_set_impl:insert_range):
Replace __detail::__cpp17_input_iterator with __has_input_iter_cat.
* testsuite/23_containers/flat_multiset/1.cc: New tests
* testsuite/23_containers/flat_set/1.cc: New tests

Reviewed-by: Jonathan Wakely 
Signed-off-by: Tomasz Kamiński 

Diff:
---
 libstdc++-v3/include/std/flat_set  |  2 +-
 .../testsuite/23_containers/flat_multiset/1.cc | 57 +++---
 libstdc++-v3/testsuite/23_containers/flat_set/1.cc | 57 +++---
 3 files changed, 103 insertions(+), 13 deletions(-)

diff --git a/libstdc++-v3/include/std/flat_set 
b/libstdc++-v3/include/std/flat_set
index bab56742ddd6..a7b0b8aef151 100644
--- a/libstdc++-v3/include/std/flat_set
+++ b/libstdc++-v3/include/std/flat_set
@@ -481,7 +481,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
  if constexpr (requires { _M_cont.insert_range(_M_cont.end(), __rg); })
__it = _M_cont.insert_range(_M_cont.end(), __rg);
  else if constexpr (ranges::common_range<_Rg>
-&& 
__detail::__cpp17_input_iterator>)
+&& __has_input_iter_cat>)
__it = _M_cont.insert(_M_cont.end(), ranges::begin(__rg), 
ranges::end(__rg));
  else
{
diff --git a/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc 
b/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc
index cc31164315ae..dc3cecd9720c 100644
--- a/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc
+++ b/libstdc++-v3/testsuite/23_containers/flat_multiset/1.cc
@@ -152,19 +152,64 @@ struct NoInsertRange : std::vector
   void insert_range(typename std::vector::const_iterator, R&&) = delete;
 };
 
+struct NoCatIterator {
+  using difference_type = int;
+  using value_type = int;
+
+  NoCatIterator() : v(0) {}
+  NoCatIterator(int x) : v(x) {}
+
+  int operator*() const
+  { return v; }
+
+  NoCatIterator& operator++()
+  {
+++v;
+return *this;
+  }
+
+  NoCatIterator operator++(int)
+  {
+++v;
+return NoCatIterator(v-1);
+  }
+
+  bool operator==(const NoCatIterator& rhs) const
+  { return v == rhs.v; }
+
+private:
+  int v;
+};
+
+template<>
+struct std::iterator_traits {
+  using difference_type = int;
+  using value_type = int;
+  using iterator_concept = std::input_iterator_tag;
+  // no iterator_category, happens also for common_iterator
+};
+
 void test07()
 {
+  std::flat_multiset s;
+  std::flat_multiset, NoInsertRange> s2;
+
+  auto r = std::ranges::subrange(1, 6);
+  s.insert_range(r);
+  VERIFY( std::ranges::equal(s, (int[]){1, 2, 3, 4, 5}) );
+  s2.insert_range(r);
+  VERIFY( std::ranges::equal(s2, (int[]){1, 2, 3, 4, 5}) );
+
 #ifdef __SIZEOF_INT128__
   // PR libstdc++/119415 - flat_foo::insert_range cannot handle common ranges
   // on c++20 only iterators
-  auto r = std::views::iota(__int128(1), __int128(6));
-
-  std::flat_multiset s;
-  s.insert_range(r);
+  auto r2 = std::views::iota(__int128(1), __int128(6));
+  s.clear();
+  s.insert_range(r2);
   VERIFY( std::ranges::equal(s, (int[]){1, 2, 3, 4, 5}) );
 
-  std::flat_multiset, NoInsertRange> s2;
-  s2.insert_range(r);
+  s2.clear();
+  s2.insert_range(r2);
   VERIFY( std::ranges::equal(s2, (int[]){1, 2, 3, 4, 5}) );
 #endif
 }
diff --git a/libstdc++-v3/testsuite/23_containers/flat_set/1.cc 
b/libstdc++-v3/testsuite/23_containers/flat_set/1.cc
index 16881d788fcd..90f5855859f7 100644
--- a/libstdc++-v3/testsuite/23_containers/flat_set/1.cc
+++ b/libstdc++-v3/testsuite/23_containers/flat_set/1.cc
@@ -167,19 +167,64 @@ struct NoInsertRange : std::vector
   void insert_range(typename std::vector::const_iterator, R&&) = delete;
 };
 
+struct NoCatIterator {
+  using difference_type = int;
+  using value_type = int;
+
+  NoCatIterator() : v(0) {}
+  NoCatIterator(int x) : v(x) {}
+
+  int operator*() const
+  { return v; }
+
+  NoCatIterator& operator++()
+  {
+++v;
+return *this;
+  }
+
+  NoCatIterator operator++(int)
+  {
+++v;
+return NoCatIter

[gcc r15-8533] gccrs: Fix FnParam pattern location ternary logic

2025-03-26 Thread Arthur Cohen via Gcc-cvs
https://gcc.gnu.org/g:b3246d3ff22035cff465e75ebe42ff2be4ce46d2

commit r15-8533-gb3246d3ff22035cff465e75ebe42ff2be4ce46d2
Author: Pierre-Emmanuel Patry 
Date:   Wed Nov 20 01:28:04 2024 +0100

gccrs: Fix FnParam pattern location ternary logic

Condition was inverted, we should retrieve the locus only if we have a
pattern.

gcc/rust/ChangeLog:

* typecheck/rust-tyty-call.cc (TypeCheckCallExpr::visit): Do not
get a reference if the pattern does not exist.
(TypeCheckMethodCallExpr::check): Likewise.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/typecheck/rust-tyty-call.cc | 10 --
 1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/typecheck/rust-tyty-call.cc 
b/gcc/rust/typecheck/rust-tyty-call.cc
index 725b8414671f..5fea34de40a2 100644
--- a/gcc/rust/typecheck/rust-tyty-call.cc
+++ b/gcc/rust/typecheck/rust-tyty-call.cc
@@ -152,12 +152,11 @@ TypeCheckCallExpr::visit (FnType &type)
   if (i < type.num_params ())
{
  auto &fnparam = type.param_at (i);
- auto &fn_param_pattern = fnparam.get_pattern ();
  BaseType *param_ty = fnparam.get_type ();
  location_t param_locus
= fnparam.has_pattern ()
-   ? mappings.lookup_location (param_ty->get_ref ())
-   : fn_param_pattern.get_locus ();
+   ? fnparam.get_pattern ().get_locus ()
+   : mappings.lookup_location (param_ty->get_ref ());
 
  HirId coercion_side_id = argument->get_mappings ().get_hirid ();
  auto resolved_argument_type
@@ -375,12 +374,11 @@ TypeCheckMethodCallExpr::check (FnType &type)
   location_t arg_locus = argument.get_locus ();
 
   auto &fnparam = type.param_at (i);
-  HIR::Pattern &fn_param_pattern = fnparam.get_pattern ();
   BaseType *param_ty = fnparam.get_type ();
   location_t param_locus
= fnparam.has_pattern ()
-   ? mappings.lookup_location (param_ty->get_ref ())
-   : fn_param_pattern.get_locus ();
+   ? fnparam.get_pattern ().get_locus ()
+   : mappings.lookup_location (param_ty->get_ref ());
 
   auto argument_expr_tyty = argument.get_argument_type ();
   HirId coercion_side_id = argument.get_mappings ().get_hirid ();


[gcc r15-8382] rust: negative polarity removes restrictions on validation of impl blocks

2025-03-26 Thread Arthur Cohen via Gcc-cvs
https://gcc.gnu.org/g:1e387486fbe93769221d905aae2b896ae0cb1cd3

commit r15-8382-g1e387486fbe93769221d905aae2b896ae0cb1cd3
Author: Philip Herron 
Date:   Fri Sep 20 17:49:36 2024 +0100

rust: negative polarity removes restrictions on validation of impl blocks

Negative polarity means we can just ignore if any trait items are not
implemented.

Fxies #3030

gcc/rust/ChangeLog:

* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): the polarity 
was reversed
* typecheck/rust-hir-type-check-item.cc: check the polarity

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-3030.rs: New test.

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/hir/rust-ast-lower-item.cc|  4 ++--
 gcc/rust/typecheck/rust-hir-type-check-item.cc |  3 ++-
 gcc/testsuite/rust/compile/issue-3030.rs   | 16 
 gcc/testsuite/rust/compile/nr2/exclude |  1 +
 4 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-item.cc 
b/gcc/rust/hir/rust-ast-lower-item.cc
index 0ef4f357c8ed..171737ab0293 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -690,8 +690,8 @@ ASTLoweringItem::visit (AST::TraitImpl &impl_block)
 }
 
   BoundPolarity polarity = impl_block.is_exclam ()
-? BoundPolarity::RegularBound
-: BoundPolarity::NegativeBound;
+? BoundPolarity::NegativeBound
+: BoundPolarity::RegularBound;
   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
 mapping, std::move (impl_items), std::move (generic_params),
 std::unique_ptr (impl_type),
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc 
b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 68e206924bbd..d707e3458f1d 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -734,7 +734,8 @@ TypeCheckItem::validate_trait_impl_block (
   bool impl_block_missing_trait_items
 = !specified_bound.is_error ()
   && trait_reference->size () != trait_item_refs.size ();
-  if (impl_block_missing_trait_items)
+  if (impl_block_missing_trait_items
+  && impl_block.get_polarity () == BoundPolarity::RegularBound)
 {
   // filter the missing impl_items
   std::vector>
diff --git a/gcc/testsuite/rust/compile/issue-3030.rs 
b/gcc/testsuite/rust/compile/issue-3030.rs
new file mode 100644
index ..0a1866d9a6b2
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3030.rs
@@ -0,0 +1,16 @@
+#![feature(negative_impls)]
+
+#[lang = "sized"]
+pub trait Sized {}
+
+pub trait Deref {}
+
+pub trait DerefMut: Deref {
+type Target;
+
+/// Mutably dereferences the value.
+#[stable(feature = "rust1", since = "1.0.0")]
+fn deref_mut(&mut self) -> &mut Self::Target;
+}
+
+impl !DerefMut for &T {}
diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index ca07ed6ecd2b..3251921acd45 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -291,3 +291,4 @@ unknown-associated-item.rs
 box_syntax_feature_gate.rs
 dropck_eyepatch_feature_gate.rs
 inline_asm_parse_output_operand.rs
+issue-3030.rs
\ No newline at end of file


[gcc r15-8926] i386: Fix up pr55583.c testcase [PR119465]

2025-03-26 Thread Jakub Jelinek via Gcc-cvs
https://gcc.gnu.org/g:7b7c0fe37332290fad9b31bf3ae05c28375e0a76

commit r15-8926-g7b7c0fe37332290fad9b31bf3ae05c28375e0a76
Author: Jakub Jelinek 
Date:   Wed Mar 26 12:19:14 2025 +0100

i386: Fix up pr55583.c testcase [PR119465]

In r15-4289 H.J. fixed up the pr55583.c testcase to use unsigned long long
or long long instead of unsigned long or long.  That change looks correct to
me because the
void test64r () { b = ((u64)b >> n) | (a << (64 - n)); }
etc. functions otherwise aren't really 64-bit rotates, but something that
triggers UB all the time (at least one of the shifts is out of bounds).
I assume that change fixed the FAILs on -mx32, but it caused
FAIL: gcc.target/i386/pr55583.c scan-assembler-times (?n)shldl?[t 
]*\$2 1
FAIL: gcc.target/i386/pr55583.c scan-assembler-times (?n)shrdl?[t 
]*\$2 2
regression on i686-linux (but just for -m32 without defaulting to SSE2 or
what).  The difference is that for say -m32 -march=x86-64 the stv pass
handles some of the rotates in SSE and so we get different sh[rl]dl
instruction counts from the case when SSE isn't enabled and stv pass isn't
done.

The following patch fixes that by disabling SSE for ia32 and always testing
for the same number of instructions.

Tested with all of
make check-gcc 
RUNTESTFLAGS='--target_board=unix\{-m32/-march=x86-64,-m32/-march=i686,-mx32,-m64\}
 i386.exp=pr55583.c'

2025-03-26  Jakub Jelinek  

PR target/55583
PR target/119465
* gcc.target/i386/pr55583.c: Add -mno-sse -mno-mmx to
dg-additional-options.  Expect 4 shrdl and 2 shldl instructions on
ia32.

Diff:
---
 gcc/testsuite/gcc.target/i386/pr55583.c | 7 +++
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.target/i386/pr55583.c 
b/gcc/testsuite/gcc.target/i386/pr55583.c
index ea6a2d54c495..87734518417d 100644
--- a/gcc/testsuite/gcc.target/i386/pr55583.c
+++ b/gcc/testsuite/gcc.target/i386/pr55583.c
@@ -1,9 +1,8 @@
 /* { dg-do compile  } */
 /* { dg-options "-O2 -Wno-shift-count-overflow" } */
-/* { dg-final { scan-assembler-times {(?n)shrd[ql]?[\t ]*\$2} 4 { target { ! 
ia32 } } } } */
-/* { dg-final { scan-assembler-times {(?n)shrdl?[\t ]*\$2} 2 { target ia32 } } 
} */
-/* { dg-final { scan-assembler-times {(?n)shldl?[\t ]*\$2} 1 { target ia32 } } 
} */
-/* { dg-final { scan-assembler-times {(?n)shld[ql]?[\t ]*\$2} 2 { target { ! 
ia32 } } } } */
+/* { dg-additional-options "-mno-sse -mno-mmx" { target ia32 } } */
+/* { dg-final { scan-assembler-times {(?n)shrd[ql]?[\t ]*\$2} 4 } } */
+/* { dg-final { scan-assembler-times {(?n)shld[ql]?[\t ]*\$2} 2 } } */
 
 typedef unsigned long long u64;
 typedef unsigned int   u32;


[gcc r15-8924] testsuite: i386: Require dfp support in gcc.target/i386/pr117946.c etc.

2025-03-26 Thread Rainer Orth via Gcc-cvs
https://gcc.gnu.org/g:2b4122c4165e23365dfcacb7df3b59900fa8e644

commit r15-8924-g2b4122c4165e23365dfcacb7df3b59900fa8e644
Author: Rainer Orth 
Date:   Wed Mar 26 11:46:57 2025 +0100

testsuite: i386: Require dfp support in gcc.target/i386/pr117946.c etc.

Two tests FAIL on 64-bit Solaris/x86:

FAIL: gcc.target/i386/pr117946.c (test for excess errors)
FAIL: gcc.target/i386/pr118017.c (test for excess errors)

The failure mode is the same in both cases:

Excess errors:
/vol/gcc/src/hg/master/local/gcc/testsuite/gcc.target/i386/pr117946.c:4:47: 
error: decimal floating-point not supported for this target

Excess errors:
/vol/gcc/src/hg/master/local/gcc/testsuite/gcc.target/i386/pr118017.c:6:47: 
error: decimal floating-point not supported for this target

Fixed by requiring dfp support.

Tested on i386-pc-solaris2.11 and x86_64-pc-linux-gnu.

2025-03-25  Rainer Orth  

gcc/testsuite:
* gcc.target/i386/pr117946.c: Require dfp support.
* gcc.target/i386/pr118017.c: Likewise.  Use
dg-require-effective-target for both this and int128.

Diff:
---
 gcc/testsuite/gcc.target/i386/pr117946.c | 1 +
 gcc/testsuite/gcc.target/i386/pr118017.c | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/gcc/testsuite/gcc.target/i386/pr117946.c 
b/gcc/testsuite/gcc.target/i386/pr117946.c
index c4bf8254967c..b46921cedaa2 100644
--- a/gcc/testsuite/gcc.target/i386/pr117946.c
+++ b/gcc/testsuite/gcc.target/i386/pr117946.c
@@ -1,4 +1,5 @@
 /* { dg-do compile  { target { ! ia32 } } } */
+/* { dg-require-effective-target dfp } */
 /* { dg-options "-O -favoid-store-forwarding -mavx10.1 -mprefer-avx128 
--param=store-forwarding-max-distance=128 -Wno-psabi" } */
 /* { dg-warning "'-mavx10.1' is aliased to 512 bit since GCC14.3 and GCC15.1 
while '-mavx10.1-256' and '-mavx10.1-512' will be deprecated in GCC 16 due to 
all machines 512 bit vector size supported" "" { target *-*-* } 0 } */
 typedef __attribute__((__vector_size__ (64))) _Decimal32 V;
diff --git a/gcc/testsuite/gcc.target/i386/pr118017.c 
b/gcc/testsuite/gcc.target/i386/pr118017.c
index 28797a0ad73f..831ec6efb645 100644
--- a/gcc/testsuite/gcc.target/i386/pr118017.c
+++ b/gcc/testsuite/gcc.target/i386/pr118017.c
@@ -1,5 +1,7 @@
 /* PR target/118017 */
-/* { dg-do compile { target int128 } } */
+/* { dg-do compile } */
+/* { dg-require-effective-target int128 } */
+/* { dg-require-effective-target dfp } */
 /* { dg-options "-Og -frounding-math -mno-80387 -mno-mmx -Wno-psabi" } */
 
 typedef __attribute__((__vector_size__ (64))) _Float128 F;


[gcc/devel/rust/master] gccrs: disable macos github workflow

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:b5c354d038f800695a8d730c56c4a4f744134adb

commit b5c354d038f800695a8d730c56c4a4f744134adb
Author: Philip Herron 
Date:   Thu Dec 19 13:35:09 2024 +

gccrs: disable macos github workflow

Its broken at the moment.

ChangeLog:

* .github/workflows/ccpp.yml: comment it out

Signed-off-by: Philip Herron 

Diff:
---
 .github/workflows/ccpp.yml | 116 ++---
 1 file changed, 58 insertions(+), 58 deletions(-)

diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml
index dfb090617f5e..4e9a629fb027 100644
--- a/.github/workflows/ccpp.yml
+++ b/.github/workflows/ccpp.yml
@@ -367,64 +367,64 @@ jobs:
   exit 0; \
 fi
 
-  build-and-check-clang-macos:
-
-env:
-  # Force CC/CXX to be explicitly clang to make it clear which compiler is 
used
-  CC: clang
-  CXX: clang++
-
-runs-on: macos-13
-
-steps:
-- uses: actions/checkout@v4
-
-- name: Install Deps
-  run: |
-  brew install dejagnu mpfr libmpc gmp;
-  # install Rust directly using rustup
-  curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- 
-y --default-toolchain=1.72.0;
-
-- name: Make Source Read-Only
-  run: chmod -R a-w ./*
-
-- name: Configure
-  run: |
-   mkdir -p gccrs-build;
-   cd gccrs-build;
-   ../configure \
-   --enable-languages=rust \
-   --disable-bootstrap \
-   --enable-multilib \
-   --with-native-system-header-dir=/usr/include \
-   --with-sysroot=$(xcrun --show-sdk-path)
-
-- name: Build
-  shell: bash
-  run: |
-   cd gccrs-build; \
-   make -j $(sysctl -n hw.ncpu) 2>&1 | tee log
-
-- name: Run Tests
-  run: |
-   cd gccrs-build; \
-   make check-rust
-- name: Archive check-rust results
-  uses: actions/upload-artifact@v3
-  with:
-name: check-rust-logs-macos
-path: |
-  gccrs-build/gcc/testsuite/rust/
-- name: Check regressions
-  run: |
-   cd gccrs-build; \
-   if grep -e "unexpected" -e "unresolved" -e "ERROR:" 
gcc/testsuite/rust/rust.sum;\
-   then \
-  echo "::error title=Regression test failed::some tests are not 
correct"; \
-  exit 1; \
-else \
-  exit 0; \
-fi
+  # build-and-check-clang-macos:
+
+  #   env:
+  # # Force CC/CXX to be explicitly clang to make it clear which compiler 
is used
+  # CC: clang
+  # CXX: clang++
+
+  #   runs-on: macos-13
+
+  #   steps:
+  #   - uses: actions/checkout@v4
+
+  #   - name: Install Deps
+  # run: |
+  # brew install dejagnu mpfr libmpc gmp;
+  # # install Rust directly using rustup
+  # curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s 
-- -y --default-toolchain=1.72.0;
+
+  #   - name: Make Source Read-Only
+  # run: chmod -R a-w ./*
+
+  #   - name: Configure
+  # run: |
+  #  mkdir -p gccrs-build;
+  #  cd gccrs-build;
+  #  ../configure \
+  #  --enable-languages=rust \
+  #  --disable-bootstrap \
+  #  --enable-multilib \
+  #  --with-native-system-header-dir=/usr/include \
+  #  --with-sysroot=$(xcrun --show-sdk-path)
+
+  #   - name: Build
+  # shell: bash
+  # run: |
+  #  cd gccrs-build; \
+  #  make -j $(sysctl -n hw.ncpu) 2>&1 | tee log
+
+  #   - name: Run Tests
+  # run: |
+  #  cd gccrs-build; \
+  #  make check-rust
+  #   - name: Archive check-rust results
+  # uses: actions/upload-artifact@v3
+  # with:
+  #   name: check-rust-logs-macos
+  #   path: |
+  # gccrs-build/gcc/testsuite/rust/
+  #   - name: Check regressions
+  # run: |
+  #  cd gccrs-build; \
+  #  if grep -e "unexpected" -e "unresolved" -e "ERROR:" 
gcc/testsuite/rust/rust.sum;\
+  #  then \
+  # echo "::error title=Regression test failed::some tests are not 
correct"; \
+  # exit 1; \
+  #   else \
+  # exit 0; \
+  #   fi
 
   build-and-check-asan:


[gcc/devel/rust/master] gccrs: implement the TuplePattern and use it for function patterns

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:daa2977f3d52968506a396523f47ab52e0e3a90a

commit daa2977f3d52968506a396523f47ab52e0e3a90a
Author: Philip Herron 
Date:   Fri Dec 13 15:51:55 2024 +

gccrs: implement the TuplePattern and use it for function patterns

In order to handle the tuple pattern of: fn test ((x _) : (i32, i32)) -> 
i32 { x }
we need to recognize that ABI wise this function still takes a tuple as the 
parameter
to this function its just how we can address the "pattern" of the tuple 
changes.

So reall if this was C it would look like:

  void test (struct tuple_type __prameter)
  {
return __parameter.0
  }

The code here reuses our existing pattern code so that we generate these 
implicit
bindings of the paramter with a field access so any time x is referenced 
it's really
just emplacing __parameter.0 for the field access into the struct which is 
a tuple.

Fixes Rust-GCC#2847

gcc/rust/ChangeLog:

* backend/rust-compile-fnparam.cc (CompileFnParam::visit): compile 
tuple patterns
(CompileSelfParam::compile): update return type
(CompileFnParam::create_tmp_param_var): return Bvariable not tree 
to stop ICE
* backend/rust-compile-fnparam.h: update prototype
* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): 
implement TuplePattern
* backend/rust-compile-pattern.h: update prototype

gcc/testsuite/ChangeLog:

* rust/compile/issue-2847.rs: New test.

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/backend/rust-compile-fnparam.cc | 31 
 gcc/rust/backend/rust-compile-fnparam.h  |  4 +-
 gcc/rust/backend/rust-compile-pattern.cc | 86 
 gcc/rust/backend/rust-compile-pattern.h  |  2 +-
 gcc/testsuite/rust/compile/issue-2847.rs |  8 +++
 5 files changed, 117 insertions(+), 14 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-fnparam.cc 
b/gcc/rust/backend/rust-compile-fnparam.cc
index f73e23359252..bb3e06ce6110 100644
--- a/gcc/rust/backend/rust-compile-fnparam.cc
+++ b/gcc/rust/backend/rust-compile-fnparam.cc
@@ -68,25 +68,36 @@ CompileFnParam::visit (HIR::WildcardPattern &pattern)
   compiled_param = Backend::parameter_variable (fndecl, "_", decl_type, locus);
 }
 
+void
+CompileFnParam::visit (HIR::TuplePattern &pattern)
+{
+  compiled_param = create_tmp_param_var (decl_type);
+  CompilePatternBindings::Compile (
+pattern, Backend::var_expression (compiled_param, locus), ctx);
+}
+
 void
 CompileFnParam::visit (HIR::StructPattern &pattern)
 {
-  tree tmp_param_var = create_tmp_param_var (decl_type);
-  CompilePatternBindings::Compile (pattern, tmp_param_var, ctx);
+  compiled_param = create_tmp_param_var (decl_type);
+  CompilePatternBindings::Compile (
+pattern, Backend::var_expression (compiled_param, locus), ctx);
 }
 
 void
 CompileFnParam::visit (HIR::TupleStructPattern &pattern)
 {
-  tree tmp_param_var = create_tmp_param_var (decl_type);
-  CompilePatternBindings::Compile (pattern, tmp_param_var, ctx);
+  compiled_param = create_tmp_param_var (decl_type);
+  CompilePatternBindings::Compile (
+pattern, Backend::var_expression (compiled_param, locus), ctx);
 }
 
 void
 CompileFnParam::visit (HIR::ReferencePattern &pattern)
 {
-  tree tmp_param_var = create_tmp_param_var (decl_type);
-  CompilePatternBindings::Compile (pattern, tmp_param_var, ctx);
+  compiled_param = create_tmp_param_var (decl_type);
+  CompilePatternBindings::Compile (
+pattern, Backend::var_expression (compiled_param, locus), ctx);
 }
 
 Bvariable *
@@ -102,7 +113,7 @@ CompileSelfParam::compile (Context *ctx, tree fndecl, 
HIR::SelfParam &self,
   return Backend::parameter_variable (fndecl, "self", decl_type, locus);
 }
 
-tree
+Bvariable *
 CompileFnParam::create_tmp_param_var (tree decl_type)
 {
   // generate the anon param
@@ -110,10 +121,8 @@ CompileFnParam::create_tmp_param_var (tree decl_type)
   std::string cpp_str_identifier = std::string (IDENTIFIER_POINTER 
(tmp_ident));
 
   decl_type = Backend::immutable_type (decl_type);
-  compiled_param = Backend::parameter_variable (fndecl, cpp_str_identifier,
-   decl_type, locus);
-
-  return Backend::var_expression (compiled_param, locus);
+  return Backend::parameter_variable (fndecl, cpp_str_identifier, decl_type,
+ locus);
 }
 
 } // namespace Compile
diff --git a/gcc/rust/backend/rust-compile-fnparam.h 
b/gcc/rust/backend/rust-compile-fnparam.h
index c36f7914f346..fed01680c4b0 100644
--- a/gcc/rust/backend/rust-compile-fnparam.h
+++ b/gcc/rust/backend/rust-compile-fnparam.h
@@ -47,12 +47,12 @@ public:
   void visit (HIR::QualifiedPathInExpression &) override {}
   void visit (HIR::RangePattern &) override {}
   void visit (HIR::SlicePattern &) override {}
-  void visit (HIR::TuplePattern &) override {}
+  void visit (HIR::TuplePattern 

[gcc/devel/rust/master] ci: update warnings after C++14 change

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:55a9d8d451354bf4ab8ff8367796faacad41a527

commit 55a9d8d451354bf4ab8ff8367796faacad41a527
Author: Sam James 
Date:   Tue Dec 10 01:58:10 2024 +

ci: update warnings after C++14 change

ChangeLog:
* .github/glibcxx_ubuntu64b_log_expected_warnings: Update.
* .github/log_expected_warnings: Update.

Diff:
---
 .github/glibcxx_ubuntu64b_log_expected_warnings | 2 +-
 .github/log_expected_warnings   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/.github/glibcxx_ubuntu64b_log_expected_warnings 
b/.github/glibcxx_ubuntu64b_log_expected_warnings
index b1f400a065dc..214a64aadcae 100644
--- a/.github/glibcxx_ubuntu64b_log_expected_warnings
+++ b/.github/glibcxx_ubuntu64b_log_expected_warnings
@@ -128,5 +128,5 @@ gengtype-lex.cc:357:15: warning: this statement may fall 
through [-Wimplicit-fal
 gengtype-lex.cc:357:15: warning: this statement may fall through 
[-Wimplicit-fallthrough=]
 gengtype-lex.cc:357:15: warning: this statement may fall through 
[-Wimplicit-fallthrough=]
 gengtype-lex.cc:357:15: warning: this statement may fall through 
[-Wimplicit-fallthrough=]
-install.texi:2231: warning: `.' or `,' must follow @xref, not f
+install.texi:2230: warning: `.' or `,' must follow @xref, not f
 libtool: install: warning: remember to run `libtool --finish 
/usr/local/libexec/gcc/x86_64-pc-linux-gnu/14.0.1'
diff --git a/.github/log_expected_warnings b/.github/log_expected_warnings
index 989829d685f4..9829eb2709cf 100644
--- a/.github/log_expected_warnings
+++ b/.github/log_expected_warnings
@@ -150,5 +150,5 @@ gengtype-lex.cc:357:15: warning: this statement may fall 
through [-Wimplicit-fal
 gengtype-lex.cc:357:15: warning: this statement may fall through 
[-Wimplicit-fallthrough=]
 gengtype-lex.cc:357:15: warning: this statement may fall through 
[-Wimplicit-fallthrough=]
 gengtype-lex.cc:357:15: warning: this statement may fall through 
[-Wimplicit-fallthrough=]
-install.texi:2231: warning: `.' or `,' must follow @xref, not f
+install.texi:2230: warning: `.' or `,' must follow @xref, not f
 libtool: install: warning: remember to run `libtool --finish 
/usr/local/libexec/gcc/x86_64-pc-linux-gnu/14.0.1'


[gcc/devel/rust/master] gcc/rust/ChangeLog:

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:600fd806b8821ea24103ea0f31d666077245c6b7

commit 600fd806b8821ea24103ea0f31d666077245c6b7
Author: Om Swaroop Nayak <96killera...@gmail.com>
Date:   Wed Jan 1 09:02:02 2025 -0800

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (get_lang_item_attr): "removed 
checker fn"
* util/rust-attributes.cc (Attributes::is_lang_item): "added fn"
* util/rust-attributes.h: "added fn"

Signed-off-by: Om Swaroop Nayak <96killera...@gmail.com>

Diff:
---
 gcc/rust/ast/rust-collect-lang-items.cc | 7 +--
 gcc/rust/util/rust-attributes.cc| 8 
 gcc/rust/util/rust-attributes.h | 2 ++
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/ast/rust-collect-lang-items.cc 
b/gcc/rust/ast/rust-collect-lang-items.cc
index 50d134a429f5..ec6919dca14e 100644
--- a/gcc/rust/ast/rust-collect-lang-items.cc
+++ b/gcc/rust/ast/rust-collect-lang-items.cc
@@ -40,12 +40,7 @@ get_lang_item_attr (const T &maybe_lang_item)
  continue;
}
 
-  bool is_lang_item = str_path == Values::Attributes::LANG
- && attr.has_attr_input ()
- && attr.get_attr_input ().get_attr_input_type ()
-  == AST::AttrInput::AttrInputType::LITERAL;
-
-  if (is_lang_item)
+  if (Analysis::Attributes::is_lang_item (str_path, attr))
{
  auto &literal
= static_cast (attr.get_attr_input ());
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 45ebf8c65461..1a66be25ccfb 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -37,6 +37,14 @@ Attributes::is_known (const std::string &attribute_path)
 
   return !lookup.is_error ();
 }
+bool
+Attributes::is_lang_item (const std::string &attribute_path,
+ const AST::Attribute &attr)
+{
+  return ((attribute_path == Values::Attributes::LANG) && attr.has_attr_input 
()
+ && (attr.get_attr_input ().get_attr_input_type ()
+ == AST::AttrInput::AttrInputType::LITERAL));
+}
 
 using Attrs = Values::Attributes;
 
diff --git a/gcc/rust/util/rust-attributes.h b/gcc/rust/util/rust-attributes.h
index c341b3e0a5db..c0d1bed64455 100644
--- a/gcc/rust/util/rust-attributes.h
+++ b/gcc/rust/util/rust-attributes.h
@@ -29,6 +29,8 @@ class Attributes
 {
 public:
   static bool is_known (const std::string &attribute_path);
+  static bool is_lang_item (const std::string &attribute_path,
+   const AST::Attribute &attr);
 };
 
 enum CompilerPass


[gcc/devel/rust/master] gccrs: cleanup our enum type layout to be closer to rustc

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:806166db9db83bee816d169a13ab9992de9d66a0

commit 806166db9db83bee816d169a13ab9992de9d66a0
Author: Philip Herron 
Date:   Tue Jan 7 18:15:37 2025 +

gccrs: cleanup our enum type layout to be closer to rustc

This changes our enum type layout so for example:

  enum Foo {
  A,
  B,
  C(char),
  D { x: i32, y: i32 },
  }

Used to get layed out like this in gccrs:

  union {
struct A { int RUST$ENUM$DISR; };
struct B { int RUST$ENUM$DISR; };
struct C { int RUST$ENUM$DISR; char __0; };
struct D { int RUST$ENUM$DISR; i64 x; i64 y; };
  }

This has some issues notably with the constexpr because this is just a
giant union it means its not simple to constify what enum variant we are
looking at because the discriminant is a mess.

This now gets layed out as:

  struct {
 int RUST$ENUM$DISR;
 union {
 struct A { };
 struct B { };
 struct C { char __0; };
 struct D { i64 x; i64 y; };
 } payload;
  }

This layout is much cleaner and allows for our constexpr to work properly.

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::visit): new layout
* backend/rust-compile-pattern.cc (CompilePatternCheckExpr::visit): 
likewise
(CompilePatternBindings::visit): likewise
* backend/rust-compile-resolve-path.cc: likewise
* backend/rust-compile-type.cc (TyTyResolveCompile::visit): 
implement new layout
* rust-gcc.cc (constructor_expression): get rid of useless assert

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc | 74 ---
 gcc/rust/backend/rust-compile-pattern.cc  | 64 +++
 gcc/rust/backend/rust-compile-resolve-path.cc |  5 +-
 gcc/rust/backend/rust-compile-type.cc | 62 +-
 gcc/rust/rust-gcc.cc  |  2 +-
 5 files changed, 128 insertions(+), 79 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 46b9b0adccfd..6a0db31efe8a 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -558,24 +558,32 @@ CompileExpr::visit (HIR::StructExprStructFields 
&struct_expr)
}
 }
 
-  // the constructor depends on whether this is actually an enum or not if
-  // its an enum we need to setup the discriminator
-  std::vector ctor_arguments;
-  if (adt->is_enum ())
+  if (!adt->is_enum ())
 {
-  HIR::Expr &discrim_expr = variant->get_discriminant ();
-  tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
-  tree folded_discrim_expr = fold_expr (discrim_expr_node);
-  tree qualifier = folded_discrim_expr;
-
-  ctor_arguments.push_back (qualifier);
+  translated
+   = Backend::constructor_expression (compiled_adt_type, adt->is_enum (),
+  arguments, union_disriminator,
+  struct_expr.get_locus ());
+  return;
 }
-  for (auto &arg : arguments)
-ctor_arguments.push_back (arg);
+
+  HIR::Expr &discrim_expr = variant->get_discriminant ();
+  tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
+  tree folded_discrim_expr = fold_expr (discrim_expr_node);
+  tree qualifier = folded_discrim_expr;
+
+  tree enum_root_files = TYPE_FIELDS (compiled_adt_type);
+  tree payload_root = DECL_CHAIN (enum_root_files);
+
+  tree payload = Backend::constructor_expression (TREE_TYPE (payload_root),
+ adt->is_enum (), arguments,
+ union_disriminator,
+ struct_expr.get_locus ());
+
+  std::vector ctor_arguments = {qualifier, payload};
 
   translated
-= Backend::constructor_expression (compiled_adt_type, adt->is_enum (),
-  ctor_arguments, union_disriminator,
+= Backend::constructor_expression (compiled_adt_type, 0, ctor_arguments, 
-1,
   struct_expr.get_locus ());
 }
 
@@ -1247,26 +1255,34 @@ CompileExpr::visit (HIR::CallExpr &expr)
  arguments.push_back (rvalue);
}
 
-  // the constructor depends on whether this is actually an enum or not if
-  // its an enum we need to setup the discriminator
-  std::vector ctor_arguments;
-  if (adt->is_enum ())
+  if (!adt->is_enum ())
{
- HIR::Expr &discrim_expr = variant->get_discriminant ();
- tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
- tree folded_discrim_expr = fold_expr (discrim_expr_node);
- tree qualifier = folded_discrim_expr;
-
- ctor_arguments.push_

[gcc/devel/rust/master] Add ForeverStackStore

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:248633dd1e48da81121544f354cb7ba47fddc92f

commit 248633dd1e48da81121544f354cb7ba47fddc92f
Author: Owen Avery 
Date:   Thu Nov 14 19:57:42 2024 -0500

Add ForeverStackStore

ForeverStackStore is meant to partially unify the internal states of
per-namespace ForeverStack instances. This commit does not contain
modifications to ForeverStack which would allow it to rely on a
ForeverStackStore to store nodes, but a future commit should address
this.

gcc/rust/ChangeLog:

* Make-lang.in: Handle rust-forever-stack.cc.
* resolve/rust-forever-stack.h
(class ForeverStackStore): Add.
* resolve/rust-forever-stack.cc: New file, based on
rust-forever-stack.hxx.

Signed-off-by: Owen Avery 

Diff:
---
 gcc/rust/Make-lang.in  |   1 +
 gcc/rust/resolve/rust-forever-stack.cc | 318 +
 gcc/rust/resolve/rust-forever-stack.h  | 151 
 3 files changed, 470 insertions(+)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index fe82ab42ec87..782c155fc32f 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -146,6 +146,7 @@ GRS_OBJS = \
 rust/rust-ast-resolve-path.o \
 rust/rust-ast-resolve-stmt.o \
 rust/rust-ast-resolve-struct-expr-field.o \
+rust/rust-forever-stack.o \
 rust/rust-hir-type-check.o \
 rust/rust-privacy-check.o \
 rust/rust-privacy-ctx.o \
diff --git a/gcc/rust/resolve/rust-forever-stack.cc 
b/gcc/rust/resolve/rust-forever-stack.cc
new file mode 100644
index ..725ae0ea0188
--- /dev/null
+++ b/gcc/rust/resolve/rust-forever-stack.cc
@@ -0,0 +1,318 @@
+// Copyright (C) 2024 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// .
+
+#include "expected.h"
+#include "rust-ast.h"
+#include "rust-diagnostics.h"
+#include "rust-forever-stack.h"
+#include "rust-rib.h"
+#include "optional.h"
+
+namespace Rust {
+namespace Resolver2_0 {
+
+bool
+ForeverStackStore::Node::is_root () const
+{
+  return !parent.has_value ();
+}
+
+bool
+ForeverStackStore::Node::is_leaf () const
+{
+  return children.empty ();
+}
+
+NodeId
+ForeverStackStore::Node::get_id () const
+{
+  return id;
+}
+
+ForeverStackStore::Node &
+ForeverStackStore::Node::insert_child (NodeId id, tl::optional 
path,
+  Rib::Kind kind)
+{
+  auto res = children.insert ({Link (id, path), Node (kind, id, *this)});
+
+  rust_debug ("inserting link: Link(%d [%s]): existed? %s", id,
+ path.has_value () ? path.value ().as_string ().c_str ()
+   : "",
+ !res.second ? "yes" : "no");
+
+  // sanity check on rib kind
+  // pick the value rib, since all ribs should have the same kind anyways
+  rust_assert (res.second || res.first->second.value_rib.kind == kind);
+
+  // verify, if we're using an existing node, our paths don't contradict
+  if (!res.second && path.has_value ())
+{
+  auto other_path = res.first->first.path;
+  rust_assert (!other_path.has_value ()
+  || other_path.value ().as_string ()
+   == path.value ().as_string ());
+}
+
+  return res.first->second;
+}
+
+tl::optional
+ForeverStackStore::Node::get_child (const Identifier &path)
+{
+  for (auto &ent : children)
+{
+  if (ent.first.path.has_value ()
+ && ent.first.path->as_string () == path.as_string ())
+   return ent.second;
+}
+  return tl::nullopt;
+}
+
+tl::optional
+ForeverStackStore::Node::get_child (const Identifier &path) const
+{
+  for (auto &ent : children)
+{
+  if (ent.first.path.has_value ()
+ && ent.first.path->as_string () == path.as_string ())
+   return ent.second;
+}
+  return tl::nullopt;
+}
+
+tl::optional
+ForeverStackStore::Node::get_parent ()
+{
+  return parent;
+}
+
+tl::optional
+ForeverStackStore::Node::get_parent () const
+{
+  if (parent)
+return *parent;
+  return tl::nullopt;
+}
+
+tl::optional
+ForeverStackStore::Node::get_parent_path () const
+{
+  if (parent.has_value ())
+for (auto &ent : parent->children)
+  if (ent.first.id == id && ent.first.path.has_value ())
+   return ent.first.path.value ();
+  return tl::nullopt;
+}
+
+Rib &
+ForeverStackStore::Node::get_rib (Namespace ns)
+{
+  sw

[gcc/devel/rust/master] nr2.0: Resolve Self inside impl blocks

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:1eaf085607f39111370e2c485ca819da7e14dfd1

commit 1eaf085607f39111370e2c485ca819da7e14dfd1
Author: Owen Avery 
Date:   Tue Jan 7 14:03:13 2025 -0500

nr2.0: Resolve Self inside impl blocks

gcc/rust/ChangeLog:

* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Insert a definition for Self when visiting
InherentImpl and TraitImpl instances.
* resolve/rust-toplevel-name-resolver-2.0.h
(TopLevel::visit): Add visitors for InherentImpl and TraitImpl.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery 

Diff:
---
 .../resolve/rust-toplevel-name-resolver-2.0.cc | 26 ++
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h |  2 ++
 gcc/testsuite/rust/compile/nr2/exclude |  3 ---
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index a88adabb094c..61e9ea4a928c 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -103,6 +103,32 @@ TopLevel::visit (AST::Trait &trait)
   DefaultResolver::visit (trait);
 }
 
+void
+TopLevel::visit (AST::InherentImpl &impl)
+{
+  auto inner_fn = [this, &impl] () {
+insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
+impl.get_type (), Namespace::Types);
+
+AST::DefaultASTVisitor::visit (impl);
+  };
+
+  ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
+}
+
+void
+TopLevel::visit (AST::TraitImpl &impl)
+{
+  auto inner_fn = [this, &impl] () {
+insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
+impl.get_type (), Namespace::Types);
+
+AST::DefaultASTVisitor::visit (impl);
+  };
+
+  ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
+}
+
 void
 TopLevel::visit (AST::TraitItemType &trait_item)
 {
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
index 4b8a51cff9cc..ff86cf48ba8c 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
@@ -148,6 +148,8 @@ private:
 
   void visit (AST::Module &module) override;
   void visit (AST::Trait &trait) override;
+  void visit (AST::InherentImpl &impl) override;
+  void visit (AST::TraitImpl &impl) override;
   void visit (AST::TraitItemType &trait_item) override;
   void visit (AST::MacroRulesDefinition ¯o) override;
   void visit (AST::Function &function) override;
diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index af7d105debc3..9b1ee7ceaf99 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -39,13 +39,11 @@ generics6.rs
 generics9.rs
 if_let_expr.rs
 issue-1019.rs
-issue-1031.rs
 issue-1034.rs
 issue-1129-2.rs
 issue-1130.rs
 issue-1173.rs
 issue-1272.rs
-issue-1289.rs
 issue-1447.rs
 issue-1483.rs
 issue-1725-1.rs
@@ -85,7 +83,6 @@ issue-855.rs
 issue-925.rs
 iterators1.rs
 lookup_err1.rs
-macros/mbe/macro-issue1233.rs
 macros/mbe/macro-issue1400.rs
 macros/mbe/macro13.rs
 macros/mbe/macro15.rs


[gcc/devel/rust/master] gccrs: fix ICE with hir dump on closure

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:02601c74706bbe645ee31e2e1a2cef0f168a73e2

commit 02601c74706bbe645ee31e2e1a2cef0f168a73e2
Author: Philip Herron 
Date:   Tue Jan 7 18:43:32 2025 +

gccrs: fix ICE with hir dump on closure

Return type and parameter types are optional on closures.

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::visit): add null guard

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/hir/rust-hir-dump.cc | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index 112e129ea4ab..b1ada75e3ffb 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -1244,13 +1244,17 @@ Dump::visit (ClosureExpr &e)
  auto oa = param.get_outer_attrs ();
  do_outer_attrs (oa);
  visit_field ("pattern", param.get_pattern ());
- visit_field ("type", param.get_type ());
+
+ if (param.has_type_given ())
+   visit_field ("type", param.get_type ());
+
  end ("ClosureParam");
}
   end_field ("params");
 }
 
-  visit_field ("return_type", e.get_return_type ());
+  if (e.has_return_type ())
+visit_field ("return_type", e.get_return_type ());
 
   visit_field ("expr", e.get_expr ());
   end ("ClosureExpr");


[gcc/devel/rust/master] collect-lang-items: Display attribute upon error finding it

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:955c4f7e587c082f0d364e4305c78b35822bd6ab

commit 955c4f7e587c082f0d364e4305c78b35822bd6ab
Author: Arthur Cohen 
Date:   Fri Jan 3 15:45:39 2025 +

collect-lang-items: Display attribute upon error finding it

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (get_lang_item_attr): Show unknown 
attribute upon error.

Diff:
---
 gcc/rust/ast/rust-collect-lang-items.cc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/ast/rust-collect-lang-items.cc 
b/gcc/rust/ast/rust-collect-lang-items.cc
index 50d134a429f5..168123ee56eb 100644
--- a/gcc/rust/ast/rust-collect-lang-items.cc
+++ b/gcc/rust/ast/rust-collect-lang-items.cc
@@ -36,7 +36,8 @@ get_lang_item_attr (const T &maybe_lang_item)
   const auto &str_path = attr.get_path ().as_string ();
   if (!Analysis::Attributes::is_known (str_path))
{
- rust_error_at (attr.get_locus (), "unknown attribute");
+ rust_error_at (attr.get_locus (), "unknown attribute %qs",
+str_path.c_str ());
  continue;
}


[gcc/devel/rust/master] ast: Refactor how lang item paths are handled.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:432859bf783a7dc9fb81f267c5159c60435973cb

commit 432859bf783a7dc9fb81f267c5159c60435973cb
Author: Arthur Cohen 
Date:   Thu Dec 26 21:46:03 2024 +

ast: Refactor how lang item paths are handled.

Lang item typepaths were not handled properly, and required a complete 
overhaul.
All old classes that concerned lang item paths are now modified to use a 
simpler
version of `AST::LangItemPath`, which has been removed. TypePath segments 
can now
be lang items, as this is requied for having generic lang item paths such as
PhantomData.

gcc/rust/ChangeLog:

* ast/rust-path.h: Rework how lang item paths are represented.
* ast/rust-path.cc: Likewise.
* ast/rust-item.h: Likewise.
* ast/rust-ast.cc: Likewise.
* ast/rust-ast-collector.cc: Adapt to new lang item path system.
* ast/rust-ast-collector.h: Likewise.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise.
* ast/rust-ast-visitor.h: Likewise.
* expand/rust-derive-copy.cc: Likewise.
* expand/rust-derive.h: Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise.
* hir/rust-ast-lower-base.h: Likewise.
* hir/rust-ast-lower-type.cc (ASTLowerTypePath::translate): 
Likewise.
(ASTLowerTypePath::visit): Likewise.
* hir/rust-ast-lower-type.h: Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise.
* resolve/rust-ast-resolve-base.h: Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Likewise.
* resolve/rust-ast-resolve-type.h: Likewise.
* resolve/rust-ast-resolve-type.cc (ResolveRelativeTypePath::go): 
Likewise.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Likewise.
* resolve/rust-late-name-resolver-2.0.h: Likewise.
* hir/tree/rust-hir-path.cc (TypePathSegment::TypePathSegment): 
Likewise.
(TypePathSegmentGeneric::TypePathSegmentGeneric): Likewise.
* hir/tree/rust-hir-path.h: Likewise.
* typecheck/rust-hir-type-check-type.cc 
(TypeCheckType::resolve_root_path): Likewise.
* ast/rust-ast-builder.cc: Likewise.
* ast/rust-ast-builder.h: Likewise.

Diff:
---
 gcc/rust/ast/rust-ast-builder.cc|  20 +-
 gcc/rust/ast/rust-ast-builder.h |   4 -
 gcc/rust/ast/rust-ast-collector.cc  |  13 -
 gcc/rust/ast/rust-ast-collector.h   |   2 -
 gcc/rust/ast/rust-ast-visitor.cc|  11 -
 gcc/rust/ast/rust-ast-visitor.h |   4 -
 gcc/rust/ast/rust-ast.cc|   4 +-
 gcc/rust/ast/rust-item.h|  40 +--
 gcc/rust/ast/rust-path.cc   |  31 +-
 gcc/rust/ast/rust-path.h| 405 +---
 gcc/rust/expand/rust-derive-copy.cc |   4 +-
 gcc/rust/expand/rust-derive.h   |   2 -
 gcc/rust/hir/rust-ast-lower-base.cc |   6 -
 gcc/rust/hir/rust-ast-lower-base.h  |   2 -
 gcc/rust/hir/rust-ast-lower-type.cc |  75 +++--
 gcc/rust/hir/rust-ast-lower-type.h  |   6 +-
 gcc/rust/hir/tree/rust-hir-path.cc  |  21 +-
 gcc/rust/hir/tree/rust-hir-path.h   |  38 ++-
 gcc/rust/resolve/rust-ast-resolve-base.cc   |   8 -
 gcc/rust/resolve/rust-ast-resolve-base.h|   2 -
 gcc/rust/resolve/rust-ast-resolve-item.cc   |  27 +-
 gcc/rust/resolve/rust-ast-resolve-type.cc   |  79 +++--
 gcc/rust/resolve/rust-ast-resolve-type.h|  31 --
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc |  19 --
 gcc/rust/resolve/rust-late-name-resolver-2.0.h  |   1 -
 gcc/rust/typecheck/rust-hir-type-check-type.cc  |   2 +-
 26 files changed, 311 insertions(+), 546 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 90832e23cf27..d10b64e6e259 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -42,15 +42,6 @@ Builder::call (std::unique_ptr &&path,
 new CallExpr (std::move (path), std::move (args), {}, loc));
 }
 
-std::unique_ptr
-Builder::call (std::unique_ptr &&path,
-  std::vector> &&args) const
-{
-  return call (std::unique_ptr (
-new PathInExpression (std::move (path), {}, loc)),
-  std::move (args));
-}
-
 std::unique_ptr
 Builder::call (std::unique_ptr &&path, std::unique_ptr &&arg) const
 {
@@ -60,15 +51,6 @@ Builder::call (std::unique_ptr &&path, 
std::unique_ptr &&arg) const
   return call (std::move (path), std::move (args));
 }
 
-std::unique_ptr
-Builder::call (std::unique_ptr &&path, std::unique_ptr &&arg) const
-{
-  auto args = std::vector> ();
-  args.emplace_back (std::move (arg));
-
-  return call (std::move (path), std::move (arg

[gcc/devel/rust/master] lang-items: Collect struct lang items.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:0bde282a597356cc443d14aff17f9953d1bfda49

commit 0bde282a597356cc443d14aff17f9953d1bfda49
Author: Arthur Cohen 
Date:   Thu Dec 26 22:35:15 2024 +

lang-items: Collect struct lang items.

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (CollectLangItems::visit): New.
* ast/rust-collect-lang-items.h: New.

Diff:
---
 gcc/rust/ast/rust-collect-lang-items.cc | 9 +
 gcc/rust/ast/rust-collect-lang-items.h  | 1 +
 2 files changed, 10 insertions(+)

diff --git a/gcc/rust/ast/rust-collect-lang-items.cc 
b/gcc/rust/ast/rust-collect-lang-items.cc
index 168123ee56eb..11c3297d2a9d 100644
--- a/gcc/rust/ast/rust-collect-lang-items.cc
+++ b/gcc/rust/ast/rust-collect-lang-items.cc
@@ -23,6 +23,7 @@
 #include "rust-attribute-values.h"
 #include "rust-attributes.h"
 #include "rust-hir-map.h"
+#include "rust-item.h"
 
 namespace Rust {
 namespace AST {
@@ -91,5 +92,13 @@ CollectLangItems::visit (AST::Function &item)
   DefaultASTVisitor::visit (item);
 }
 
+void
+CollectLangItems::visit (AST::StructStruct &item)
+{
+  maybe_add_lang_item (item);
+
+  DefaultASTVisitor::visit (item);
+}
+
 } // namespace AST
 } // namespace Rust
diff --git a/gcc/rust/ast/rust-collect-lang-items.h 
b/gcc/rust/ast/rust-collect-lang-items.h
index 1d021b1d9c50..39cb4be31a0c 100644
--- a/gcc/rust/ast/rust-collect-lang-items.h
+++ b/gcc/rust/ast/rust-collect-lang-items.h
@@ -48,6 +48,7 @@ public:
   void visit (AST::Trait &item) override;
   void visit (AST::TraitItemType &item) override;
   void visit (AST::Function &item) override;
+  void visit (AST::StructStruct &item) override;
 
 private:
   template  void maybe_add_lang_item (const T &item);


[gcc/devel/rust/master] lang-item: Add LangItem::PrettyString

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:68f142a0e92cc451d92e0bd9a4ca75dc50a7ff8f

commit 68f142a0e92cc451d92e0bd9a4ca75dc50a7ff8f
Author: Arthur Cohen 
Date:   Thu Dec 26 22:45:12 2024 +

lang-item: Add LangItem::PrettyString

Which formats a lang item as it appears in source code.

gcc/rust/ChangeLog:

* util/rust-lang-item.cc (LangItem::PrettyString): New.
* util/rust-lang-item.h: New.

Diff:
---
 gcc/rust/util/rust-lang-item.cc | 6 ++
 gcc/rust/util/rust-lang-item.h  | 1 +
 2 files changed, 7 insertions(+)

diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index bd5a29da9ffe..674b18919d27 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -118,6 +118,12 @@ LangItem::ToString (LangItem::Kind type)
   return str.value ();
 }
 
+std::string
+LangItem::PrettyString (LangItem::Kind type)
+{
+  return "#[lang = \"" + LangItem::ToString (type) + "\"]";
+}
+
 LangItem::Kind
 LangItem::OperatorToLangItem (ArithmeticOrLogicalOperator op)
 {
diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h
index 35ee5c2a2c3a..f2e9d7036124 100644
--- a/gcc/rust/util/rust-lang-item.h
+++ b/gcc/rust/util/rust-lang-item.h
@@ -134,6 +134,7 @@ public:
 
   static tl::optional Parse (const std::string &item);
   static std::string ToString (Kind type);
+  static std::string PrettyString (Kind type);
   static Kind OperatorToLangItem (ArithmeticOrLogicalOperator op);
   static Kind
   CompoundAssignmentOperatorToLangItem (ArithmeticOrLogicalOperator op);


[gcc/devel/rust/master] gccrs: Add missing name resolution to static items in blocks

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:0a935444fbf9e99c7a3eff026e2a7ab910e77151

commit 0a935444fbf9e99c7a3eff026e2a7ab910e77151
Author: Philip Herron 
Date:   Mon Jan 13 11:51:51 2025 +

gccrs: Add missing name resolution to static items in blocks

We need to add name resolution and hir lowering for items as part of blocks
in order to typecheck and compile them correctly.

Fixes Rust-GCC#3350

gcc/rust/ChangeLog:

* hir/rust-ast-lower-stmt.cc (ASTLoweringStmt::visit): hir lowering
* hir/rust-ast-lower-stmt.h: likewise
* resolve/rust-ast-resolve-stmt.cc (ResolveStmt::visit): name 
resolution
* resolve/rust-ast-resolve-stmt.h: likewise

gcc/testsuite/ChangeLog:

* rust/compile/issue-3350.rs: New test.

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/hir/rust-ast-lower-stmt.cc   |  6 ++
 gcc/rust/hir/rust-ast-lower-stmt.h|  1 +
 gcc/rust/resolve/rust-ast-resolve-stmt.cc | 21 +
 gcc/rust/resolve/rust-ast-resolve-stmt.h  |  1 +
 gcc/testsuite/rust/compile/issue-3350.rs  | 10 ++
 5 files changed, 39 insertions(+)

diff --git a/gcc/rust/hir/rust-ast-lower-stmt.cc 
b/gcc/rust/hir/rust-ast-lower-stmt.cc
index 29da916cefc4..761a638a80e6 100644
--- a/gcc/rust/hir/rust-ast-lower-stmt.cc
+++ b/gcc/rust/hir/rust-ast-lower-stmt.cc
@@ -163,5 +163,11 @@ ASTLoweringStmt::visit (AST::TraitImpl &impl_block)
   translated = ASTLoweringItem::translate (impl_block);
 }
 
+void
+ASTLoweringStmt::visit (AST::StaticItem &var)
+{
+  translated = ASTLoweringItem::translate (var);
+}
+
 } // namespace HIR
 } // namespace Rust
diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h 
b/gcc/rust/hir/rust-ast-lower-stmt.h
index 111990d7aa88..ed395123859d 100644
--- a/gcc/rust/hir/rust-ast-lower-stmt.h
+++ b/gcc/rust/hir/rust-ast-lower-stmt.h
@@ -45,6 +45,7 @@ public:
   void visit (AST::Trait &trait) override;
   void visit (AST::InherentImpl &impl_block) override;
   void visit (AST::TraitImpl &impl_block) override;
+  void visit (AST::StaticItem &var) override;
 
 private:
   ASTLoweringStmt () : translated (nullptr), terminated (false) {}
diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.cc 
b/gcc/rust/resolve/rust-ast-resolve-stmt.cc
index 7b62d1f7f788..ecd197691948 100644
--- a/gcc/rust/resolve/rust-ast-resolve-stmt.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-stmt.cc
@@ -56,5 +56,26 @@ ResolveStmt::visit (AST::TraitImpl &impl_block)
   ResolveItem::go (impl_block, prefix, canonical_prefix);
 }
 
+void
+ResolveStmt::visit (AST::StaticItem &var)
+{
+  auto decl = CanonicalPath::new_seg (var.get_node_id (),
+ var.get_identifier ().as_string ());
+  auto path = decl;
+  auto cpath = canonical_prefix.append (decl);
+  mappings.insert_canonical_path (var.get_node_id (), cpath);
+
+  resolver->get_name_scope ().insert (
+path, var.get_node_id (), var.get_locus (), false, Rib::ItemType::Static,
+[&] (const CanonicalPath &, NodeId, location_t locus) -> void {
+  rich_location r (line_table, var.get_locus ());
+  r.add_range (locus);
+  rust_error_at (r, "redefined multiple times");
+});
+
+  ResolveType::go (var.get_type ());
+  ResolveExpr::go (var.get_expr (), path, cpath);
+}
+
 } // namespace Resolver
 } // namespace Rust
diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h 
b/gcc/rust/resolve/rust-ast-resolve-stmt.h
index 7118b70d7058..fd9880cc21d2 100644
--- a/gcc/rust/resolve/rust-ast-resolve-stmt.h
+++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h
@@ -388,6 +388,7 @@ public:
   void visit (AST::Trait &trait) override;
   void visit (AST::InherentImpl &impl_block) override;
   void visit (AST::TraitImpl &impl_block) override;
+  void visit (AST::StaticItem &var) override;
 
 private:
   ResolveStmt (const CanonicalPath &prefix,
diff --git a/gcc/testsuite/rust/compile/issue-3350.rs 
b/gcc/testsuite/rust/compile/issue-3350.rs
new file mode 100644
index ..8880659afe68
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3350.rs
@@ -0,0 +1,10 @@
+static FOO: i32 = 0;
+
+pub fn bar() -> i32 {
+FOO
+}
+
+pub fn baz() -> i32 {
+static QUX: i32 = 0;
+QUX
+}


[gcc/devel/rust/master] tychk: resolve lang item type paths properly

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:e7011f62930b662426ceb53a4cc40a64268163b4

commit e7011f62930b662426ceb53a4cc40a64268163b4
Author: Arthur Cohen 
Date:   Thu Jan 2 10:37:00 2025 +

tychk: resolve lang item type paths properly

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc 
(TypeCheckType::resolve_root_path): Adapt
code to handle lang item type paths.

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-type.cc | 26 +-
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc 
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 7e623e045468..e367c1b70ed8 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -379,6 +379,7 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, 
size_t *offset,
 {
   TyTy::BaseType *root_tyty = nullptr;
   *offset = 0;
+
   for (size_t i = 0; i < path.get_num_segments (); i++)
 {
   std::unique_ptr &seg = path.get_segments ().at (i);
@@ -390,18 +391,25 @@ TypeCheckType::resolve_root_path (HIR::TypePath &path, 
size_t *offset,
   // then lookup the reference_node_id
   NodeId ref_node_id = UNKNOWN_NODEID;
 
-  // FIXME: HACK: ARTHUR: Remove this
-  if (flag_name_resolution_2_0)
+  if (seg->is_lang_item ())
+   ref_node_id = Analysis::Mappings::get ().get_lang_item_node (
+ seg->get_lang_item ());
+  else
{
- auto nr_ctx
-   = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
+ // FIXME: HACK: ARTHUR: Remove this
+ if (flag_name_resolution_2_0)
+   {
+ auto nr_ctx = Resolver2_0::ImmutableNameResolutionContext::get ()
+ .resolver ();
 
- // assign the ref_node_id if we've found something
- nr_ctx.lookup (path.get_mappings ().get_nodeid ())
-   .map ([&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
+ // assign the ref_node_id if we've found something
+ nr_ctx.lookup (path.get_mappings ().get_nodeid ())
+   .map (
+ [&ref_node_id] (NodeId resolved) { ref_node_id = resolved; });
+   }
+ else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
+   resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
}
-  else if (!resolver->lookup_resolved_name (ast_node_id, &ref_node_id))
-   resolver->lookup_resolved_type (ast_node_id, &ref_node_id);
 
   // ref_node_id is the NodeId that the segments refers to.
   if (ref_node_id == UNKNOWN_NODEID)


[gcc/devel/rust/master] nr2.0: Early resolve pending eager macro invocations

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:c6b3dab560c3ed06eb74361848c98fbb18ad3004

commit c6b3dab560c3ed06eb74361848c98fbb18ad3004
Author: Owen Avery 
Date:   Fri Jan 10 23:55:29 2025 -0500

nr2.0: Early resolve pending eager macro invocations

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc
(Early::visit): Resolve the pending eager invocations inside
builtin macro invocations.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery 

Diff:
---
 gcc/rust/resolve/rust-early-name-resolver-2.0.cc | 4 
 gcc/testsuite/rust/compile/nr2/exclude   | 5 -
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
index 402af27c47be..ae02a17de3bc 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -238,6 +238,10 @@ Early::visit (AST::MacroInvocation &invoc)
 {
   auto path = invoc.get_invoc_data ().get_path ();
 
+  if (invoc.get_kind () == AST::MacroInvocation::InvocKind::Builtin)
+for (auto &pending_invoc : invoc.get_pending_eager_invocations ())
+  pending_invoc->accept_vis (*this);
+
   // When a macro is invoked by an unqualified identifier (not part of a
   // multi-part path), it is first looked up in textual scoping. If this does
   // not yield any results, then it is looked up in path-based scoping. If the
diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index 9b490c18bab4..0f482df2f005 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -1,11 +1,6 @@
 bounds1.rs
 break-rust2.rs
 break-rust3.rs
-macros/builtin/eager1.rs
-macros/builtin/eager2.rs
-macros/builtin/recurse2.rs
-macros/builtin/include3.rs
-macros/builtin/include4.rs
 canonical_paths1.rs
 cfg1.rs
 cfg3.rs


[gcc/devel/rust/master] testsuite: Fix missing handling of little endian.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:3474059707a939cf2fece418daef83e6f5bf1494

commit 3474059707a939cf2fece418daef83e6f5bf1494
Author: Arthur Cohen 
Date:   Thu Jan 18 17:24:01 2024 +0100

testsuite: Fix missing handling of little endian.

Some failures occur in the testsuite because we
did not account for the little-endian case.

gcc/testsuite/ChangeLog:

* rust/compile/issue-1446.rs: Add swap_bytes function.
* rust/compile/iterators1.rs: Remove unused {to, from}_le functions.

Diff:
---
 gcc/testsuite/rust/compile/issue-1446.rs | 10 +-
 gcc/testsuite/rust/compile/iterators1.rs | 18 --
 2 files changed, 9 insertions(+), 19 deletions(-)

diff --git a/gcc/testsuite/rust/compile/issue-1446.rs 
b/gcc/testsuite/rust/compile/issue-1446.rs
index 8bfa42b9dace..969ad380ee69 100644
--- a/gcc/testsuite/rust/compile/issue-1446.rs
+++ b/gcc/testsuite/rust/compile/issue-1446.rs
@@ -1,3 +1,11 @@
+// fake function
+pub fn swap_bytes(this: u32) -> u32 {
+(((this) & 0xff00) >> 24)
+| (((this) & 0x00ff) >> 8)
+| (((this) & 0xff00) << 8)
+| (((this) & 0x00ff) << 24)
+}
+
 pub fn to_le(this: u32) -> u32 {
 #[cfg(target_endian = "little")]
 {
@@ -5,6 +13,6 @@ pub fn to_le(this: u32) -> u32 {
 }
 #[cfg(not(target_endian = "little"))]
 {
-this.swap_bytes()
+swap_bytes(this)
 }
 }
diff --git a/gcc/testsuite/rust/compile/iterators1.rs 
b/gcc/testsuite/rust/compile/iterators1.rs
index 35fea5a04938..1141758b14a7 100644
--- a/gcc/testsuite/rust/compile/iterators1.rs
+++ b/gcc/testsuite/rust/compile/iterators1.rs
@@ -232,24 +232,6 @@ macro_rules! impl_uint {
 }
 }
 
-pub fn to_le(self) -> Self {
-#[cfg(target_endian = "little")]
-{
-self
-}
-}
-
-pub const fn from_le_bytes(bytes: [u8; 
mem::size_of::()]) -> Self {
-Self::from_le(Self::from_ne_bytes(bytes))
-}
-
-pub const fn from_le(x: Self) -> Self {
-#[cfg(target_endian = "little")]
-{
-x
-}
-}
-
 pub const fn from_ne_bytes(bytes: [u8; 
mem::size_of::()]) -> Self {
 unsafe { mem::transmute(bytes) }
 }


[gcc/devel/rust/master] ast: Fix warning about copy elision for moved expr

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:035990c57b58a1dc95ac2a36a17a15d1ae7255a2

commit 035990c57b58a1dc95ac2a36a17a15d1ae7255a2
Author: Arthur Cohen 
Date:   Thu Dec 26 21:31:21 2024 +

ast: Fix warning about copy elision for moved expr

gcc/rust/ChangeLog:

* ast/rust-ast.cc (BlockExpr::normalize_tail_expr): Remove 
overzealous
std::move

Diff:
---
 gcc/rust/ast/rust-ast.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index d9197e3db059..ce4254ac8d07 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -4270,7 +4270,7 @@ BlockExpr::normalize_tail_expr ()
 
  if (!stmt.is_semicolon_followed ())
{
- expr = std::move (stmt.take_expr ());
+ expr = stmt.take_expr ();
  statements.pop_back ();
}
}


[gcc/devel/rust/master] attributes: Add #[derive] as a built-in attribute

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:e530ccb311082eb2cde113f0873a62ce4aa82a5c

commit e530ccb311082eb2cde113f0873a62ce4aa82a5c
Author: Arthur Cohen 
Date:   Fri Jan 3 15:45:57 2025 +

attributes: Add #[derive] as a built-in attribute

gcc/rust/ChangeLog:

* util/rust-attribute-values.h: Declare new attribute value.
* util/rust-attributes.cc: Use it.

Diff:
---
 gcc/rust/util/rust-attribute-values.h | 1 +
 gcc/rust/util/rust-attributes.cc  | 1 +
 2 files changed, 2 insertions(+)

diff --git a/gcc/rust/util/rust-attribute-values.h 
b/gcc/rust/util/rust-attribute-values.h
index fa316b45a9e9..75dc9e141109 100644
--- a/gcc/rust/util/rust-attribute-values.h
+++ b/gcc/rust/util/rust-attribute-values.h
@@ -29,6 +29,7 @@ public:
   static constexpr auto &COLD = "cold";
   static constexpr auto &CFG = "cfg";
   static constexpr auto &CFG_ATTR = "cfg_attr";
+  static constexpr auto &DERIVE_ATTR = "derive";
   static constexpr auto &DEPRECATED = "deprecated";
   static constexpr auto &ALLOW = "allow";
   static constexpr auto &ALLOW_INTERNAL_UNSTABLE = "allow_internal_unstable";
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index 45ebf8c65461..079e17793db6 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -46,6 +46,7 @@ static const BuiltinAttrDefinition __definitions[]
  {Attrs::COLD, CODE_GENERATION},
  {Attrs::CFG, EXPANSION},
  {Attrs::CFG_ATTR, EXPANSION},
+ {Attrs::DERIVE_ATTR, EXPANSION},
  {Attrs::DEPRECATED, STATIC_ANALYSIS},
  {Attrs::ALLOW, STATIC_ANALYSIS},
  {Attrs::ALLOW_INTERNAL_UNSTABLE, STATIC_ANALYSIS},


[gcc/devel/rust/master] lang-item: Add Option::{None, Some}, Iterator::next, IntoIter::into_iter

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:699fdd454615c20baf43295c1d6727e5c1e98531

commit 699fdd454615c20baf43295c1d6727e5c1e98531
Author: Arthur Cohen 
Date:   Mon Dec 16 14:17:29 2024 +0100

lang-item: Add Option::{None, Some}, Iterator::next, IntoIter::into_iter

gcc/rust/ChangeLog:

* util/rust-lang-item.h: Add new lang items.
* util/rust-lang-item.cc: Likewise.

Diff:
---
 gcc/rust/util/rust-lang-item.cc | 6 ++
 gcc/rust/util/rust-lang-item.h  | 9 +
 2 files changed, 15 insertions(+)

diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index 5a7bfd5f79c0..76fcd348e3f4 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -92,6 +92,12 @@ const BiMap 
Rust::LangItem::lang_items = {{
   {"str", Kind::STR},
   {"f32_runtime", Kind::F32_RUNTIME},
   {"f64_runtime", Kind::F64_RUNTIME},
+
+  {"Some", Kind::OPTION_SOME},
+  {"None", Kind::OPTION_NONE},
+
+  {"into_iter", Kind::INTOITER_INTOITER},
+  {"next", Kind::ITERATOR_NEXT},
 }};
 
 tl::optional
diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h
index 18ba37dc22df..5d57533499ee 100644
--- a/gcc/rust/util/rust-lang-item.h
+++ b/gcc/rust/util/rust-lang-item.h
@@ -26,6 +26,9 @@ namespace Rust {
 class LangItem
 {
 public:
+  // FIXME: We should clean up that enum to make it more inline with the list 
of
+  // lang-items in Rust 1.49
+  // 
https://github.com/rust-lang/rust/blob/1.49.0/compiler/rustc_hir/src/lang_items.rs
   enum class Kind
   {
 // 
https://github.com/rust-lang/rust/blob/master/library/core/src/ops/arith.rs
@@ -117,6 +120,12 @@ public:
 STR,
 F32_RUNTIME,
 F64_RUNTIME,
+
+OPTION_SOME,
+OPTION_NONE,
+
+INTOITER_INTOITER,
+ITERATOR_NEXT,
   };
 
   static const BiMap lang_items;


[gcc/devel/rust/master] typecheck: Add note about erorring out on additional trait bounds.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:1d8041b36cc2c4e34a0a9dbf52b931b97db5ddca

commit 1d8041b36cc2c4e34a0a9dbf52b931b97db5ddca
Author: Arthur Cohen 
Date:   Wed Dec 25 11:07:17 2024 +

typecheck: Add note about erorring out on additional trait bounds.

If additional trait bounds aren't auto traits, then the typechecker
must error out (Rust-GCC#3008)

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-type.cc: Add TODO note.

Diff:
---
 gcc/rust/typecheck/rust-hir-type-check-type.cc | 5 +
 1 file changed, 5 insertions(+)

diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc 
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 2962674da62f..d2758a5f76ad 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -757,6 +757,11 @@ TypeCheckType::visit (HIR::TraitObjectType &type)
   std::vector specified_bounds;
   for (auto &bound : type.get_type_param_bounds ())
 {
+  // TODO: here we need to check if there are additional bounds that aren't
+  // auto traits. this is an error. for example, `dyn A + Sized + Sync` is
+  // okay, because Sized and Sync are both auto traits but `dyn A + Copy +
+  // Clone` is not okay and should error out.
+
   if (bound->get_bound_type ()
  != HIR::TypeParamBound::BoundType::TRAITBOUND)
continue;


[gcc/devel/rust/master] ast: Add new constructors for PathInExpression

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:ec1cf232403a6da004f10d292409216317036d89

commit ec1cf232403a6da004f10d292409216317036d89
Author: Arthur Cohen 
Date:   Sat Dec 21 22:53:50 2024 +

ast: Add new constructors for PathInExpression

This commit adds two new constructors for AST::PathInExpression: One using 
a provided lang-item, and one with an already built std::unique_ptr

gcc/rust/ChangeLog:

* ast/rust-path.h: Add two new constructors.

Diff:
---
 gcc/rust/ast/rust-path.h | 21 -
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index 93171321ea9c..09d15165a8c8 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -715,6 +715,24 @@ public:
   marked_for_strip (false)
   {}
 
+  PathInExpression (LangItem::Kind lang_item_kind,
+   std::vector outer_attrs, location_t locus)
+: outer_attrs (std::move (outer_attrs)),
+  has_opening_scope_resolution (false), locus (locus),
+  _node_id (Analysis::Mappings::get ().get_next_node_id ()),
+  path (Rust::make_unique (lang_item_kind, locus)),
+  marked_for_strip (false)
+  {}
+
+  PathInExpression (std::unique_ptr path,
+   std::vector outer_attrs, location_t locus,
+   bool has_opening_scope_resolution = false)
+: outer_attrs (std::move (outer_attrs)),
+  has_opening_scope_resolution (has_opening_scope_resolution),
+  locus (locus), _node_id (Analysis::Mappings::get ().get_next_node_id ()),
+  path (std::move (path)), marked_for_strip (false)
+  {}
+
   PathInExpression (const PathInExpression &other)
 : outer_attrs (other.outer_attrs),
   has_opening_scope_resolution (other.has_opening_scope_resolution),
@@ -738,7 +756,8 @@ public:
   // Creates an error state path in expression.
   static PathInExpression create_error ()
   {
-return PathInExpression ({}, {}, UNDEF_LOCATION);
+return PathInExpression (std::vector (), {},
+UNDEF_LOCATION);
   }
 
   // Returns whether path in expression is in an error state.


[gcc/devel/rust/master] ast-builder: Add more methods

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:62058776ed9efe98b1c3677383bc82db04aa4608

commit 62058776ed9efe98b1c3677383bc82db04aa4608
Author: Arthur Cohen 
Date:   Sat Dec 21 22:52:57 2024 +

ast-builder: Add more methods

This commit adds new methods for building pattern nodes, match expressions 
and more precise call expressions.

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc: Add new functions.
* ast/rust-ast-builder.h: Declare them.

Diff:
---
 gcc/rust/ast/rust-ast-builder.cc | 91 
 gcc/rust/ast/rust-ast-builder.h  | 30 +
 2 files changed, 121 insertions(+)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 121b8c8d7e0a..92bc5a2f1f49 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -20,6 +20,7 @@
 #include "rust-ast-builder-type.h"
 #include "rust-common.h"
 #include "rust-expr.h"
+#include "rust-path.h"
 #include "rust-token.h"
 #include "rust-make-unique.h"
 
@@ -42,6 +43,33 @@ Builder::call (std::unique_ptr &&path,
 new CallExpr (std::move (path), std::move (args), {}, loc));
 }
 
+std::unique_ptr
+Builder::call (std::unique_ptr &&path,
+  std::vector> &&args) const
+{
+  return call (std::unique_ptr (
+new PathInExpression (std::move (path), {}, loc)),
+  std::move (args));
+}
+
+std::unique_ptr
+Builder::call (std::unique_ptr &&path, std::unique_ptr &&arg) const
+{
+  auto args = std::vector> ();
+  args.emplace_back (std::move (arg));
+
+  return call (std::move (path), std::move (args));
+}
+
+std::unique_ptr
+Builder::call (std::unique_ptr &&path, std::unique_ptr &&arg) const
+{
+  auto args = std::vector> ();
+  args.emplace_back (std::move (arg));
+
+  return call (std::move (path), std::move (args));
+}
+
 std::unique_ptr
 Builder::array (std::vector> &&members) const
 {
@@ -56,6 +84,13 @@ Builder::identifier (std::string name) const
   return std::unique_ptr (new IdentifierExpr (name, {}, loc));
 }
 
+std::unique_ptr
+Builder::identifier_pattern (std::string name, bool mut) const
+{
+  return std::unique_ptr (
+new IdentifierPattern (name, loc, false, mut));
+}
+
 std::unique_ptr
 Builder::tuple_idx (std::string receiver, int idx) const
 {
@@ -117,6 +152,22 @@ Builder::path_in_expression (std::vector 
&&segments) const
   return PathInExpression (std::move (path_segments), {}, loc);
 }
 
+PathInExpression
+Builder::path_in_expression (LangItem::Kind lang_item) const
+{
+  return PathInExpression (lang_item, {}, loc);
+}
+
+std::unique_ptr
+Builder::block (std::unique_ptr &&stmt,
+   std::unique_ptr &&tail_expr) const
+{
+  auto stmts = std::vector> ();
+  stmts.emplace_back (std::move (stmt));
+
+  return block (std::move (stmts), std::move (tail_expr));
+}
+
 std::unique_ptr
 Builder::block (std::vector> &&stmts,
std::unique_ptr &&tail_expr) const
@@ -189,6 +240,46 @@ Builder::wildcard () const
   return std::unique_ptr (new WildcardPattern (loc));
 }
 
+std::unique_ptr
+Builder::lang_item_path (LangItem::Kind kind) const
+{
+  return std::unique_ptr (new LangItemPath (kind, loc));
+}
+
+std::unique_ptr
+Builder::match (std::unique_ptr &&scrutinee,
+   std::vector &&cases)
+{
+  return std::unique_ptr (
+new MatchExpr (std::move (scrutinee), std::move (cases), {}, {}, loc));
+}
+
+MatchArm
+Builder::match_arm (std::unique_ptr &&pattern)
+{
+  auto patterns = std::vector> ();
+  patterns.emplace_back (std::move (pattern));
+
+  return MatchArm (std::move (patterns), loc);
+}
+
+MatchCase
+Builder::match_case (std::unique_ptr &&pattern,
+std::unique_ptr &&expr)
+{
+  return MatchCase (match_arm (std::move (pattern)), std::move (expr));
+}
+
+std::unique_ptr
+Builder::loop (std::vector> &&stmts)
+{
+  auto block = std::unique_ptr (
+new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), 
loc,
+  loc));
+
+  return std::unique_ptr (new LoopExpr (std::move (block), loc));
+}
+
 std::unique_ptr
 Builder::new_type (Type &type)
 {
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index fa258c7dfa82..85469097fb57 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -20,6 +20,7 @@
 #define AST_BUILDER_H
 
 #include "rust-ast-full.h"
+#include "rust-expr.h"
 
 namespace Rust {
 namespace AST {
@@ -38,6 +39,8 @@ public:
 
   /* Create an identifier expression (`variable`) */
   std::unique_ptr identifier (std::string name) const;
+  std::unique_ptr identifier_pattern (std::string name,
+  bool mut = false) const;
 
   /* Create a tuple index expression (`receiver.0`) */
   std::unique_ptr tuple_idx (std::string receiver, int idx) const;
@@ -53,6 +56,9 @@ public:
   std::unique_ptr block (std::vector> &&stmts,
   std::unique_ptr &&tail_expr
   

[gcc/devel/rust/master] resolve: Name resolve trait bounds properly

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:5ab3543b1b0753f48ba364f4b6ea29c5a3f80ffa

commit 5ab3543b1b0753f48ba364f4b6ea29c5a3f80ffa
Author: Arthur Cohen 
Date:   Wed Dec 25 11:03:44 2024 +

resolve: Name resolve trait bounds properly

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-type.cc 
(ResolveTypeToCanonicalPath::visit): Resolve additional
trait bounds.
* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Error out 
properly on unresolved
type-path instead of crashing.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Exclude additional-trait-bounds2 for 
different error message.
* rust/compile/additional-trait-bounds1.rs: New test.
* rust/compile/additional-trait-bounds2.rs: New test.
* rust/compile/additional-trait-bounds2nr2.rs: New test.

Diff:
---
 gcc/rust/resolve/rust-ast-resolve-type.cc  | 57 --
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc|  3 +-
 .../rust/compile/additional-trait-bounds1.rs   | 10 
 .../rust/compile/additional-trait-bounds2.rs   |  9 
 .../rust/compile/additional-trait-bounds2nr2.rs| 11 +
 gcc/testsuite/rust/compile/nr2/exclude |  1 +
 6 files changed, 87 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc 
b/gcc/rust/resolve/rust-ast-resolve-type.cc
index 15ae8db9389e..56ce95f5f903 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-type.cc
@@ -18,6 +18,8 @@
 
 #include "rust-ast-resolve-type.h"
 #include "rust-ast-resolve-expr.h"
+#include "rust-canonical-path.h"
+#include "rust-type.h"
 
 namespace Rust {
 namespace Resolver {
@@ -495,10 +497,59 @@ ResolveTypeToCanonicalPath::visit 
(AST::TraitObjectTypeOneBound &type)
 }
 
 void
-ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &)
+ResolveTypeToCanonicalPath::visit (AST::TraitObjectType &type)
 {
-  // FIXME is this actually allowed? dyn A+B
-  rust_unreachable ();
+  rust_assert (!type.get_type_param_bounds ().empty ());
+
+  auto &first_bound = type.get_type_param_bounds ().front ();
+
+  // Is it allowed or even possible to have a lifetime bound as a first bound?
+  if (first_bound->get_bound_type () == AST::TraitBound::LIFETIME)
+rust_unreachable ();
+
+  auto &trait = static_cast (*first_bound);
+
+  CanonicalPath path = CanonicalPath::create_empty ();
+  bool ok = ResolveTypeToCanonicalPath::go (trait.get_type_path (), path);
+
+  // right?
+  rust_assert (ok);
+
+  auto slice_path = "get_bound_type ())
+   {
+ case AST::TypeParamBound::TRAIT: {
+   auto bound_path = CanonicalPath::create_empty ();
+
+   auto &bound_type_path
+ = static_cast (*additional_bound)
+ .get_type_path ();
+   bool ok
+ = ResolveTypeToCanonicalPath::go (bound_type_path, bound_path);
+
+   if (!ok)
+ continue;
+
+   str = bound_path.get ();
+   break;
+ }
+   case AST::TypeParamBound::LIFETIME:
+ rust_unreachable ();
+ break;
+   }
+  slice_path += " + " + str;
+}
+
+  slice_path += ">";
+
+  result = CanonicalPath::new_seg (type.get_node_id (), slice_path);
 }
 
 void
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index f2245c987397..6d450bc59569 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -282,7 +282,8 @@ Late::visit (AST::TypePath &type)
 ctx.map_usage (Usage (type.get_node_id ()),
   Definition (resolved->get_node_id ()));
   else
-rust_unreachable ();
+rust_error_at (type.get_locus (), "could not resolve type path %qs",
+  str.c_str ());
 
   DefaultResolver::visit (type);
 }
diff --git a/gcc/testsuite/rust/compile/additional-trait-bounds1.rs 
b/gcc/testsuite/rust/compile/additional-trait-bounds1.rs
new file mode 100644
index ..449a72fe4612
--- /dev/null
+++ b/gcc/testsuite/rust/compile/additional-trait-bounds1.rs
@@ -0,0 +1,10 @@
+#![feature(optin_builtin_traits)]
+
+pub unsafe auto trait Send {}
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+trait A {}
+
+impl dyn A + Send {}
+impl dyn A + Send + Sync {}
diff --git a/gcc/testsuite/rust/compile/additional-trait-bounds2.rs 
b/gcc/testsuite/rust/compile/additional-trait-bounds2.rs
new file mode 100644
index ..843228ae9a64
--- /dev/null
+++ b/gcc/testsuite/rust/compile/additional-trait-bounds2.rs
@@ -0,0 +1,9 @@
+#![feature(optin_builtin_traits)]
+
+pub unsafe auto trait Send {}
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+trait A {}
+
+impl dyn A + Send + Sync + NonExist {} // { dg-error "failed to resolve 
TypePath: NonExist in this scope" }
diff --git a/gcc/testsuite/rust/compile/additional-trait-bounds2nr2.rs 
b/gcc/testsuite/rust/compile/additional

[gcc/devel/rust/master] typecheck-path: Fix typo (reciever -> receiver)

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:af5fdeebdcde53bded8145c6eb4cca9ca1ad047b

commit af5fdeebdcde53bded8145c6eb4cca9ca1ad047b
Author: Arthur Cohen 
Date:   Fri Dec 20 11:29:32 2024 +

typecheck-path: Fix typo (reciever -> receiver)

gcc/rust/ChangeLog:

* typecheck/rust-hir-path-probe.cc: Fix typos.
* typecheck/rust-hir-path-probe.h: Likewise.
* typecheck/rust-hir-type-check-path.cc: Likewise.

Diff:
---
 gcc/rust/typecheck/rust-hir-path-probe.cc  |  4 ++--
 gcc/rust/typecheck/rust-hir-path-probe.h   |  2 +-
 gcc/rust/typecheck/rust-hir-type-check-path.cc | 10 +-
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc 
b/gcc/rust/typecheck/rust-hir-path-probe.cc
index fa3ef47ebeaa..16976c34989d 100644
--- a/gcc/rust/typecheck/rust-hir-path-probe.cc
+++ b/gcc/rust/typecheck/rust-hir-path-probe.cc
@@ -168,7 +168,7 @@ PathProbeType::Probe (const TyTy::BaseType *receiver,
   if (!probe_bounds)
 return probe.candidates;
 
-  if (!probe.is_reciever_generic ())
+  if (!probe.is_receiver_generic ())
 {
   std::vector> probed_bounds
= TypeBoundsProbe::Probe (receiver);
@@ -433,7 +433,7 @@ PathProbeType::union_bounds (
 }
 
 bool
-PathProbeType::is_reciever_generic () const
+PathProbeType::is_receiver_generic () const
 {
   const TyTy::BaseType *root = receiver->get_root ();
   bool receiver_is_type_param = root->get_kind () == TyTy::TypeKind::PARAM;
diff --git a/gcc/rust/typecheck/rust-hir-path-probe.h 
b/gcc/rust/typecheck/rust-hir-path-probe.h
index b71d8c33fdef..5839a7e02144 100644
--- a/gcc/rust/typecheck/rust-hir-path-probe.h
+++ b/gcc/rust/typecheck/rust-hir-path-probe.h
@@ -145,7 +145,7 @@ protected:
 const std::vector> b)
 const;
 
-  bool is_reciever_generic () const;
+  bool is_receiver_generic () const;
 
   const TyTy::BaseType *receiver;
   const HIR::PathIdentSegment &search;
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc 
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index d3f341264482..9e156725d5b8 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -354,13 +354,13 @@ TypeCheckExpr::resolve_segments (NodeId 
root_resolved_node_id,
 {
   NodeId resolved_node_id = root_resolved_node_id;
   TyTy::BaseType *prev_segment = tyseg;
-  bool reciever_is_generic = prev_segment->get_kind () == 
TyTy::TypeKind::PARAM;
-  bool reciever_is_dyn = prev_segment->get_kind () == TyTy::TypeKind::DYNAMIC;
+  bool receiver_is_generic = prev_segment->get_kind () == 
TyTy::TypeKind::PARAM;
+  bool receiver_is_dyn = prev_segment->get_kind () == TyTy::TypeKind::DYNAMIC;
 
   for (size_t i = offset; i < segments.size (); i++)
 {
   HIR::PathExprSegment &seg = segments.at (i);
-  bool probe_impls = !reciever_is_generic;
+  bool probe_impls = !receiver_is_generic;
 
   // probe the path is done in two parts one where we search impls if no
   // candidate is found then we search extensions from traits
@@ -435,7 +435,7 @@ TypeCheckExpr::resolve_segments (NodeId 
root_resolved_node_id,
}
}
 
-  if (associated_impl_block != nullptr && !reciever_is_dyn)
+  if (associated_impl_block != nullptr && !receiver_is_dyn)
{
  // associated types
  HirId impl_block_id
@@ -492,7 +492,7 @@ TypeCheckExpr::resolve_segments (NodeId 
root_resolved_node_id,
  if (tyseg->get_kind () == TyTy::TypeKind::ERROR)
return;
}
-  else if (tyseg->needs_generic_substitutions () && !reciever_is_generic)
+  else if (tyseg->needs_generic_substitutions () && !receiver_is_generic)
{
  location_t locus = seg.get_locus ();
  tyseg = SubstMapper::InferSubst (tyseg, locus);


[gcc/devel/rust/master] Fix NR2.0 compiler ICE caused by Generics in Enums

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:198d3ee34fbeec1749c184e30cc13b3d323f90dc

commit 198d3ee34fbeec1749c184e30cc13b3d323f90dc
Author: Liam Naddell 
Date:   Tue Dec 17 11:48:03 2024 -0500

Fix NR2.0 compiler ICE caused by Generics in Enums

gcc/rust/ChangeLog:
* resolve/rust-late-name-resolver-2.0.cc:
Change the late name resolver to enter proper lexical scope during 
typechecking
* resolve/rust-late-name-resolver-2.0.h:
Add needed prototype to header
* resolve/rust-toplevel-name-resolver-2.0.cc:
Add generic parameters to enum's scoped RIB to allow for proper 
name resolution on types.

gcc/testsuite/ChangeLog:
* rust/compile/issue-3304.rs:
Add small test for generics+enums combination for NR2.0

Signed-off-by: Liam Naddell 

Diff:
---
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc |  7 +++
 gcc/rust/resolve/rust-late-name-resolver-2.0.h  |  1 +
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc |  9 +
 gcc/testsuite/rust/compile/issue-3304.rs| 10 ++
 4 files changed, 27 insertions(+)

diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index 6d450bc59569..a331e24908f4 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -295,6 +295,13 @@ Late::visit (AST::StructStruct &s)
   ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
 }
 
+void
+Late::visit (AST::Enum &s)
+{
+  auto s_vis = [this, &s] () { AST::DefaultASTVisitor::visit (s); };
+  ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
+}
+
 void
 Late::visit (AST::StructExprStruct &s)
 {
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
index 98cf09205bf4..7260f99507d1 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
@@ -52,6 +52,7 @@ public:
   void visit (AST::StructExprStructBase &) override;
   void visit (AST::StructExprStructFields &) override;
   void visit (AST::StructStruct &) override;
+  void visit (AST::Enum &) override;
   void visit (AST::GenericArgs &) override;
   void visit (AST::GenericArg &);
 
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index 01c5d26f4c15..8fa4809d7158 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -332,6 +332,15 @@ TopLevel::visit (AST::EnumItemDiscriminant &variant)
 void
 TopLevel::visit (AST::Enum &enum_item)
 {
+  auto generic_vis = [this, &enum_item] () {
+for (auto &g : enum_item.get_generic_params ())
+  {
+   g->accept_vis (*this);
+  }
+  };
+
+  ctx.scoped (Rib::Kind::Item, enum_item.get_node_id (), generic_vis);
+
   insert_or_error_out (enum_item.get_identifier (), enum_item,
   Namespace::Types);
 
diff --git a/gcc/testsuite/rust/compile/issue-3304.rs 
b/gcc/testsuite/rust/compile/issue-3304.rs
new file mode 100644
index ..6ab614fa2d5d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3304.rs
@@ -0,0 +1,10 @@
+// { dg-additional-options "-frust-name-resolution-2.0" }
+#[lang = "sized"]
+trait Sized {}
+
+pub enum ROption {
+RSome(T),
+RNone,
+}
+
+fn main() {}


[gcc/devel/rust/master] ast: Add EnumItem::Kind

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:daf98a1495d274dd12eba238229741c80899bdcc

commit daf98a1495d274dd12eba238229741c80899bdcc
Author: Arthur Cohen 
Date:   Thu Jan 2 18:31:54 2025 +

ast: Add EnumItem::Kind

gcc/rust/ChangeLog:

* ast/rust-item.h: Add EnumItem::Kind for differentiating all 
variants that may be
used inside an enum declaration.

Diff:
---
 gcc/rust/ast/rust-item.h | 52 
 1 file changed, 52 insertions(+)

diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 4e3338a487ce..a380081a0421 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -1994,6 +1994,41 @@ class EnumItem : public VisItem
   location_t locus;
 
 public:
+  enum class Kind
+  {
+Identifier,
+Tuple,
+Struct,
+
+// FIXME: In the future, we'll need to remove this possibility as well as
+// remove the EnumItemDiscriminant class. The feature for arbitrary
+// discriminants on all kinds of variants has been stabilized, and a
+// "discriminant" is no longer an enum item variant - it's simply an
+// optional part of all variants.
+//
+// Per the reference:
+//
+// EnumItem :
+//OuterAttribute* Visibility?
+//IDENTIFIER ( EnumItemTuple | EnumItemStruct )? EnumItemDiscriminant?
+//
+// EnumItemTuple :
+//( TupleFields? )
+//
+// EnumItemStruct :
+//{ StructFields? }
+//
+// EnumItemDiscriminant :
+//= Expression
+//
+// So we instead need to remove the class, and add an optional expression 
to
+// the base EnumItem class
+//
+// gccrs#3340
+
+Discriminant,
+  };
+
   virtual ~EnumItem () {}
 
   EnumItem (Identifier variant_name, Visibility vis,
@@ -2002,6 +2037,8 @@ public:
   variant_name (std::move (variant_name)), locus (locus)
   {}
 
+  virtual Kind get_enum_item_kind () const { return Kind::Identifier; }
+
   // Unique pointer custom clone function
   std::unique_ptr clone_enum_item () const
   {
@@ -2043,6 +2080,11 @@ public:
   tuple_fields (std::move (tuple_fields))
   {}
 
+  EnumItem::Kind get_enum_item_kind () const override
+  {
+return EnumItem::Kind::Tuple;
+  }
+
   std::string as_string () const override;
 
   void accept_vis (ASTVisitor &vis) override;
@@ -2080,6 +2122,11 @@ public:
   struct_fields (std::move (struct_fields))
   {}
 
+  EnumItem::Kind get_enum_item_kind () const override
+  {
+return EnumItem::Kind::Struct;
+  }
+
   std::string as_string () const override;
 
   void accept_vis (ASTVisitor &vis) override;
@@ -2133,6 +2180,11 @@ public:
   EnumItemDiscriminant (EnumItemDiscriminant &&other) = default;
   EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default;
 
+  EnumItem::Kind get_enum_item_kind () const override
+  {
+return EnumItem::Kind::Discriminant;
+  }
+
   std::string as_string () const override;
 
   void accept_vis (ASTVisitor &vis) override;


[gcc/devel/rust/master] lang-items: Collect trait functions that are lang items

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:9a07804e4d950331a63eac23615ab99b41388b0a

commit 9a07804e4d950331a63eac23615ab99b41388b0a
Author: Arthur Cohen 
Date:   Wed Dec 18 12:20:27 2024 +0100

lang-items: Collect trait functions that are lang items

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.cc (CollectLangItems::visit): Add 
visitor for collecting
functions that might be lang items.
* ast/rust-collect-lang-items.h: Likewise.

Diff:
---
 gcc/rust/ast/rust-collect-lang-items.cc | 8 
 gcc/rust/ast/rust-collect-lang-items.h  | 1 +
 2 files changed, 9 insertions(+)

diff --git a/gcc/rust/ast/rust-collect-lang-items.cc 
b/gcc/rust/ast/rust-collect-lang-items.cc
index 308720ae69ab..50d134a429f5 100644
--- a/gcc/rust/ast/rust-collect-lang-items.cc
+++ b/gcc/rust/ast/rust-collect-lang-items.cc
@@ -82,5 +82,13 @@ CollectLangItems::visit (AST::TraitItemType &item)
   DefaultASTVisitor::visit (item);
 }
 
+void
+CollectLangItems::visit (AST::Function &item)
+{
+  maybe_add_lang_item (item);
+
+  DefaultASTVisitor::visit (item);
+}
+
 } // namespace AST
 } // namespace Rust
diff --git a/gcc/rust/ast/rust-collect-lang-items.h 
b/gcc/rust/ast/rust-collect-lang-items.h
index 552648f04eda..1d021b1d9c50 100644
--- a/gcc/rust/ast/rust-collect-lang-items.h
+++ b/gcc/rust/ast/rust-collect-lang-items.h
@@ -47,6 +47,7 @@ public:
 
   void visit (AST::Trait &item) override;
   void visit (AST::TraitItemType &item) override;
+  void visit (AST::Function &item) override;
 
 private:
   template  void maybe_add_lang_item (const T &item);


[gcc/devel/rust/master] nr2.0: Resolve type aliases inside trait definitions

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:43369f262c5a3dc63ae6a14c679f25d06256084d

commit 43369f262c5a3dc63ae6a14c679f25d06256084d
Author: Owen Avery 
Date:   Thu Jan 2 05:29:03 2025 -0500

nr2.0: Resolve type aliases inside trait definitions

gcc/rust/ChangeLog:

* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Add visitor for TraitItemType.
* resolve/rust-toplevel-name-resolver-2.0.h
(TopLevel::visit): Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery 

Diff:
---
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc | 9 +
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h  | 1 +
 gcc/testsuite/rust/compile/nr2/exclude  | 5 -
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index e51aef8e9d04..369003765563 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -109,6 +109,15 @@ TopLevel::visit (AST::Trait &trait)
   DefaultResolver::visit (trait);
 }
 
+void
+TopLevel::visit (AST::TraitItemType &trait_item)
+{
+  insert_or_error_out (trait_item.get_identifier ().as_string (), trait_item,
+  Namespace::Types);
+
+  DefaultResolver::visit (trait_item);
+}
+
 template 
 static void
 insert_macros (std::vector ¯os, NameResolutionContext &ctx)
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
index ce84cd392fea..081ad2ddd829 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
@@ -148,6 +148,7 @@ private:
 
   void visit (AST::Module &module) override;
   void visit (AST::Trait &trait) override;
+  void visit (AST::TraitItemType &trait_item) override;
   void visit (AST::MacroRulesDefinition ¯o) override;
   void visit (AST::Function &function) override;
   void visit (AST::BlockExpr &expr) override;
diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index e23669f309b4..da5880d9a57c 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -74,8 +74,6 @@ issue-2139.rs
 issue-2142.rs
 issue-2165.rs
 issue-2166.rs
-issue-2190-1.rs
-issue-2190-2.rs
 issue-2238.rs
 issue-2304.rs
 issue-2330.rs
@@ -85,7 +83,6 @@ issue-2723-1.rs
 issue-2723-2.rs
 issue-2772-2.rs
 issue-2775.rs
-issue-2747.rs
 issue-2782.rs
 issue-2812.rs
 issue-850.rs
@@ -98,7 +95,6 @@ macros/mbe/macro-issue1233.rs
 macros/mbe/macro-issue1400.rs
 macros/mbe/macro13.rs
 macros/mbe/macro15.rs
-macros/mbe/macro20.rs
 macros/mbe/macro23.rs
 macros/mbe/macro40.rs
 macros/mbe/macro43.rs
@@ -198,7 +194,6 @@ iflet.rs
 issue-3033.rs
 issue-3009.rs
 issue-2323.rs
-issue-2953-1.rs
 issue-2953-2.rs
 issue-1773.rs
 issue-2905-1.rs


[gcc/devel/rust/master] ast: Add new Kind enums for more precise downcasting

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:26d3103c2d65741221920a0239d9f02ac5e61e6d

commit 26d3103c2d65741221920a0239d9f02ac5e61e6d
Author: Arthur Cohen 
Date:   Sat Dec 21 22:56:52 2024 +

ast: Add new Kind enums for more precise downcasting

This commit adds things like Item::Kind, Expr::Kind, etc, and implements 
the associated `get_*_kind` functions.
It also removes the more generic AST::Kind enum we were using, which was 
incomplete and painful to use.

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add new Kind enums, remove Node class.
* ast/rust-builtin-ast-nodes.h: Use new Kind enums.
* ast/rust-expr.h (class LoopLabel): Likewise.
* ast/rust-item.h: Likewise.
* ast/rust-macro.h: Likewise.
* ast/rust-path.h: Likewise.
* expand/rust-macro-builtins-helpers.cc: Likewise.
* expand/rust-macro-builtins-utility.cc 
(MacroBuiltin::concat_handler): Likewise.
(MacroBuiltin::stringify_handler): Likewise.
* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Likewise.
* resolve/rust-early-name-resolver.cc: Likewise.
* hir/rust-ast-lower.cc (ASTLoweringBlock::visit): Likewise.

Diff:
---
 gcc/rust/ast/rust-ast.h| 94 ++
 gcc/rust/ast/rust-builtin-ast-nodes.h  |  2 +
 gcc/rust/ast/rust-expr.h   | 84 ++-
 gcc/rust/ast/rust-item.h   | 36 +-
 gcc/rust/ast/rust-macro.h  | 34 ++
 gcc/rust/ast/rust-path.h   | 10 +++
 gcc/rust/expand/rust-macro-builtins-helpers.cc |  2 +-
 gcc/rust/expand/rust-macro-builtins-utility.cc |  4 +-
 gcc/rust/hir/rust-ast-lower.cc |  6 +-
 gcc/rust/resolve/rust-ast-resolve-expr.cc  |  2 +-
 gcc/rust/resolve/rust-early-name-resolver.cc   | 10 +--
 11 files changed, 229 insertions(+), 55 deletions(-)

diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index f83c99a57d2d..2d907e2ca21a 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -70,16 +70,6 @@ namespace AST {
 class ASTVisitor;
 using AttrVec = std::vector;
 
-// The available kinds of AST Nodes
-enum class Kind
-{
-  UNKNOWN,
-  MODULE,
-  MACRO_RULES_DEFINITION,
-  MACRO_INVOCATION,
-  IDENTIFIER,
-};
-
 class Visitable
 {
 public:
@@ -87,20 +77,6 @@ public:
   virtual void accept_vis (ASTVisitor &vis) = 0;
 };
 
-// Abstract base class for all AST elements
-class Node : public Visitable
-{
-public:
-  /**
-   * Get the kind of Node this is. This is used to differentiate various AST
-   * elements with very little overhead when extracting the derived type
-   * through static casting is not necessary.
-   */
-  // FIXME: Mark this as `= 0` in the future to make sure every node
-  // implements it
-  virtual Kind get_ast_kind () const { return Kind::UNKNOWN; }
-};
-
 // Delimiter types - used in macros and whatever.
 enum DelimType
 {
@@ -1092,7 +1068,7 @@ class MetaListNameValueStr;
 /* Base statement abstract class. Note that most "statements" are not allowed
  * in top-level module scope - only a subclass of statements called "items"
  * are. */
-class Stmt : public Node
+class Stmt : public Visitable
 {
 public:
   enum class Kind
@@ -1141,6 +1117,28 @@ protected:
 class Item : public Stmt
 {
 public:
+  enum class Kind
+  {
+MacroRulesDefinition,
+MacroInvocation,
+Module,
+ExternCrate,
+UseDeclaration,
+Function,
+TypeAlias,
+Struct,
+EnumItem,
+Enum,
+Union,
+ConstantItem,
+StaticItem,
+Trait,
+Impl,
+ExternBlock,
+  };
+
+  virtual Kind get_item_kind () const = 0;
+
   // Unique pointer custom clone function
   std::unique_ptr clone_item () const
   {
@@ -1221,14 +1219,54 @@ public:
   {
 return outer_attrs;
   }
+
+  virtual Item::Kind get_item_kind () const override = 0;
 };
+
 // forward decl of ExprWithoutBlock
 class ExprWithoutBlock;
 
 // Base expression AST node - abstract
-class Expr : public Node
+class Expr : public Visitable
 {
 public:
+  enum class Kind
+  {
+PathInExpression,
+QualifiedPathInExpression,
+Literal,
+Operator,
+Grouped,
+Array,
+ArrayIndex,
+Tuple,
+TupleIndex,
+Struct,
+Call,
+MethodCall,
+FieldAccess,
+Closure,
+Block,
+Continue,
+Break,
+Range,
+Box,
+Return,
+UnsafeBlock,
+Loop,
+If,
+IfLet,
+Match,
+Await,
+AsyncBlock,
+InlineAsm,
+Identifier,
+FormatArgs,
+MacroInvocation,
+  };
+
+  virtual Kind get_expr_kind () const = 0;
+
   // Unique pointer custom clone function
   std::unique_ptr clone_expr () const
   {
@@ -1343,7 +1381,7 @@ public:
 outer_attrs = std::move (new_attrs);
   }
 
-  Kind get_ast_kind () const override { return Kind::IDENTIFIER; }
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::Identifier; }
 
 pr

[gcc/devel/rust/master] nr2.0: Improve default, top-level, and late resolvers

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:fa6747f326dfbf883292d5599c7d926cbf6c62e3

commit fa6747f326dfbf883292d5599c7d926cbf6c62e3
Author: Owen Avery 
Date:   Thu Jan 2 05:55:38 2025 -0500

nr2.0: Improve default, top-level, and late resolvers

gcc/rust/ChangeLog:

* resolve/rust-default-resolver.cc
(DefaultResolver::visit): Make sure to scope visitation of the
children of type definition items.
* resolve/rust-default-resolver.h
(DefaultResolver::visit): Add overrides for TupleStruct, Union,
and TypeAlias.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Remove override for Enum.
* resolve/rust-late-name-resolver-2.0.h
(Late::visit): Likewise.
* resolve/rust-toplevel-name-resolver-2.0.cc
(TopLevel::visit): Rely more on DefaultResolver::visit.
* resolve/rust-toplevel-name-resolver-2.0.h
(TopLevel::visit): Remove override for BlockExpr.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery 

Diff:
---
 gcc/rust/resolve/rust-default-resolver.cc  | 35 ---
 gcc/rust/resolve/rust-default-resolver.h   |  3 ++
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc|  7 ---
 gcc/rust/resolve/rust-late-name-resolver-2.0.h |  1 -
 .../resolve/rust-toplevel-name-resolver-2.0.cc | 50 +-
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h |  1 -
 gcc/testsuite/rust/compile/nr2/exclude |  9 
 7 files changed, 41 insertions(+), 65 deletions(-)

diff --git a/gcc/rust/resolve/rust-default-resolver.cc 
b/gcc/rust/resolve/rust-default-resolver.cc
index 5d0f7a0e9303..58e477660cdc 100644
--- a/gcc/rust/resolve/rust-default-resolver.cc
+++ b/gcc/rust/resolve/rust-default-resolver.cc
@@ -88,27 +88,48 @@ DefaultResolver::visit (AST::TraitImpl &impl)
 void
 DefaultResolver::visit (AST::StructStruct &type)
 {
-  // do we need to scope anything here? no, right?
+  auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
 
-  // we also can't visit `StructField`s by default, so there's nothing to do -
-  // correct? or should we do something like
+  ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
+ inner_fn, type.get_struct_name ());
+}
 
-  AST::DefaultASTVisitor::visit (type);
+void
+DefaultResolver::visit (AST::TupleStruct &type)
+{
+  auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
 
-  // FIXME: ???
+  ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
+ inner_fn, type.get_struct_name ());
 }
 
 void
 DefaultResolver::visit (AST::Enum &type)
 {
-  // FIXME: Do we need to scope anything by default?
-
   auto variant_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
 
   ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
  variant_fn, type.get_identifier ());
 }
 
+void
+DefaultResolver::visit (AST::Union &type)
+{
+  auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
+
+  ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
+ inner_fn, type.get_identifier ());
+}
+
+void
+DefaultResolver::visit (AST::TypeAlias &type)
+{
+  auto inner_fn = [this, &type] () { AST::DefaultASTVisitor::visit (type); };
+
+  ctx.scoped (Rib::Kind::Item /* FIXME: Correct? */, type.get_node_id (),
+ inner_fn, type.get_new_type_name ());
+}
+
 void
 DefaultResolver::visit (AST::ClosureExprInner &expr)
 {
diff --git a/gcc/rust/resolve/rust-default-resolver.h 
b/gcc/rust/resolve/rust-default-resolver.h
index 21e67d8e5c5e..eb08dc87fead 100644
--- a/gcc/rust/resolve/rust-default-resolver.h
+++ b/gcc/rust/resolve/rust-default-resolver.h
@@ -52,7 +52,10 @@ public:
 
   // type dec nodes, which visit their fields or variants by default
   void visit (AST::StructStruct &) override;
+  void visit (AST::TupleStruct &) override;
   void visit (AST::Enum &) override;
+  void visit (AST::Union &) override;
+  void visit (AST::TypeAlias &) override;
 
   // Visitors that visit their expression node(s)
   void visit (AST::ClosureExprInner &) override;
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index b28d38f3e399..974e1fa3129b 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -308,13 +308,6 @@ Late::visit (AST::StructStruct &s)
   ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
 }
 
-void
-Late::visit (AST::Enum &s)
-{
-  auto s_vis = [this, &s] () { AST::DefaultASTVisitor::visit (s); };
-  ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis);
-}
-
 void
 Late::visit (AST::StructExprStruct &s)
 {
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-late-name-r

[gcc/devel/rust/master] gccrs: use StackedContexts for block context

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:a62630f99093b253bf499d0c2da4765e197a5608

commit a62630f99093b253bf499d0c2da4765e197a5608
Author: Prajwal S N 
Date:   Mon Dec 16 13:29:46 2024 +0530

gccrs: use StackedContexts for block context

Replaces the DIY vector stack with a StackedContexts object for block
scopes in the type checker context.

Fixes #3284.

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check.h (class TypeCheckContext): add
header file and use StackedContexts for blocks
* typecheck/rust-typecheck-context.cc: update methods
* typecheck/rust-hir-trait-resolve.cc: refactor function calls
* typecheck/rust-hir-type-check-implitem.cc: refactor function calls
* typecheck/rust-hir-type-check-type.cc: refactor function calls

Signed-off-by: Prajwal S N 

Diff:
---
 gcc/rust/typecheck/rust-hir-trait-resolve.cc   |  4 ++--
 gcc/rust/typecheck/rust-hir-type-check-implitem.cc |  5 +++--
 gcc/rust/typecheck/rust-hir-type-check-type.cc |  7 +++---
 gcc/rust/typecheck/rust-hir-type-check.h   |  8 +++
 gcc/rust/typecheck/rust-typecheck-context.cc   | 26 +++---
 5 files changed, 15 insertions(+), 35 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc 
b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index 470e9b45919d..8b90039f2835 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -278,7 +278,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
 }
   self->inherit_bounds (specified_bounds);
 
-  context->push_block_context (TypeCheckBlockContextItem (trait_reference));
+  context->block_context ().enter (TypeCheckBlockContextItem 
(trait_reference));
   std::vector item_refs;
   for (auto &item : trait_reference->get_trait_items ())
 {
@@ -308,7 +308,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
   // resolve the blocks of functions etc because it can end up in a recursive
   // loop of trying to resolve traits as required by the types
   tref->on_resolved ();
-  context->pop_block_context ();
+  context->block_context ().exit ();
 
   return tref;
 }
diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc 
b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
index fac96b1a8b39..ee2c6b78452d 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
@@ -335,9 +335,10 @@ TypeCheckImplItem::Resolve (
 
   // resolve
   TypeCheckImplItem resolver (parent, self, substitutions);
-  resolver.context->push_block_context (TypeCheckBlockContextItem (&parent));
+  resolver.context->block_context ().enter (
+TypeCheckBlockContextItem (&parent));
   item.accept_vis (resolver);
-  resolver.context->pop_block_context ();
+  resolver.context->block_context ().exit ();
 
   return resolver.result;
 }
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc 
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 3f4076c00dc8..ad4199a16b2c 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -591,10 +591,11 @@ TypeCheckType::resolve_segments (
   bool first_segment = i == offset;
   bool selfResolveOk = false;
 
-  if (first_segment && tySegIsBigSelf && context->have_block_context ()
- && context->peek_block_context ().is_impl_block ())
+  if (first_segment && tySegIsBigSelf
+ && context->block_context ().is_in_context ()
+ && context->block_context ().peek ().is_impl_block ())
{
- TypeCheckBlockContextItem ctx = context->peek_block_context ();
+ TypeCheckBlockContextItem ctx = context->block_context ().peek ();
  TyTy::BaseType *lookup = nullptr;
  selfResolveOk
= resolve_associated_type (seg->as_string (), ctx, &lookup);
diff --git a/gcc/rust/typecheck/rust-hir-type-check.h 
b/gcc/rust/typecheck/rust-hir-type-check.h
index 65c929af9477..42ef2d14325f 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.h
+++ b/gcc/rust/typecheck/rust-hir-type-check.h
@@ -22,6 +22,7 @@
 #include "rust-hir-map.h"
 #include "rust-tyty.h"
 #include "rust-hir-trait-reference.h"
+#include "rust-stacked-contexts.h"
 #include "rust-autoderef.h"
 #include "rust-tyty-region.h"
 #include "rust-tyty-variance-analysis.h"
@@ -186,10 +187,7 @@ public:
 TyTy::BaseType *return_type);
   void pop_return_type ();
 
-  bool have_block_context () const;
-  TypeCheckBlockContextItem peek_block_context ();
-  void push_block_context (TypeCheckBlockContextItem item);
-  void pop_block_context ();
+  StackedContexts &block_context ();
 
   void iterate (std::function cb);
 
@@ -282,7 +280,7 @@ private:
   std::vector>
 return_type_stack;
   std::vector loop_type_stack;
-  std::vector block_stack;
+  StackedContexts block_stack;
   std::map trait_context;
   std::

[gcc/devel/rust/master] lower: Correctly lower parenthesized types

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:31d3f550bad1d8fe8d9f0d08252412a8d106d860

commit 31d3f550bad1d8fe8d9f0d08252412a8d106d860
Author: Arthur Cohen 
Date:   Wed Dec 25 17:55:09 2024 +

lower: Correctly lower parenthesized types

This is useful for handling multiple trait bounds, and required for better 
handling of auto traits.

gcc/rust/ChangeLog:

* hir/rust-ast-lower-type.cc (ASTLoweringType::visit): Add 
implementation for
ParenthesizedType.
* hir/rust-ast-lower-type.h: Declare that new visitor.

gcc/testsuite/ChangeLog:

* rust/compile/auto_traits1.rs: New test.

Diff:
---
 gcc/rust/hir/rust-ast-lower-type.cc| 19 +++
 gcc/rust/hir/rust-ast-lower-type.h |  2 ++
 gcc/testsuite/rust/compile/auto_traits1.rs | 27 +++
 3 files changed, 48 insertions(+)

diff --git a/gcc/rust/hir/rust-ast-lower-type.cc 
b/gcc/rust/hir/rust-ast-lower-type.cc
index 2c124d1a16ba..605a2e572104 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -19,6 +19,7 @@
 #include "rust-ast-lower-type.h"
 #include "rust-hir-map.h"
 #include "rust-hir-path.h"
+#include "rust-hir-type.h"
 #include "rust-path.h"
 #include "rust-pattern.h"
 
@@ -471,6 +472,24 @@ ASTLoweringType::visit (AST::TraitObjectType &type)
 type.get_locus (), type.is_dyn ());
 }
 
+void
+ASTLoweringType::visit (AST::ParenthesisedType &type)
+{
+  auto *inner = ASTLoweringType::translate (*type.get_type_in_parens (),
+   default_to_static_lifetime);
+
+  auto crate_num = mappings.get_current_crate ();
+  Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
+mappings.get_next_hir_id (crate_num),
+mappings.get_next_localdef_id (crate_num));
+
+  // FIXME: Do we actually need to know if a type is parenthesized in the HIR?
+  // or can we just use the type in parens?
+  translated
+= new HIR::ParenthesisedType (mapping, std::unique_ptr (inner),
+ type.get_locus ());
+}
+
 HIR::GenericParam *
 ASTLowerGenericParam::translate (AST::GenericParam ¶m)
 {
diff --git a/gcc/rust/hir/rust-ast-lower-type.h 
b/gcc/rust/hir/rust-ast-lower-type.h
index 2509276dec22..1e88ec2bd14d 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -22,6 +22,7 @@
 #include "rust-ast-lower-base.h"
 #include "rust-ast-lower-expr.h"
 #include "rust-hir-path.h"
+#include "rust-type.h"
 
 namespace Rust {
 namespace HIR {
@@ -83,6 +84,7 @@ public:
   void visit (AST::NeverType &type) override;
   void visit (AST::TraitObjectTypeOneBound &type) override;
   void visit (AST::TraitObjectType &type) override;
+  void visit (AST::ParenthesisedType &type) override;
 
 private:
   ASTLoweringType (bool default_to_static_lifetime)
diff --git a/gcc/testsuite/rust/compile/auto_traits1.rs 
b/gcc/testsuite/rust/compile/auto_traits1.rs
new file mode 100644
index ..192052d48151
--- /dev/null
+++ b/gcc/testsuite/rust/compile/auto_traits1.rs
@@ -0,0 +1,27 @@
+// { dg-additional-options "-frust-compile-until=typecheck" }
+
+#![feature(optin_builtin_traits)]
+
+pub unsafe auto trait Send {}
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+trait A {
+fn a_method(&self) {}
+}
+
+fn foo(a: &(dyn A + Send + Sync)) {
+a.a_method();
+}
+
+struct S;
+
+impl A for S {
+fn a_method(&self) {}
+}
+
+fn main() {
+let s = S;
+
+foo(&s);
+}


[gcc/devel/rust/master] tychk: Add more support for additional trait bounds in functions

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:9de42352bb28dc20752fa81ca26cf9929936a48a

commit 9de42352bb28dc20752fa81ca26cf9929936a48a
Author: Arthur Cohen 
Date:   Wed Dec 25 18:13:43 2024 +

tychk: Add more support for additional trait bounds in functions

This commit correctly lowers and typechecks parenthesized types, which are 
used for trait objects with additional bounds.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-type.cc (ResolveType::visit): New 
visitor to handle
ParenthesizedType.
* resolve/rust-ast-resolve-type.h: Likewise.
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): 
Likewise.
* typecheck/rust-hir-type-check-type.h: Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/auto_traits2.rs: New test.
* rust/compile/auto_traits3.rs: New test.
* rust/compile/nr2/exclude: Add auto_traits2 test.

Diff:
---
 gcc/rust/resolve/rust-ast-resolve-type.cc  |  6 +
 gcc/rust/resolve/rust-ast-resolve-type.h   |  3 +++
 gcc/rust/typecheck/rust-hir-type-check-type.cc |  6 +
 gcc/rust/typecheck/rust-hir-type-check-type.h  |  4 +--
 gcc/testsuite/rust/compile/auto_traits2.rs | 26 
 gcc/testsuite/rust/compile/auto_traits3.rs | 34 ++
 gcc/testsuite/rust/compile/nr2/exclude |  2 ++
 7 files changed, 78 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc 
b/gcc/rust/resolve/rust-ast-resolve-type.cc
index 56ce95f5f903..a4878a2b816a 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-type.cc
@@ -50,6 +50,12 @@ ResolveType::visit (AST::TraitObjectType &type)
 }
 }
 
+void
+ResolveType::visit (AST::ParenthesisedType &type)
+{
+  resolved_node = ResolveType::go (*type.get_type_in_parens ());
+}
+
 void
 ResolveType::visit (AST::ReferenceType &type)
 {
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h 
b/gcc/rust/resolve/rust-ast-resolve-type.h
index 763838ea488b..3a7dbd68dabb 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.h
+++ b/gcc/rust/resolve/rust-ast-resolve-type.h
@@ -24,6 +24,7 @@
 #include "rust-diagnostics.h"
 #include "rust-hir-map.h"
 #include "rust-path.h"
+#include "rust-type.h"
 #include "util/rust-hir-map.h"
 
 namespace Rust {
@@ -143,6 +144,8 @@ public:
 
   void visit (AST::TraitObjectType &type) override;
 
+  void visit (AST::ParenthesisedType &type) override;
+
   void visit (AST::SliceType &type) override;
 
 private:
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.cc 
b/gcc/rust/typecheck/rust-hir-type-check-type.cc
index 3c41b2b59fa6..3f4076c00dc8 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.cc
@@ -790,6 +790,12 @@ TypeCheckType::visit (HIR::TraitObjectType &type)
   std::move (specified_bounds));
 }
 
+void
+TypeCheckType::visit (HIR::ParenthesisedType &type)
+{
+  translated = TypeCheckType::Resolve (type.get_type_in_parens ());
+}
+
 void
 TypeCheckType::visit (HIR::ArrayType &type)
 {
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h 
b/gcc/rust/typecheck/rust-hir-type-check-type.h
index 10acde081b7d..aafdac86b8b4 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.h
@@ -59,6 +59,7 @@ public:
   void visit (HIR::InferredType &type) override;
   void visit (HIR::NeverType &type) override;
   void visit (HIR::TraitObjectType &type) override;
+  void visit (HIR::ParenthesisedType &type) override;
 
   void visit (HIR::TypePathSegmentFunction &segment) override
   { /* TODO */
@@ -69,9 +70,6 @@ public:
   void visit (HIR::ImplTraitType &type) override
   { /* TODO */
   }
-  void visit (HIR::ParenthesisedType &type) override
-  { /* TODO */
-  }
   void visit (HIR::ImplTraitTypeOneBound &type) override
   { /* TODO */
   }
diff --git a/gcc/testsuite/rust/compile/auto_traits2.rs 
b/gcc/testsuite/rust/compile/auto_traits2.rs
new file mode 100644
index ..7d0dcc11cd2a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/auto_traits2.rs
@@ -0,0 +1,26 @@
+#![feature(optin_builtin_traits)]
+
+pub unsafe auto trait Send {}
+#[lang = "sync"]
+pub unsafe auto trait Sync {}
+
+trait A {
+fn a_method(&self) {}
+}
+
+fn foo(a: &(dyn A + Send + Sync)) {
+a.a_method();
+}
+
+struct S;
+
+impl A for S {
+fn a_method(&self) {}
+}
+
+fn main() {
+let s = S;
+
+foo(&s); // { dg-error "bounds not satisfied" }
+ // { dg-error "mismatched type" "" { target *-*-* } .-1 }
+}
diff --git a/gcc/testsuite/rust/compile/auto_traits3.rs 
b/gcc/testsuite/rust/compile/auto_traits3.rs
new file mode 100644
index ..81c39ecda7f4
--- /dev/null
+++ b/gcc/testsuite/rust/compile/auto_traits3.rs
@@ -0,0 +1,34 @@
+#![feature(optin_builtin_traits)]
+
+pub unsafe auto trait Send {}
+#[lang = "sync"]
+pub unsafe auto trait Sync {}

[gcc/devel/rust/master] gccrs: fix ICE during HIR dump

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:48aa71c3a7f5b8d9eb634c53e389c74dc29f7fe2

commit 48aa71c3a7f5b8d9eb634c53e389c74dc29f7fe2
Author: Philip Herron 
Date:   Mon Jan 6 11:02:51 2025 +

gccrs: fix ICE during HIR dump

These hir nodes have optional expressions which need guarded

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::do_qualifiedpathtype): add guard
(Dump::do_traitfunctiondecl): likewise
(Dump::visit): likewise

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/hir/rust-hir-dump.cc | 9 ++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index a6a880121a1d..112e129ea4ab 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -404,7 +404,8 @@ Dump::do_qualifiedpathtype (QualifiedPathType &e)
   do_mappings (e.get_mappings ());
   visit_field ("type", e.get_type ());
 
-  visit_field ("trait", e.get_trait ());
+  if (e.has_as_clause ())
+visit_field ("trait", e.get_trait ());
 }
 
 void
@@ -521,7 +522,8 @@ Dump::do_traitfunctiondecl (TraitFunctionDecl &e)
   else
 put_field ("function_params", "empty");
 
-  visit_field ("return_type", e.get_return_type ());
+  if (e.has_return_type ())
+visit_field ("return_type", e.get_return_type ());
 
   if (e.has_where_clause ())
 put_field ("where_clause", e.get_where_clause ().as_string ());
@@ -1295,7 +1297,8 @@ Dump::visit (BreakExpr &e)
   else
 put_field ("label", "none");
 
-  visit_field ("break_expr ", e.get_expr ());
+  if (e.has_break_expr ())
+visit_field ("break_expr ", e.get_expr ());
 
   end ("BreakExpr");
 }


[gcc/devel/rust/master] build: update bootstrap req to C++14

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:f3b1216cc6b8f1341ca635f679e7987c454e263d

commit f3b1216cc6b8f1341ca635f679e7987c454e263d
Author: Jason Merrill 
Date:   Tue Sep 17 17:38:35 2024 -0400

build: update bootstrap req to C++14

We moved to a bootstrap requirement of C++11 in GCC 11, 8 years after
support was stable in GCC 4.8.

It is now 8 years since C++14 was the default mode in GCC 6 (and 9 years
since support was complete in GCC 5), and we have a few bits of optional
C++14 code in the compiler, so it seems a good time to update the bootstrap
requirement again.

The big benefit of the change is the greater constexpr power, but C++14 also
added variable templates, generic lambdas, lambda init-capture, binary
literals, and numeric literal digit separators.

C++14 was feature-complete in GCC 5, and became the default in GCC 6.  5.4.0
bootstraps trunk correctly; trunk stage1 built with 5.3.0 breaks in
eh_data_format_name due to PR69995.

gcc/ChangeLog:

* doc/install.texi (Prerequisites): Update to C++14.

ChangeLog:

* configure.ac: Update requirement to C++14.
* configure: Regenerate.

Diff:
---
 configure| 832 +++
 configure.ac |  12 +-
 gcc/doc/install.texi |  27 +-
 3 files changed, 795 insertions(+), 76 deletions(-)

diff --git a/configure b/configure
index d3374ba2229a..d18ce9f4d8ad 100755
--- a/configure
+++ b/configure
@@ -712,8 +712,8 @@ gmplibs
 PGO_BUILD_LTO_CFLAGS
 PGO_BUILD_USE_CFLAGS
 PGO_BUILD_GEN_CFLAGS
-HAVE_CXX11_FOR_BUILD
-HAVE_CXX11
+HAVE_CXX14_FOR_BUILD
+HAVE_CXX14
 do_compare
 GDC
 GNATMAKE
@@ -5865,13 +5865,13 @@ $as_echo "$as_me: WARNING: trying to bootstrap a cross 
compiler" >&2;}
 ;;
 esac
 
-# When bootstrapping with GCC, build stage 1 in C++11 mode to ensure that a
-# C++11 compiler can still start the bootstrap.  Otherwise, if building GCC,
-# require C++11 (or higher).
+# When bootstrapping with GCC, build stage 1 in C++14 mode to ensure that a
+# C++14 compiler can still start the bootstrap.  Otherwise, if building GCC,
+# require C++14 (or higher).
 if test "$enable_bootstrap:$GXX" = "yes:yes"; then
-  CXX="$CXX -std=c++11"
+  CXX="$CXX -std=c++14"
 elif test "$have_compiler" = yes; then
-ax_cxx_compile_alternatives="11 0x"ax_cxx_compile_cxx11_required=true
+ax_cxx_compile_alternatives="14 1y"ax_cxx_compile_cxx14_required=true
   ac_ext=cpp
 ac_cpp='$CXXCPP $CPPFLAGS'
 ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
@@ -5879,9 +5879,9 @@ ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS 
$LDFLAGS conftest.$ac_ex
 ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
   ac_success=no
 
-  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX supports 
C++11 features by default" >&5
-$as_echo_n "checking whether $CXX supports C++11 features by default... " >&6; 
}
-if ${ax_cv_cxx_compile_cxx11+:} false; then :
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether $CXX supports 
C++14 features by default" >&5
+$as_echo_n "checking whether $CXX supports C++14 features by default... " >&6; 
}
+if ${ax_cv_cxx_compile_cxx14+:} false; then :
   $as_echo_n "(cached) " >&6
 else
   cat confdefs.h - <<_ACEOF >conftest.$ac_ext
@@ -6174,26 +6174,146 @@ namespace cxx11
 
 
 
+
+// If the compiler admits that it is not ready for C++14, why torture it?
+// Hopefully, this will speed up the test.
+
+#ifndef __cplusplus
+
+#error "This is not a C++ compiler"
+
+#elif __cplusplus < 201402L
+
+#error "This is not a C++14 compiler"
+
+#else
+
+namespace cxx14
+{
+
+  namespace test_polymorphic_lambdas
+  {
+
+int
+test()
+{
+  const auto lambda = [](auto&&... args){
+const auto istiny = [](auto x){
+  return (sizeof(x) == 1UL) ? 1 : 0;
+};
+const int aretiny[] = { istiny(args)... };
+return aretiny[0];
+  };
+  return lambda(1, 1L, 1.0f, '1');
+}
+
+  }
+
+  namespace test_binary_literals
+  {
+
+constexpr auto ivii = 0b00101010;
+static_assert(ivii == 42, "wrong value");
+
+  }
+
+  namespace test_generalized_constexpr
+  {
+
+template < typename CharT >
+constexpr unsigned long
+strlen_c(const CharT *const s) noexcept
+{
+  auto length = 0UL;
+  for (auto p = s; *p; ++p)
+++length;
+  return length;
+}
+
+static_assert(strlen_c("") == 0UL, "");
+static_assert(strlen_c("x") == 1UL, "");
+static_assert(strlen_c("test") == 4UL, "");
+static_assert(strlen_c("another\0test") == 7UL, "");
+
+  }
+
+  namespace test_lambda_init_capture
+  {
+
+int
+test()
+{
+  auto x = 0;
+  const auto lambda1 = [a = x](int b){ return a + b; };
+  const auto lambda2 = [a = lambda1(x)](){ return a; };
+  return lambda2();
+}
+
+  }
+
+  namespace test_digit_separators
+  {
+
+constexpr auto ten_million = 100'000'000;

[gcc/devel/rust/master] derive(Clone): Mark PhantomData as a lang item

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:533abf7e53c6edb699d67418a84b662bd41cf381

commit 533abf7e53c6edb699d67418a84b662bd41cf381
Author: Arthur Cohen 
Date:   Thu Dec 26 10:57:07 2024 +

derive(Clone): Mark PhantomData as a lang item

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro4.rs: Make PhantomData a lang item.

Diff:
---
 gcc/testsuite/rust/compile/derive_macro4.rs | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/gcc/testsuite/rust/compile/derive_macro4.rs 
b/gcc/testsuite/rust/compile/derive_macro4.rs
index 7802e8fd8000..b20043ba927b 100644
--- a/gcc/testsuite/rust/compile/derive_macro4.rs
+++ b/gcc/testsuite/rust/compile/derive_macro4.rs
@@ -6,12 +6,9 @@ pub trait Clone {
 fn clone(&self) -> Self;
 }
 
+#[lang = "phantom_data"]
 struct PhantomData;
 
-pub struct AssertParamIsCopy {
-_field: PhantomData,
-}
-
 #[derive(Clone)] // { dg-error "bounds not satisfied for U .Copy. is not 
satisfied" }
 union U {
 i: i32,


[gcc/devel/rust/master] ast-collector: Adapt to lang item type path segments

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:b8eb6cd44753940f312db3d42ba7c2e6a0a57dbd

commit b8eb6cd44753940f312db3d42ba7c2e6a0a57dbd
Author: Arthur Cohen 
Date:   Tue Dec 31 17:36:50 2024 +

ast-collector: Adapt to lang item type path segments

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Fix collector 
to better
handle lang item type path segments.

Diff:
---
 gcc/rust/ast/rust-ast-collector.cc | 23 +++
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index d9ebaee68c6b..32e021c4d3b4 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -543,10 +543,14 @@ TokenCollector::visit (TypePathSegment &segment)
 {
   // Syntax:
   //PathIdentSegment
-  auto ident_segment = segment.get_ident_segment ();
-  auto id = ident_segment.as_string ();
-  push (
-Rust::Token::make_identifier (ident_segment.get_locus (), std::move (id)));
+
+  auto locus = segment.is_lang_item ()
+? segment.get_locus ()
+: segment.get_ident_segment ().get_locus ();
+  auto segment_string = segment.is_lang_item ()
+ ? LangItem::PrettyString (segment.get_lang_item ())
+ : segment.get_ident_segment ().as_string ();
+  push (Rust::Token::make_identifier (locus, std::move (segment_string)));
 }
 
 void
@@ -558,10 +562,13 @@ TokenCollector::visit (TypePathSegmentGeneric &segment)
   //`<` `>`
   //| `<` ( GenericArg `,` )* GenericArg `,`? `>`
 
-  auto ident_segment = segment.get_ident_segment ();
-  auto id = ident_segment.as_string ();
-  push (
-Rust::Token::make_identifier (ident_segment.get_locus (), std::move (id)));
+  auto locus = segment.is_lang_item ()
+? segment.get_locus ()
+: segment.get_ident_segment ().get_locus ();
+  auto segment_string = segment.is_lang_item ()
+ ? LangItem::PrettyString (segment.get_lang_item ())
+ : segment.get_ident_segment ().as_string ();
+  push (Rust::Token::make_identifier (locus, std::move (segment_string)));
 
   if (segment.get_separating_scope_resolution ())
 push (Rust::Token::make (SCOPE_RESOLUTION, UNDEF_LOCATION));


[gcc/devel/rust/master] derive(Clone): Manually generate AssertParamIsCopy struct for unions

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:d970cf081f7528938470e66c3dc5ee9940d1645f

commit d970cf081f7528938470e66c3dc5ee9940d1645f
Author: Arthur Cohen 
Date:   Thu Dec 26 10:50:13 2024 +

derive(Clone): Manually generate AssertParamIsCopy struct for unions

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::visit_union): Manually 
generate
the struct used for asserting a union implements Copy.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc | 38 
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index 2d1b5995ba2f..eeffb009b953 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -17,6 +17,8 @@
 // .
 
 #include "rust-derive-clone.h"
+#include "rust-ast.h"
+#include "rust-ast-dump.h"
 #include "rust-item.h"
 
 namespace Rust {
@@ -238,13 +240,40 @@ void
 DeriveClone::visit_union (Union &item)
 {
   // FIXME: Should be $crate::core::clone::AssertParamIsCopy (or similar)
+  // (Rust-GCC#3329)
+
+  auto copy_path = TypePath (vec (builder.type_path_segment ("Copy")), loc);
+  auto sized_path = TypePath (vec (builder.type_path_segment ("Sized")), loc);
+
+  auto copy_bound = std::unique_ptr (
+new TraitBound (copy_path, item.get_locus ()));
+  auto sized_bound = std::unique_ptr (
+new TraitBound (sized_path, item.get_locus (), false, true));
+
+  auto bounds = std::vector> ();
+  bounds.emplace_back (std::move (copy_bound));
+  bounds.emplace_back (std::move (sized_bound));
+
+  // struct AssertParamIsCopy { _t: PhantomData }
+  auto assert_param_is_copy = "AssertParamIsCopy";
+  auto t = std::unique_ptr (
+new TypeParam (Identifier ("T"), item.get_locus (), std::move (bounds)));
+  auto assert_param_is_copy_struct = builder.struct_struct (
+assert_param_is_copy, vec (std::move (t)),
+{StructField (
+  Identifier ("_t"),
+  builder.single_generic_type_path (
+   "PhantomData",
+   GenericArgs (
+ {}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
+  Visibility::create_private (), item.get_locus ())});
 
   // 
   auto arg = GenericArg::create_type (builder.single_type_path ("Self"));
 
   // AssertParamIsCopy::
   auto type = std::unique_ptr (
-new TypePathSegmentGeneric (PathIdentSegment ("AssertParamIsCopy", loc),
+new TypePathSegmentGeneric (PathIdentSegment (assert_param_is_copy, loc),
false, GenericArgs ({}, {arg}, {}, loc), loc));
   auto type_paths = std::vector> ();
   type_paths.emplace_back (std::move (type));
@@ -252,11 +281,12 @@ DeriveClone::visit_union (Union &item)
   auto full_path
 = std::unique_ptr (new TypePath ({std::move (type_paths)}, loc));
 
-  auto stmts = std::vector> ();
-  stmts.emplace_back (
-builder.let (builder.wildcard (), std::move (full_path), nullptr));
   auto tail_expr = builder.deref (builder.identifier ("self"));
 
+  auto stmts
+= vec (std::move (assert_param_is_copy_struct),
+  builder.let (builder.wildcard (), std::move (full_path), nullptr));
+
   auto block = builder.block (std::move (stmts), std::move (tail_expr));
 
   expanded = clone_impl (clone_fn (std::move (block)),


[gcc/devel/rust/master] lang-items: Mark Clone trait as a lang item in testsuite

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:3351befe04d041e57f345075727346573c78560e

commit 3351befe04d041e57f345075727346573c78560e
Author: Arthur Cohen 
Date:   Fri Jan 3 15:46:33 2025 +

lang-items: Mark Clone trait as a lang item in testsuite

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro1.rs: Add #[lang = "clone"] to Clone 
trait.
* rust/compile/derive_macro3.rs: Likewise.
* rust/compile/derive_macro6.rs: Likewise.
* rust/execute/torture/derive_macro3.rs: Likewise.

Diff:
---
 gcc/testsuite/rust/compile/derive_macro1.rs | 1 +
 gcc/testsuite/rust/compile/derive_macro3.rs | 1 +
 gcc/testsuite/rust/compile/derive_macro6.rs | 7 +++
 gcc/testsuite/rust/execute/torture/derive_macro3.rs | 1 +
 4 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/rust/compile/derive_macro1.rs 
b/gcc/testsuite/rust/compile/derive_macro1.rs
index 779aad78e11d..bc10d601bb8d 100644
--- a/gcc/testsuite/rust/compile/derive_macro1.rs
+++ b/gcc/testsuite/rust/compile/derive_macro1.rs
@@ -1,6 +1,7 @@
 #[lang = "sized"]
 pub trait Sized {}
 
+#[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;
 }
diff --git a/gcc/testsuite/rust/compile/derive_macro3.rs 
b/gcc/testsuite/rust/compile/derive_macro3.rs
index 1c7d4737bfe9..ad40cae94b51 100644
--- a/gcc/testsuite/rust/compile/derive_macro3.rs
+++ b/gcc/testsuite/rust/compile/derive_macro3.rs
@@ -1,6 +1,7 @@
 #[lang = "sized"]
 pub trait Sized {}
 
+#[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;
 }
diff --git a/gcc/testsuite/rust/compile/derive_macro6.rs 
b/gcc/testsuite/rust/compile/derive_macro6.rs
index b7bf7a78acd1..35327c03b54c 100644
--- a/gcc/testsuite/rust/compile/derive_macro6.rs
+++ b/gcc/testsuite/rust/compile/derive_macro6.rs
@@ -2,6 +2,9 @@
 pub trait Sized {}
 
 pub trait Copy {}
+
+
+#[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;
 }
@@ -9,10 +12,6 @@ pub trait Clone {
 #[lang = "phantom_data"]
 pub struct PhantomData;
 
-pub struct AssertParamIsCopy {
-pub _field: PhantomData,
-}
-
 impl Copy for i32 {}
 impl Copy for i64 {}
 impl Copy for U {}
diff --git a/gcc/testsuite/rust/execute/torture/derive_macro3.rs 
b/gcc/testsuite/rust/execute/torture/derive_macro3.rs
index 7b3a089d7514..4138a5bf7e4c 100644
--- a/gcc/testsuite/rust/execute/torture/derive_macro3.rs
+++ b/gcc/testsuite/rust/execute/torture/derive_macro3.rs
@@ -1,6 +1,7 @@
 #[lang = "sized"]
 pub trait Sized {}
 
+#[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;
 }


[gcc/devel/rust/master] derive(Copy): Use copy lang item when deriving Copy.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:1a0ec0ec4c8045772603f667abd306266f824ea2

commit 1a0ec0ec4c8045772603f667abd306266f824ea2
Author: Arthur Cohen 
Date:   Thu Dec 26 22:09:11 2024 +

derive(Copy): Use copy lang item when deriving Copy.

gcc/rust/ChangeLog:

* expand/rust-derive-copy.cc: Use lang item path.

Diff:
---
 gcc/rust/expand/rust-derive-copy.cc | 8 ++--
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-copy.cc 
b/gcc/rust/expand/rust-derive-copy.cc
index 358a52e94816..1f52a08e7cc0 100644
--- a/gcc/rust/expand/rust-derive-copy.cc
+++ b/gcc/rust/expand/rust-derive-copy.cc
@@ -18,6 +18,7 @@
 
 #include "rust-derive-copy.h"
 #include "rust-ast-full.h"
+#include "rust-hir-map.h"
 #include "rust-mapping-common.h"
 #include "rust-path.h"
 
@@ -43,12 +44,7 @@ DeriveCopy::copy_impl (
   std::string name,
   const std::vector> &type_generics)
 {
-  // `$crate::core::marker::Copy` instead
-  auto segments = std::vector> ();
-  segments.emplace_back (builder.type_path_segment ("Copy"));
-
-  auto copy = TypePath (std::move (segments), loc);
-  // auto copy = TypePath (LangItem::Kind::COPY, loc);
+  auto copy = builder.type_path (LangItem::Kind::COPY);
 
   // we need to build up the generics for this impl block which will be just a
   // clone of the types specified ones


[gcc/devel/rust/master] ast-collector: Fix tuple struct pattern collection

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:aacecbae35bec0f9fbbc53f9db1c46fa6ba70ecc

commit aacecbae35bec0f9fbbc53f9db1c46fa6ba70ecc
Author: Arthur Cohen 
Date:   Fri Jan 3 14:28:07 2025 +

ast-collector: Fix tuple struct pattern collection

gcc/rust/ChangeLog:

* ast/rust-ast-collector.cc (TokenCollector::visit): Visit tuple 
pattern items as
separated by commas.

Diff:
---
 gcc/rust/ast/rust-ast-collector.cc | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-collector.cc 
b/gcc/rust/ast/rust-ast-collector.cc
index 32e021c4d3b4..a3c54198358b 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -2470,10 +2470,7 @@ TokenCollector::visit (StructPattern &pattern)
 void
 TokenCollector::visit (TupleStructItemsNoRange &pattern)
 {
-  for (auto &pat : pattern.get_patterns ())
-{
-  visit (pat);
-}
+  visit_items_joined_by_separator (pattern.get_patterns ());
 }
 
 void


[gcc/devel/rust/master] derive(Clone): Implement clone for enum tuple variants

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:40574e5eb02fb0d607267f39e338dcbd53f650da

commit 40574e5eb02fb0d607267f39e338dcbd53f650da
Author: Arthur Cohen 
Date:   Fri Jan 3 14:27:52 2025 +

derive(Clone): Implement clone for enum tuple variants

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::variant_match_path): 
New function.
(DeriveClone::clone_enum_identifier): Rename.
(DeriveClone::clone_enum_tuple): New function.
(DeriveClone::visit_enum): Visit tuple variants properly.
* expand/rust-derive-clone.h: Declare new functions.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc | 58 +---
 gcc/rust/expand/rust-derive-clone.h  | 12 
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index cf944a194276..36fa52935583 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -21,6 +21,8 @@
 #include "rust-ast-dump.h"
 #include "rust-item.h"
 #include "rust-path.h"
+#include "rust-pattern.h"
+#include "rust-system.h"
 
 namespace Rust {
 namespace AST {
@@ -236,14 +238,20 @@ DeriveClone::visit_struct (StructStruct &item)
 item.get_generic_params ());
 }
 
+PathInExpression
+DeriveClone::variant_match_path (Enum &item, const Identifier &variant)
+{
+  return PathInExpression ({builder.path_segment (
+ item.get_identifier ().as_string ()),
+   builder.path_segment (variant.as_string ())},
+  {}, loc, false);
+}
+
 MatchCase
 DeriveClone::clone_enum_identifier (Enum &item,
const std::unique_ptr &variant)
 {
-  auto variant_path = PathInExpression (
-{builder.path_segment (item.get_identifier ().as_string ()),
- builder.path_segment (variant->get_identifier ().as_string ())},
-{}, loc, false);
+  auto variant_path = variant_match_path (item, variant->get_identifier ());
 
   auto pattern = std::unique_ptr (new ReferencePattern (
 std::unique_ptr (new PathInExpression (variant_path)), false,
@@ -253,6 +261,45 @@ DeriveClone::clone_enum_identifier (Enum &item,
   return builder.match_case (std::move (pattern), std::move (expr));
 }
 
+MatchCase
+DeriveClone::clone_enum_tuple (Enum &item, const EnumItemTuple &variant)
+{
+  auto variant_path = variant_match_path (item, variant.get_identifier ());
+
+  auto patterns = std::vector> ();
+  auto cloned_patterns = std::vector> ();
+
+  for (size_t i = 0; i < variant.get_tuple_fields ().size (); i++)
+{
+  // The pattern we're creating for each field is `self_` where `i` is
+  // the index of the field. It doesn't actually matter what we use, as 
long
+  // as it's ordered, unique, and that we can reuse it in the match case's
+  // return expression to clone the field.
+  auto pattern_str = "__self_" + std::to_string (i);
+
+  patterns.emplace_back (builder.identifier_pattern (pattern_str));
+
+  // Now, for each tuple's element, we create a new expression calling
+  // `clone` on it for the match case's return expression
+  cloned_patterns.emplace_back (
+   clone_call (builder.ref (builder.identifier (pattern_str;
+}
+
+  auto pattern_items = std::unique_ptr (
+new TupleStructItemsNoRange (std::move (patterns)));
+
+  auto pattern = std::unique_ptr (
+new ReferencePattern (std::unique_ptr (new TupleStructPattern (
+   variant_path, std::move (pattern_items))),
+ false, false, loc));
+
+  auto expr
+= builder.call (std::unique_ptr (new PathInExpression 
(variant_path)),
+   std::move (cloned_patterns));
+
+  return builder.match_case (std::move (pattern), std::move (expr));
+}
+
 void
 DeriveClone::visit_enum (Enum &item)
 {
@@ -274,6 +321,9 @@ DeriveClone::visit_enum (Enum &item)
  cases.emplace_back (clone_enum_identifier (item, variant));
  break;
case EnumItem::Kind::Tuple:
+ cases.emplace_back (
+   clone_enum_tuple (item, static_cast (*variant)));
+ break;
case EnumItem::Kind::Struct:
  rust_unreachable ();
  break;
diff --git a/gcc/rust/expand/rust-derive-clone.h 
b/gcc/rust/expand/rust-derive-clone.h
index e5181170b4f9..844b53f0472f 100644
--- a/gcc/rust/expand/rust-derive-clone.h
+++ b/gcc/rust/expand/rust-derive-clone.h
@@ -63,8 +63,20 @@ private:
   clone_impl (std::unique_ptr &&clone_fn, std::string name,
  const std::vector> &type_generics);
 
+  /**
+   * Get the path to use for matching and creating a variant when matching on 
an
+   * enum. E.g. for the `Option` enum, with the `None` variant, this will 
create
+   * a path `Option::None`
+   */
+  PathInExpression variant_match_path (Enum &item, const Identifier &variant);
+
+  /**
+   * Implementation of clone f

[gcc/devel/rust/master] derive(Clone): Add deriving of simple enum variants

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:43747f4062a81e3b3f0e10b8ff2573eab9acca35

commit 43747f4062a81e3b3f0e10b8ff2573eab9acca35
Author: Arthur Cohen 
Date:   Thu Jan 2 10:59:33 2025 +

derive(Clone): Add deriving of simple enum variants

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc: Clone enum identifier variants 
properly
* expand/rust-derive-clone.h: Declare new functions used.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc | 52 ++--
 gcc/rust/expand/rust-derive-clone.h  |  3 +++
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index 2cabb72ac524..cf944a194276 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -20,6 +20,7 @@
 #include "rust-ast.h"
 #include "rust-ast-dump.h"
 #include "rust-item.h"
+#include "rust-path.h"
 
 namespace Rust {
 namespace AST {
@@ -235,11 +236,58 @@ DeriveClone::visit_struct (StructStruct &item)
 item.get_generic_params ());
 }
 
+MatchCase
+DeriveClone::clone_enum_identifier (Enum &item,
+   const std::unique_ptr &variant)
+{
+  auto variant_path = PathInExpression (
+{builder.path_segment (item.get_identifier ().as_string ()),
+ builder.path_segment (variant->get_identifier ().as_string ())},
+{}, loc, false);
+
+  auto pattern = std::unique_ptr (new ReferencePattern (
+std::unique_ptr (new PathInExpression (variant_path)), false,
+false, loc));
+  auto expr = std::unique_ptr (new PathInExpression (variant_path));
+
+  return builder.match_case (std::move (pattern), std::move (expr));
+}
+
 void
 DeriveClone::visit_enum (Enum &item)
 {
-  rust_sorry_at (item.get_locus (), "cannot derive %qs for these items yet",
-"Clone");
+  // Create an arm for each variant of the enum
+  // For enum item variants, just create the same variant
+  // For struct and tuple variants, destructure the pattern and call clone for
+  // each field
+
+  auto cases = std::vector ();
+
+  for (const auto &variant : item.get_variants ())
+{
+  switch (variant->get_enum_item_kind ())
+   {
+   // Identifiers and discriminated variants are the same for a clone - we
+   // just return the same variant
+   case EnumItem::Kind::Identifier:
+   case EnumItem::Kind::Discriminant:
+ cases.emplace_back (clone_enum_identifier (item, variant));
+ break;
+   case EnumItem::Kind::Tuple:
+   case EnumItem::Kind::Struct:
+ rust_unreachable ();
+ break;
+   }
+}
+
+  // match self { ... }
+  auto match = builder.match (builder.identifier ("self"), std::move (cases));
+
+  expanded = clone_impl (clone_fn (std::move (match)),
+item.get_identifier ().as_string (),
+item.get_generic_params ());
+
+  AST::Dump::debug (*expanded);
 }
 
 void
diff --git a/gcc/rust/expand/rust-derive-clone.h 
b/gcc/rust/expand/rust-derive-clone.h
index 043f9182efac..e5181170b4f9 100644
--- a/gcc/rust/expand/rust-derive-clone.h
+++ b/gcc/rust/expand/rust-derive-clone.h
@@ -63,6 +63,9 @@ private:
   clone_impl (std::unique_ptr &&clone_fn, std::string name,
  const std::vector> &type_generics);
 
+  MatchCase clone_enum_identifier (Enum &item,
+  const std::unique_ptr &variant);
+
   virtual void visit_struct (StructStruct &item);
   virtual void visit_tuple (TupleStruct &item);
   virtual void visit_enum (Enum &item);


[gcc/devel/rust/master] derive(Clone): Improve existing testcase

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:0722d35b69aa281622be416417a242a1fa698890

commit 0722d35b69aa281622be416417a242a1fa698890
Author: Arthur Cohen 
Date:   Thu Jan 2 11:00:21 2025 +

derive(Clone): Improve existing testcase

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro4.rs: Mark Copy and Clone as lang items.

Diff:
---
 gcc/testsuite/rust/compile/derive_macro4.rs | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/gcc/testsuite/rust/compile/derive_macro4.rs 
b/gcc/testsuite/rust/compile/derive_macro4.rs
index b20043ba927b..8bf1bcaf5f7b 100644
--- a/gcc/testsuite/rust/compile/derive_macro4.rs
+++ b/gcc/testsuite/rust/compile/derive_macro4.rs
@@ -1,7 +1,10 @@
 #[lang = "sized"]
 pub trait Sized {}
 
+#[lang = "copy"]
 pub trait Copy {}
+
+#[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;
 }


[gcc/devel/rust/master] nr2.0: late: Better format PathInExpression resolution

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:9c6e09e2f73c056ad30382a5d000d938e1dfd473

commit 9c6e09e2f73c056ad30382a5d000d938e1dfd473
Author: Arthur Cohen 
Date:   Wed Jan 15 11:55:54 2025 +

nr2.0: late: Better format PathInExpression resolution

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Improve 
formatting.

Diff:
---
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 18 ++
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index 60b8952f1524..f3ee08f4a03c 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -218,19 +218,12 @@ Late::visit (AST::PathInExpression &expr)
   // in a function item` error here?
   // do we emit it in `get`?
 
-  rust_debug ("[ARTHUR]: %s", expr.as_simple_path ().as_string ().c_str ());
+  auto resolved
+= ctx.values.resolve_path (expr.get_segments ()).or_else ([&] () {
+   return ctx.types.resolve_path (expr.get_segments ());
+  });
 
-  tl::optional resolved = tl::nullopt;
-
-  if (auto value = ctx.values.resolve_path (expr.get_segments ()))
-{
-  resolved = value;
-}
-  else if (auto type = ctx.types.resolve_path (expr.get_segments ()))
-{
-  resolved = type;
-}
-  else
+  if (!resolved)
 {
   rust_error_at (expr.get_locus (),
 "could not resolve path expression: %qs",
@@ -244,6 +237,7 @@ Late::visit (AST::PathInExpression &expr)
 expr.as_string ().c_str ());
   return;
 }
+
   ctx.map_usage (Usage (expr.get_node_id ()),
 Definition (resolved->get_node_id ()));
 }


[gcc/devel/rust/master] derive(Clone): Add lang item typepaths failure testcases to nr2 exclude

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:1c47dbe119b33765ab151d54a1992fe3ff44fbe8

commit 1c47dbe119b33765ab151d54a1992fe3ff44fbe8
Author: Arthur Cohen 
Date:   Wed Jan 15 10:41:28 2025 +

derive(Clone): Add lang item typepaths failure testcases to nr2 exclude

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Add failing lang item typepaths tests.
* rust/execute/torture/derive_macro4.rs: Mark Clone as lang item.

Diff:
---
 gcc/testsuite/rust/compile/nr2/exclude  | 5 +
 gcc/testsuite/rust/execute/torture/derive_macro4.rs | 1 +
 2 files changed, 6 insertions(+)

diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index 1a9c8e7113a3..60322f3276a9 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -146,4 +146,9 @@ cmp1.rs
 derive_clone_enum1.rs
 derive_clone_enum2.rs
 derive_clone_enum3.rs
+derive_macro4.rs
+derive_macro6.rs
+issue-2987.rs
+issue-3139-1.rs
+issue-3139-3.rs
 # please don't delete the trailing newline
diff --git a/gcc/testsuite/rust/execute/torture/derive_macro4.rs 
b/gcc/testsuite/rust/execute/torture/derive_macro4.rs
index c355ac7905f5..38c4808574a3 100644
--- a/gcc/testsuite/rust/execute/torture/derive_macro4.rs
+++ b/gcc/testsuite/rust/execute/torture/derive_macro4.rs
@@ -1,6 +1,7 @@
 #[lang = "sized"]
 pub trait Sized {}
 
+#[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;
 }


[gcc/devel/rust/master] derive(Clone): Use lang item for PhantomData in Clone

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:e8d3ccf05dce82d682649865a8e5fc61b92fc2d4

commit e8d3ccf05dce82d682649865a8e5fc61b92fc2d4
Author: Arthur Cohen 
Date:   Thu Dec 26 23:01:32 2024 +

derive(Clone): Use lang item for PhantomData in Clone

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::visit_union): Create a 
lang item path
instead of a regular path.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index eeffb009b953..aefc64cf97e9 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -263,7 +263,7 @@ DeriveClone::visit_union (Union &item)
 {StructField (
   Identifier ("_t"),
   builder.single_generic_type_path (
-   "PhantomData",
+   LangItem::Kind::PHANTOM_DATA,
GenericArgs (
  {}, {GenericArg::create_type (builder.single_type_path ("T"))}, {})),
   Visibility::create_private (), item.get_locus ())});


[gcc/devel/rust/master] derive(Clone): Add note about Clone::clone()

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:0c52c4c271dbde15541f495977ae9f9b14c49ef9

commit 0c52c4c271dbde15541f495977ae9f9b14c49ef9
Author: Arthur Cohen 
Date:   Thu Jan 2 10:56:11 2025 +

derive(Clone): Add note about Clone::clone()

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::clone_call): Mention 
using `clone_fn`
lang item in the future.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc | 14 ++
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index aefc64cf97e9..2cabb72ac524 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -30,6 +30,15 @@ DeriveClone::clone_call (std::unique_ptr &&to_clone)
   // $crate::core::clone::Clone::clone for the fully qualified path - we don't
   // link with `core` yet so that might be an issue. Use `Clone::clone` for 
now?
   // TODO: Factor this function inside the DeriveAccumulator
+
+  // Interestingly, later versions of Rust have a `clone_fn` lang item which
+  // corresponds to this. But because we are first targeting 1.49, we cannot 
use
+  // it yet. Once we target a new, more recent version of the language, we'll
+  // have figured out how to compile and distribute `core`, meaning we'll be
+  // able to directly call `::core::clone::Clone::clone()`
+
+  // Not sure how to call it properly in the meantime...
+
   auto path = std::unique_ptr (
 new PathInExpression (builder.path_in_expression ({"Clone", "clone"})));
 
@@ -79,10 +88,7 @@ DeriveClone::clone_impl (
   std::unique_ptr &&clone_fn, std::string name,
   const std::vector> &type_generics)
 {
-  // should that be `$crate::core::clone::Clone` instead?
-  auto segments = std::vector> ();
-  segments.emplace_back (builder.type_path_segment ("Clone"));
-  auto clone = TypePath (std::move (segments), loc);
+  auto clone = builder.type_path (LangItem::Kind::CLONE);
 
   auto trait_items = std::vector> ();
   trait_items.emplace_back (std::move (clone_fn));


[gcc/devel/rust/master] ast-builder: Add new methods around type paths.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:4b3588023ed829338da66d69f86df361dbf93066

commit 4b3588023ed829338da66d69f86df361dbf93066
Author: Arthur Cohen 
Date:   Thu Dec 26 22:09:46 2024 +

ast-builder: Add new methods around type paths.

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc: New functions.
* ast/rust-ast-builder.h: Declare them.

Diff:
---
 gcc/rust/ast/rust-ast-builder.cc | 57 ++--
 gcc/rust/ast/rust-ast-builder.h  | 13 -
 2 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index deef429908ca..728d5c0a4ede 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -23,6 +23,7 @@
 #include "rust-expr.h"
 #include "rust-path.h"
 #include "rust-item.h"
+#include "rust-path.h"
 #include "rust-token.h"
 
 namespace Rust {
@@ -101,12 +102,27 @@ Builder::type_path_segment (std::string seg) const
 }
 
 std::unique_ptr
-Builder::generic_type_path_segment (std::string seg, GenericArgs args) const
+Builder::type_path_segment (LangItem::Kind lang_item) const
+{
+  return std::unique_ptr (
+new TypePathSegment (lang_item, loc));
+}
+
+std::unique_ptr
+Builder::type_path_segment_generic (std::string seg, GenericArgs args) const
 {
   return std::unique_ptr (
 new TypePathSegmentGeneric (PathIdentSegment (seg, loc), false, args, 
loc));
 }
 
+std::unique_ptr
+Builder::type_path_segment_generic (LangItem::Kind lang_item,
+   GenericArgs args) const
+{
+  return std::unique_ptr (
+new TypePathSegmentGeneric (lang_item, args, loc));
+}
+
 std::unique_ptr
 Builder::single_type_path (std::string type) const
 {
@@ -116,15 +132,52 @@ Builder::single_type_path (std::string type) const
   return std::unique_ptr (new TypePath (std::move (segments), loc));
 }
 
+std::unique_ptr
+Builder::single_type_path (LangItem::Kind lang_item) const
+{
+  return std::unique_ptr (new TypePath (lang_item, {}, loc));
+}
+
 std::unique_ptr
 Builder::single_generic_type_path (std::string type, GenericArgs args) const
 {
   auto segments = std::vector> ();
-  segments.emplace_back (generic_type_path_segment (type, args));
+  segments.emplace_back (type_path_segment_generic (type, args));
 
   return std::unique_ptr (new TypePath (std::move (segments), loc));
 }
 
+std::unique_ptr
+Builder::single_generic_type_path (LangItem::Kind lang_item,
+  GenericArgs args) const
+{
+  auto segments = std::vector> ();
+  segments.emplace_back (type_path_segment_generic (lang_item, args));
+
+  return std::unique_ptr (new TypePath (std::move (segments), loc));
+}
+
+TypePath
+Builder::type_path (std::unique_ptr &&segment) const
+{
+  auto segments = std::vector> ();
+  segments.emplace_back (std::move (segment));
+
+  return TypePath (std::move (segments), loc);
+}
+
+TypePath
+Builder::type_path (std::string type) const
+{
+  return type_path (type_path_segment (type));
+}
+
+TypePath
+Builder::type_path (LangItem::Kind lang_item) const
+{
+  return type_path (type_path_segment (lang_item));
+}
+
 PathInExpression
 Builder::path_in_expression (std::vector &&segments) const
 {
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index bb003779e488..86279b0c5bb5 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -114,16 +114,27 @@ public:
 
   /* And similarly for type path segments */
   std::unique_ptr type_path_segment (std::string seg) const;
+  std::unique_ptr
+  type_path_segment (LangItem::Kind lang_item) const;
 
   std::unique_ptr
-  generic_type_path_segment (std::string seg, GenericArgs args) const;
+  type_path_segment_generic (std::string seg, GenericArgs args) const;
+  std::unique_ptr
+  type_path_segment_generic (LangItem::Kind lang_item, GenericArgs args) const;
 
   /* Create a Type from a single string - the most basic kind of type in our 
AST
*/
   std::unique_ptr single_type_path (std::string type) const;
+  std::unique_ptr single_type_path (LangItem::Kind lang_item) const;
 
   std::unique_ptr single_generic_type_path (std::string type,
  GenericArgs args) const;
+  std::unique_ptr single_generic_type_path (LangItem::Kind lang_item,
+ GenericArgs args) const;
+
+  TypePath type_path (std::unique_ptr &&segment) const;
+  TypePath type_path (std::string type) const;
+  TypePath type_path (LangItem::Kind lang_item) const;
 
   /**
* Create a path in expression from multiple segments (`Clone::clone`). You


[gcc/devel/rust/master] hir-dump: Improve handling of typepathsegments that are lang items.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:c84c92ac0fa33bb8bc2f01aa74d0fd15302913a1

commit c84c92ac0fa33bb8bc2f01aa74d0fd15302913a1
Author: Arthur Cohen 
Date:   Wed Jan 15 12:58:53 2025 +

hir-dump: Improve handling of typepathsegments that are lang items.

gcc/rust/ChangeLog:

* hir/rust-hir-dump.cc (Dump::do_typepathsegment): Add handling for 
lang items.

Diff:
---
 gcc/rust/hir/rust-hir-dump.cc | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc
index b1ada75e3ffb..1eca752d5d45 100644
--- a/gcc/rust/hir/rust-hir-dump.cc
+++ b/gcc/rust/hir/rust-hir-dump.cc
@@ -388,7 +388,10 @@ void
 Dump::do_typepathsegment (TypePathSegment &e)
 {
   do_mappings (e.get_mappings ());
-  put_field ("ident_segment", e.get_ident_segment ().as_string ());
+  if (e.is_lang_item ())
+put_field ("ident_segment", LangItem::PrettyString (e.get_lang_item ()));
+  else
+put_field ("ident_segment", e.get_ident_segment ().as_string ());
 }
 
 void


[gcc/devel/rust/master] derive(Clone): Use lang item bounds on AssertParamIsCopy

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:1f39e0f2cbe23d46b805f976db0807e5aa27d362

commit 1f39e0f2cbe23d46b805f976db0807e5aa27d362
Author: Arthur Cohen 
Date:   Mon Jan 20 11:36:53 2025 +

derive(Clone): Use lang item bounds on AssertParamIsCopy

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::visit_union): Use lang 
items for Copy and
Sized bounds.

gcc/testsuite/ChangeLog:

* rust/compile/derive_macro6.rs: Add lang item attribute to Copy 
trait.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc| 11 +--
 gcc/testsuite/rust/compile/derive_macro6.rs |  2 +-
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index 754d89cd84df..6ee83a72b531 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -422,17 +422,16 @@ DeriveClone::visit_union (Union &item)
   // FIXME: Should be $crate::core::clone::AssertParamIsCopy (or similar)
   // (Rust-GCC#3329)
 
-  auto copy_path = TypePath (vec (builder.type_path_segment ("Copy")), loc);
-  auto sized_path = TypePath (vec (builder.type_path_segment ("Sized")), loc);
+  auto copy_path = builder.type_path (LangItem::Kind::COPY);
+  auto sized_path = builder.type_path (LangItem::Kind::SIZED);
 
   auto copy_bound = std::unique_ptr (
 new TraitBound (copy_path, item.get_locus ()));
   auto sized_bound = std::unique_ptr (
-new TraitBound (sized_path, item.get_locus (), false, true));
+new TraitBound (sized_path, item.get_locus (), false,
+   true /* opening_question_mark */));
 
-  auto bounds = std::vector> ();
-  bounds.emplace_back (std::move (copy_bound));
-  bounds.emplace_back (std::move (sized_bound));
+  auto bounds = vec (std::move (copy_bound), std::move (sized_bound));
 
   // struct AssertParamIsCopy { _t: PhantomData }
   auto assert_param_is_copy = "AssertParamIsCopy";
diff --git a/gcc/testsuite/rust/compile/derive_macro6.rs 
b/gcc/testsuite/rust/compile/derive_macro6.rs
index 35327c03b54c..412144d5917a 100644
--- a/gcc/testsuite/rust/compile/derive_macro6.rs
+++ b/gcc/testsuite/rust/compile/derive_macro6.rs
@@ -1,9 +1,9 @@
 #[lang = "sized"]
 pub trait Sized {}
 
+#[lang = "copy"]
 pub trait Copy {}
 
-
 #[lang = "clone"]
 pub trait Clone {
 fn clone(&self) -> Self;


[gcc/devel/rust/master] derive(Clone): Add Clone bound on generated impl blocks

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:2d76d473f7b1f699ae939e6fa5cb821eebcd3881

commit 2d76d473f7b1f699ae939e6fa5cb821eebcd3881
Author: Arthur Cohen 
Date:   Wed Jan 15 13:00:45 2025 +

derive(Clone): Add Clone bound on generated impl blocks

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc: Add extra bound when deriving 
generic Clone

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc | 17 +++--
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index e914d6012b62..754d89cd84df 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -97,15 +97,15 @@ DeriveClone::clone_impl (
   auto trait_items = std::vector> ();
   trait_items.emplace_back (std::move (clone_fn));
 
-  // we need to build up the generics for this impl block which will be just a
-  // clone of the types specified ones
+  // We need to build up the generics for this impl block which will be just a
+  // clone of the generics specified, with added `Clone` bounds
   //
-  // for example:
+  // For example with:
   //
   // #[derive(Clone)]
-  // struct Be { ... }
+  // struct Be { ... }
   //
-  // we need to generate the impl block:
+  // We need to generate the following impl block:
   //
   // impl Clone for Be
 
@@ -138,7 +138,12 @@ DeriveClone::clone_impl (
  = GenericArg::create_type (std::move (associated_type));
generic_args.push_back (std::move (type_arg));
 
-   auto impl_type_param = builder.new_type_param (type_param);
+   std::vector> extra_bounds;
+   extra_bounds.emplace_back (std::unique_ptr (
+ new TraitBound (builder.type_path (LangItem::Kind::CLONE), loc)));
+
+   auto impl_type_param
+ = builder.new_type_param (type_param, std::move (extra_bounds));
impl_generics.push_back (std::move (impl_type_param));
  }
  break;


[gcc/devel/rust/master] derive(Copy): Improve bounds when deriving Copy

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:490c70374d9e98d8f9b761afe1be1a36a8ed41d0

commit 490c70374d9e98d8f9b761afe1be1a36a8ed41d0
Author: Arthur Cohen 
Date:   Thu Jan 16 14:46:06 2025 +0100

derive(Copy): Improve bounds when deriving Copy

gcc/rust/ChangeLog:

* expand/rust-derive-copy.cc: Always add an extra Copy bound on 
generic Copy impls.

Diff:
---
 gcc/rust/expand/rust-derive-copy.cc | 9 +++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-copy.cc 
b/gcc/rust/expand/rust-derive-copy.cc
index 1f52a08e7cc0..d969943d9894 100644
--- a/gcc/rust/expand/rust-derive-copy.cc
+++ b/gcc/rust/expand/rust-derive-copy.cc
@@ -52,7 +52,7 @@ DeriveCopy::copy_impl (
   // for example:
   //
   // #[derive(Copy)]
-  // struct Be { ... }
+  // struct Be { ... }
   //
   // we need to generate the impl block:
   //
@@ -87,7 +87,12 @@ DeriveCopy::copy_impl (
  = GenericArg::create_type (std::move (associated_type));
generic_args.push_back (std::move (type_arg));
 
-   auto impl_type_param = builder.new_type_param (type_param);
+   std::vector> extra_bounds;
+   extra_bounds.emplace_back (std::unique_ptr (
+ new TraitBound (builder.type_path (LangItem::Kind::COPY), loc)));
+
+   auto impl_type_param
+ = builder.new_type_param (type_param, std::move (extra_bounds));
impl_generics.push_back (std::move (impl_type_param));
  }
  break;


[gcc/devel/rust/master] typecheck: Add basic handling for applying auto trait bounds

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:53dfc6acf966284a86826141c8f364c1732d3d3d

commit 53dfc6acf966284a86826141c8f364c1732d3d3d
Author: Arthur Cohen 
Date:   Thu Jan 16 17:10:02 2025 +0100

typecheck: Add basic handling for applying auto trait bounds

gcc/rust/ChangeLog:

* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Register 
auto traits in mappings.
* util/rust-hir-map.cc (Mappings::insert_auto_trait): New.
(Mappings::get_auto_traits): New.
* util/rust-hir-map.h: Declare them.
* typecheck/rust-tyty-bounds.cc (TypeBoundsProbe::scan): Add auto 
trait bounds when
scanning.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Some parts of nr2.0 can't handle auto 
traits yet.
* rust/compile/auto_traits3.rs: Removed in favor of...
* rust/compile/auto_traits2.rs: ...this one.
* rust/compile/auto_traits4.rs: New test.

Diff:
---
 gcc/rust/hir/rust-ast-lower-item.cc| 11 +-
 gcc/rust/typecheck/rust-tyty-bounds.cc |  4 
 gcc/rust/util/rust-hir-map.cc  | 12 +++
 gcc/rust/util/rust-hir-map.h   |  6 ++
 gcc/testsuite/rust/compile/auto_traits2.rs |  5 ++---
 gcc/testsuite/rust/compile/auto_traits3.rs | 34 --
 gcc/testsuite/rust/compile/auto_traits4.rs | 14 
 gcc/testsuite/rust/compile/nr2/exclude |  3 ++-
 8 files changed, 46 insertions(+), 43 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-item.cc 
b/gcc/rust/hir/rust-ast-lower-item.cc
index 2457e919aa47..4094891dced6 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -606,17 +606,18 @@ ASTLoweringItem::visit (AST::Trait &trait)
 mappings.get_next_hir_id (crate_num),
 mappings.get_next_localdef_id (crate_num));
 
-  auto trait_unsafety = Unsafety::Normal;
-  if (trait.is_unsafe ())
-{
-  trait_unsafety = Unsafety::Unsafe;
-}
+  auto trait_unsafety
+= trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
 
   HIR::Trait *hir_trait
 = new HIR::Trait (mapping, trait.get_identifier (), trait_unsafety,
  std::move (generic_params), std::move (type_param_bounds),
  where_clause, std::move (trait_items), vis,
  trait.get_outer_attrs (), trait.get_locus ());
+
+  if (trait.is_auto ())
+mappings.insert_auto_trait (hir_trait);
+
   translated = hir_trait;
 
   for (auto trait_item_id : trait_item_ids)
diff --git a/gcc/rust/typecheck/rust-tyty-bounds.cc 
b/gcc/rust/typecheck/rust-tyty-bounds.cc
index 5d65b94150ca..1d25d4a71355 100644
--- a/gcc/rust/typecheck/rust-tyty-bounds.cc
+++ b/gcc/rust/typecheck/rust-tyty-bounds.cc
@@ -97,6 +97,10 @@ TypeBoundsProbe::scan ()
 
   // marker traits...
   assemble_sized_builtin ();
+
+  // add auto trait bounds
+  for (auto *auto_trait : mappings.get_auto_traits ())
+add_trait_bound (auto_trait);
 }
 
 void
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index bf09c94543e6..26e3ee134c14 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1309,5 +1309,17 @@ Mappings::get_lang_item_node (LangItem::Kind item_type)
LangItem::ToString (item_type).c_str ());
 }
 
+void
+Mappings::insert_auto_trait (HIR::Trait *trait)
+{
+  auto_traits.emplace_back (trait);
+}
+
+std::vector &
+Mappings::get_auto_traits ()
+{
+  return auto_traits;
+}
+
 } // namespace Analysis
 } // namespace Rust
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 9cf977a2b691..177894de9f85 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -342,6 +342,9 @@ public:
   tl::optional
   lookup_trait_item_lang_item (LangItem::Kind item, location_t locus);
 
+  void insert_auto_trait (HIR::Trait *trait);
+  std::vector &get_auto_traits ();
+
 private:
   Mappings ();
 
@@ -380,6 +383,9 @@ private:
   std::map hirTraitItemsToTraitMappings;
   std::map hirPatternMappings;
 
+  // FIXME: Add documentation
+  std::vector auto_traits;
+
   // We need to have two maps here, as lang-items need to be used for both AST
   // passes and HIR passes. Thus those two maps are created at different times.
   std::map lang_item_mappings;
diff --git a/gcc/testsuite/rust/compile/auto_traits2.rs 
b/gcc/testsuite/rust/compile/auto_traits2.rs
index 7d0dcc11cd2a..382d44608113 100644
--- a/gcc/testsuite/rust/compile/auto_traits2.rs
+++ b/gcc/testsuite/rust/compile/auto_traits2.rs
@@ -15,12 +15,11 @@ fn foo(a: &(dyn A + Send + Sync)) {
 struct S;
 
 impl A for S {
-fn a_method(&self) {}
+fn a_method(&self) {} // { dg-warning "unused name" }
 }
 
 fn main() {
 let s = S;
 
-foo(&s); // { dg-error "bounds not satisfied" }
- // { dg-error "mismatched type" "" { target *-*-* } .-1 }
+foo(&s);
 }
diff --gi

[gcc/devel/rust/master] Fix rust breakage with nr2

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:289bf52597fbe4cd85c0dca2bcb0404a960ad588

commit 289bf52597fbe4cd85c0dca2bcb0404a960ad588
Author: Pierre-Emmanuel Patry 
Date:   Mon Nov 25 18:04:06 2024 +0100

Fix rust breakage with nr2

Nr2 did not emit the correct error message for break identifier "rust".

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Add "rust"
identifier detection akin to nr1.
(funny_ice_finalizer): Copy ICE finalizer from nr1.
* resolve/rust-late-name-resolver-2.0.h: Add funny_error member
context state.
* Make-lang.in: Add new translation unit for new ice finalizer.
* resolve/rust-ast-resolve-expr.cc: Move ice
finalizer to it's own file.
* resolve/rust-ice-finalizer.cc: New file.
* resolve/rust-ice-finalizer.h: New file.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove break-rust3.rs from exclude list.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/Make-lang.in   |  1 +
 gcc/rust/resolve/rust-ast-resolve-expr.cc   | 41 +---
 gcc/rust/resolve/rust-ice-finalizer.cc  | 35 ++
 gcc/rust/resolve/rust-ice-finalizer.h   | 64 +
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 37 ++
 gcc/rust/resolve/rust-late-name-resolver-2.0.h  |  3 ++
 gcc/testsuite/rust/compile/nr2/exclude  |  1 -
 7 files changed, 141 insertions(+), 41 deletions(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 782c155fc32f..751ae874def8 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -133,6 +133,7 @@ GRS_OBJS = \
 rust/rust-toplevel-name-resolver-2.0.o \
 rust/rust-early-name-resolver-2.0.o \
rust/rust-finalize-imports-2.0.o \
+   rust/rust-ice-finalizer.o \
 rust/rust-late-name-resolver-2.0.o \
rust/rust-immutable-name-resolution-context.o \
 rust/rust-early-name-resolver.o \
diff --git a/gcc/rust/resolve/rust-ast-resolve-expr.cc 
b/gcc/rust/resolve/rust-ast-resolve-expr.cc
index 5936cf2c31ef..a74901560310 100644
--- a/gcc/rust/resolve/rust-ast-resolve-expr.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-expr.cc
@@ -22,8 +22,8 @@
 #include "rust-ast-resolve-type.h"
 #include "rust-ast-resolve-pattern.h"
 #include "rust-ast-resolve-path.h"
-#include "diagnostic.h"
 #include "rust-expr.h"
+#include "rust-ice-finalizer.h"
 
 namespace Rust {
 namespace Resolver {
@@ -108,45 +108,6 @@ ResolveExpr::visit (AST::AssignmentExpr &expr)
   ResolveExpr::go (expr.get_right_expr (), prefix, canonical_prefix);
 }
 
-/* The "break rust" Easter egg.
-
-   Backstory: once upon a time, there used to be a bug in rustc: it would ICE
-   during typechecking on a 'break' with an expression outside of a loop.  The
-   issue has been reported [0] and fixed [1], but in recognition of this, as a
-   special Easter egg, "break rust" was made to intentionally cause an ICE.
-
-   [0]: https://github.com/rust-lang/rust/issues/43162
-   [1]: https://github.com/rust-lang/rust/pull/43745
-
-   This was made in a way that does not break valid programs: namely, it only
-   happens when the 'break' is outside of a loop (so invalid anyway).
-
-   GCC Rust supports this essential feature as well, but in a slightly
-   different way.  Instead of delaying the error until type checking, we emit
-   it here in the resolution phase.  We, too, only do this to programs that
-   are already invalid: we only emit our funny ICE if the name "rust" (which
-   must be immediately inside a break-with-a-value expression) fails to
-   resolve.  Note that "break (rust)" does not trigger our ICE, only using
-   "break rust" directly does, and only if there's no "rust" in scope.  We do
-   this in the same way regardless of whether the "break" is outside of a loop
-   or inside one.
-
-   As a GNU extension, we also support "break gcc", much to the same effect,
-   subject to the same rules.  */
-
-/* The finalizer for our funny ICE.  This prints a custom message instead of
-   the default bug reporting instructions, as there is no bug to report.  */
-
-static void ATTRIBUTE_NORETURN
-funny_ice_finalizer (diagnostic_context *context,
-const diagnostic_info *diagnostic, diagnostic_t diag_kind)
-{
-  gcc_assert (diag_kind == DK_ICE_NOBT);
-  default_diagnostic_finalizer (context, diagnostic, diag_kind);
-  fnotice (stderr, "You have broken GCC Rust. This is a feature.\n");
-  exit (ICE_EXIT_CODE);
-}
-
 void
 ResolveExpr::visit (AST::IdentifierExpr &expr)
 {
diff --git a/gcc/rust/resolve/rust-ice-finalizer.cc 
b/gcc/rust/resolve/rust-ice-finalizer.cc
new file mode 100644
index ..3ec8438289ba
--- /dev/null
+++ b/gcc/rust/resolve/rust-ice-finalizer.cc
@@ -0,0 +1,35 @@
+// Copyright (C) 2020-2024 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GC

[gcc/devel/rust/master] lang-item:Add LangItem::Kind::RECEIVER

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:8a50054579daceca97cc83d02db949d85c7fdfba

commit 8a50054579daceca97cc83d02db949d85c7fdfba
Author: lishin 
Date:   Mon Jan 20 17:53:18 2025 +

lang-item:Add LangItem::Kind::RECEIVER

Add and implement a lang item (receiver) in source code.

gcc/rust/ChangeLog:

* util/rust-lang-item.cc: Add receiver to map.
* util/rust-lang-item.h: Define LangItem::Kind::RECEIVER.

gcc/testsuite/ChangeLog:

* rust/compile/issue-2954.rs: New test.

Signed-off-by: lishin 

Diff:
---
 gcc/rust/util/rust-lang-item.cc  |  1 +
 gcc/rust/util/rust-lang-item.h   |  1 +
 gcc/testsuite/rust/compile/issue-2954.rs | 17 +
 3 files changed, 19 insertions(+)

diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index 674b18919d27..e6bcc8572982 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -46,6 +46,7 @@ const BiMap 
Rust::LangItem::lang_items = {{
   {"shr_assign", Kind::SHR_ASSIGN},
   {"deref", Kind::DEREF},
   {"deref_mut", Kind::DEREF_MUT},
+  {"receiver", Kind::RECEIVER},
   {"index", Kind::INDEX},
   {"index_mut", Kind::INDEX_MUT},
   {"RangeFull", Kind::RANGE_FULL},
diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h
index f2e9d7036124..3b3d86eddf2e 100644
--- a/gcc/rust/util/rust-lang-item.h
+++ b/gcc/rust/util/rust-lang-item.h
@@ -61,6 +61,7 @@ public:
 
 DEREF,
 DEREF_MUT,
+RECEIVER,
 
 // 
https://github.com/rust-lang/rust/blob/master/library/core/src/ops/index.rs
 INDEX,
diff --git a/gcc/testsuite/rust/compile/issue-2954.rs 
b/gcc/testsuite/rust/compile/issue-2954.rs
new file mode 100644
index ..52f7c9140ecc
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2954.rs
@@ -0,0 +1,17 @@
+#[lang = "sized"]
+trait Sized {}
+
+#[lang = "receiver"]
+#[unstable(feature = "receiver_trait", issue = "none")]
+// #[doc(hidden)]
+pub trait Receiver {
+// Empty.
+}
+
+#[unstable(feature = "receiver_trait", issue = "none")]
+impl Receiver for &T {}
+
+#[unstable(feature = "receiver_trait", issue = "none")]
+impl Receiver for &mut T {}
+
+


[gcc/devel/rust/master] derive(Clone): Implement derive clone for enum struct variants

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:f027f0a605b58b495878fde922a5b2eab08d6124

commit f027f0a605b58b495878fde922a5b2eab08d6124
Author: Arthur Cohen 
Date:   Fri Jan 3 15:14:45 2025 +

derive(Clone): Implement derive clone for enum struct variants

gcc/rust/ChangeLog:

* expand/rust-derive-clone.cc (DeriveClone::clone_enum_struct): New 
function for deriving
enum struct variants.
(DeriveClone::visit_enum): Call into the new function.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude:
* rust/compile/derive_clone_enum1.rs: New test.
* rust/compile/derive_clone_enum2.rs: New test.
* rust/compile/derive_clone_enum3.rs: New test.
* rust/execute/torture/derive_clone_enum1.rs: New test.

Diff:
---
 gcc/rust/expand/rust-derive-clone.cc   | 85 --
 gcc/testsuite/rust/compile/derive_clone_enum1.rs   | 16 
 gcc/testsuite/rust/compile/derive_clone_enum2.rs   | 16 
 gcc/testsuite/rust/compile/derive_clone_enum3.rs   | 16 
 gcc/testsuite/rust/compile/nr2/exclude |  3 +
 .../rust/execute/torture/derive_clone_enum1.rs | 51 +
 6 files changed, 180 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/expand/rust-derive-clone.cc 
b/gcc/rust/expand/rust-derive-clone.cc
index 36fa52935583..e914d6012b62 100644
--- a/gcc/rust/expand/rust-derive-clone.cc
+++ b/gcc/rust/expand/rust-derive-clone.cc
@@ -19,6 +19,7 @@
 #include "rust-derive-clone.h"
 #include "rust-ast.h"
 #include "rust-ast-dump.h"
+#include "rust-expr.h"
 #include "rust-item.h"
 #include "rust-path.h"
 #include "rust-pattern.h"
@@ -300,13 +301,84 @@ DeriveClone::clone_enum_tuple (Enum &item, const 
EnumItemTuple &variant)
   return builder.match_case (std::move (pattern), std::move (expr));
 }
 
+MatchCase
+DeriveClone::clone_enum_struct (Enum &item, const EnumItemStruct &variant)
+{
+  auto variant_path = variant_match_path (item, variant.get_identifier ());
+
+  auto field_patterns = std::vector> ();
+  auto cloned_fields = std::vector> ();
+
+#if 0
+  // NOTE: We currently do not support compiling struct patterns where an
+  // identifier is assigned a new pattern, e.g. Bloop { f0: x }
+  // This is the code we should eventually produce as it mimics what rustc does
+  // - which is probably here for a good reason. In the meantime, we can just
+  // use the field's identifier as the pattern: Bloop { f0 }
+  // We can then clone the field directly instead of calling `clone()` on the
+  // new pattern.
+  // TODO: Figure out if that is actually needed and why rustc does it?
+
+  for (size_t i = 0; i < variant.get_struct_fields ().size (); i++)
+{
+  auto &field = variant.get_struct_fields ()[i];
+
+  // Just like for tuples, the pattern we're creating for each field is
+  // `self_` where `i` is the index of the field. It doesn't actually
+  // matter what we use, as long as it's ordered, unique, and that we can
+  // reuse it in the match case's return expression to clone the field.
+  auto pattern_str = "__self_" + std::to_string (i);
+
+  field_patterns.emplace_back (
+   std::unique_ptr (new StructPatternFieldIdentPat (
+ field.get_field_name (), builder.identifier_pattern (pattern_str), {},
+ loc)));
+
+  cloned_fields.emplace_back (
+   std::unique_ptr (new StructExprFieldIdentifierValue (
+ field.get_field_name (),
+ clone_call (builder.ref (builder.identifier (pattern_str))), {},
+ loc)));
+}
+#endif
+
+  for (const auto &field : variant.get_struct_fields ())
+{
+  // We match on the struct's fields, and then recreate an instance of that
+  // struct, cloning each field
+
+  field_patterns.emplace_back (
+   std::unique_ptr (new StructPatternFieldIdent (
+ field.get_field_name (), false /* is_ref? true? */, false, {}, loc)));
+
+  cloned_fields.emplace_back (
+   std::unique_ptr (new StructExprFieldIdentifierValue (
+ field.get_field_name (),
+ clone_call (builder.ref (
+   builder.identifier (field.get_field_name ().as_string (,
+ {}, loc)));
+}
+
+  auto pattern_elts = StructPatternElements (std::move (field_patterns));
+
+  auto pattern = std::unique_ptr (
+new ReferencePattern (std::unique_ptr (new StructPattern (
+   variant_path, loc, pattern_elts)),
+ false, false, loc));
+  auto expr = std::unique_ptr (
+new StructExprStructFields (variant_path, std::move (cloned_fields), loc));
+
+  return builder.match_case (std::move (pattern), std::move (expr));
+}
+
 void
 DeriveClone::visit_enum (Enum &item)
 {
-  // Create an arm for each variant of the enum
-  // For enum item variants, just create the same variant
-  // For struct and tuple variants, destructure the pattern and call clone for
-  // each field
+  // Create an arm for each variant of the enum:

[gcc/devel/rust/master] hir: Adapt visitors to lang item PathInExpressions

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:a7d273ca770382b941453a00f579abdf3e606f4a

commit a7d273ca770382b941453a00f579abdf3e606f4a
Author: Arthur Cohen 
Date:   Wed Jan 22 12:36:59 2025 +

hir: Adapt visitors to lang item PathInExpressions

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (ResolvePathRef::visit): 
Adapt visitor to lang item
HIR::PathInExpressions.
* typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::visit): 
Likewise.

Diff:
---
 gcc/rust/backend/rust-compile-resolve-path.cc  | 22 ++--
 gcc/rust/typecheck/rust-hir-type-check-path.cc | 78 ++
 2 files changed, 85 insertions(+), 15 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 74e95e545d77..3af3431f8490 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -35,15 +35,29 @@ namespace Compile {
 void
 ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
 {
-  resolved = resolve (expr.get_final_segment ().get_segment (),
- expr.get_mappings (), expr.get_locus (), true);
+  auto final_segment = HIR::PathIdentSegment::create_error ();
+  if (expr.is_lang_item ())
+final_segment
+  = HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
+  else
+final_segment = expr.get_final_segment ().get_segment ();
+
+  resolved
+= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
 }
 
 void
 ResolvePathRef::visit (HIR::PathInExpression &expr)
 {
-  resolved = resolve (expr.get_final_segment ().get_segment (),
- expr.get_mappings (), expr.get_locus (), false);
+  auto final_segment = HIR::PathIdentSegment::create_error ();
+  if (expr.is_lang_item ())
+final_segment
+  = HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
+  else
+final_segment = expr.get_final_segment ().get_segment ();
+
+  resolved
+= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
 }
 
 tree
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc 
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index 9e156725d5b8..5f05deb4bc20 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -16,6 +16,9 @@
 // along with GCC; see the file COPYING3.  If not see
 // .
 
+#include "rust-diagnostics.h"
+#include "rust-hir-map.h"
+#include "rust-hir-path.h"
 #include "rust-hir-type-check-expr.h"
 #include "rust-hir-type-check-type.h"
 #include "rust-hir-type-check-item.h"
@@ -24,6 +27,7 @@
 #include "rust-hir-path-probe.h"
 #include "rust-type-util.h"
 #include "rust-hir-type-bounds.h"
+#include "rust-hir-item.h"
 #include "rust-session-manager.h"
 #include "rust-immutable-name-resolution-context.h"
 
@@ -179,20 +183,72 @@ void
 TypeCheckExpr::visit (HIR::PathInExpression &expr)
 {
   NodeId resolved_node_id = UNKNOWN_NODEID;
-  size_t offset = -1;
-  TyTy::BaseType *tyseg = resolve_root_path (expr, &offset, &resolved_node_id);
-  if (tyseg->get_kind () == TyTy::TypeKind::ERROR)
-return;
-
-  bool fully_resolved = offset == expr.get_segments ().size ();
-  if (fully_resolved)
+  if (expr.is_lang_item ())
 {
-  infered = tyseg;
-  return;
+  auto lookup
+   = Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ());
+  auto hir_id = mappings.lookup_node_to_hir (lookup);
+
+  // We can type resolve the path in expression easily as it is a lang
+  // item path, but we still need to setup the various generics and
+  // substitutions
+
+  // FIXME: We probably need to check *if* the type needs substitutions
+  // or not
+  if (LangItem::IsEnumVariant (expr.get_lang_item ()))
+   {
+ std::pair enum_item_lookup
+   = mappings.lookup_hir_enumitem (*hir_id);
+ bool enum_item_ok = enum_item_lookup.first != nullptr
+ && enum_item_lookup.second != nullptr;
+ rust_assert (enum_item_ok);
+
+ HirId variant_id
+   = enum_item_lookup.second->get_mappings ().get_hirid ();
+
+ HIR::EnumItem *enum_item = enum_item_lookup.second;
+ resolved_node_id = enum_item->get_mappings ().get_nodeid ();
+
+ // insert the id of the variant we are resolved to
+ context->insert_variant_definition (expr.get_mappings ().get_hirid (),
+ variant_id);
+
+ query_type (variant_id, &infered);
+ infered = SubstMapper::InferSubst (infered, expr.get_locus ());
+   }
+  else
+   {
+ TyTy::BaseType *resolved = nullptr;
+ context->lookup_type (*hir_id, &resolved);
+
+ rust_assert (resolved);
+
+ query_type (*hir_id, &infered);
+
+ infered = SubstMapper::InferSubst (resolved, expr.get_locus ());
+   }
+
+

[gcc/devel/rust/master] mappings: Improve error message for get_lang_item_node

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:e6a2ce133f88bb5ba9a8a146170a9bbd3e9d77ed

commit e6a2ce133f88bb5ba9a8a146170a9bbd3e9d77ed
Author: Arthur Cohen 
Date:   Wed Jan 22 12:37:58 2025 +

mappings: Improve error message for get_lang_item_node

gcc/rust/ChangeLog:

* util/rust-hir-map.cc (Mappings::get_lang_item_node): Better 
formatting when a lang
item does not exist when it should.

Diff:
---
 gcc/rust/util/rust-hir-map.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 4d2927281a21..99839eb6701e 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1305,8 +1305,8 @@ Mappings::get_lang_item_node (LangItem::Kind item_type)
   if (auto lookup = lookup_lang_item_node (item_type))
 return *lookup;
 
-  rust_fatal_error (UNKNOWN_LOCATION, "failed to find lang item %qs",
-   LangItem::ToString (item_type).c_str ());
+  rust_fatal_error (UNKNOWN_LOCATION, "undeclared lang item: %qs",
+   LangItem::PrettyString (item_type).c_str ());
 }
 
 void


[gcc/devel/rust/master] Add environment capture to NR2

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:2b473162d33e0f3fe31a7f098c745cb388a01aa4

commit 2b473162d33e0f3fe31a7f098c745cb388a01aa4
Author: Pierre-Emmanuel Patry 
Date:   Mon Jan 20 13:49:25 2025 +0100

Add environment capture to NR2

The compiler was still relying on NR1 for closure captures when using nr2
even though the resolver was not used and thus it's state empty.

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Add 
environment
collection.
* resolve/rust-late-name-resolver-2.0.h: Add function prototype.
* resolve/rust-name-resolver.cc (Resolver::get_captures): Add 
assertion
to prevent NR2 usage with nr1 capture functions.
* typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Use
nr2 captures.
* util/rust-hir-map.cc (Mappings::add_capture): Add function to
register capture for a given closure.
(Mappings::lookup_captures):  Add a function to lookup all captures
available for a given closure.
* util/rust-hir-map.h: Add function prototypes.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 13 +
 gcc/rust/resolve/rust-late-name-resolver-2.0.h  |  2 ++
 gcc/rust/resolve/rust-name-resolver.cc  |  2 ++
 gcc/rust/typecheck/rust-hir-type-check-expr.cc  | 20 +++-
 gcc/rust/util/rust-hir-map.cc   | 20 
 gcc/rust/util/rust-hir-map.h|  5 +
 6 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index 7c6948565202..1e7f9f1546cf 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -390,5 +390,18 @@ Late::visit (AST::GenericArg &arg)
   DefaultResolver::visit (arg);
 }
 
+void
+Late::visit (AST::ClosureExprInner &closure)
+{
+  auto vals = ctx.values.peek ().get_values ();
+  for (auto &val : vals)
+{
+  ctx.mappings.add_capture (closure.get_node_id (),
+   val.second.get_node_id ());
+}
+
+  DefaultResolver::visit (closure);
+}
+
 } // namespace Resolver2_0
 } // namespace Rust
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
index bdaae143e73d..3030261f10bf 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
@@ -21,6 +21,7 @@
 
 #include "rust-ast-full.h"
 #include "rust-default-resolver.h"
+#include "rust-expr.h"
 
 namespace Rust {
 namespace Resolver2_0 {
@@ -55,6 +56,7 @@ public:
   void visit (AST::StructStruct &) override;
   void visit (AST::GenericArgs &) override;
   void visit (AST::GenericArg &);
+  void visit (AST::ClosureExprInner &) override;
 
 private:
   /* Setup Rust's builtin types (u8, i32, !...) in the resolver */
diff --git a/gcc/rust/resolve/rust-name-resolver.cc 
b/gcc/rust/resolve/rust-name-resolver.cc
index 6b131ad374d5..31da593b86c8 100644
--- a/gcc/rust/resolve/rust-name-resolver.cc
+++ b/gcc/rust/resolve/rust-name-resolver.cc
@@ -674,6 +674,8 @@ Resolver::decl_needs_capture (NodeId decl_rib_node_id,
 const std::set &
 Resolver::get_captures (NodeId id) const
 {
+  rust_assert (!flag_name_resolution_2_0);
+
   auto it = closures_capture_mappings.find (id);
   rust_assert (it != closures_capture_mappings.end ());
   return it->second;
diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.cc 
b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
index 2554a72dc2a7..356a960f3174 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-expr.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-expr.cc
@@ -16,6 +16,7 @@
 // along with GCC; see the file COPYING3.  If not see
 // .
 
+#include "rust-system.h"
 #include "rust-tyty-call.h"
 #include "rust-hir-type-check-struct-field.h"
 #include "rust-hir-path-probe.h"
@@ -1599,7 +1600,24 @@ TypeCheckExpr::visit (HIR::ClosureExpr &expr)
 
   // generate the closure type
   NodeId closure_node_id = expr.get_mappings ().get_nodeid ();
-  const std::set &captures = resolver->get_captures (closure_node_id);
+
+  // Resolve closure captures
+
+  std::set captures;
+  if (flag_name_resolution_2_0)
+{
+  auto &nr_ctx = const_cast (
+   Resolver2_0::ImmutableNameResolutionContext::get ().resolver ());
+
+  if (auto opt_cap = nr_ctx.mappings.lookup_captures (closure_node_id))
+   for (auto cap : opt_cap.value ())
+ captures.insert (cap);
+}
+  else
+{
+  captures = resolver->get_captures (closure_node_id);
+}
+
   infered = new TyTy::ClosureType (ref, id, ident, closure_args, result_type,
   subst_refs, captures);
 
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
inde

[gcc/devel/rust/master] Infer crate name after file opening

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:a3e147802e38ec7fc027d4b4116e110ac0a0d7b8

commit a3e147802e38ec7fc027d4b4116e110ac0a0d7b8
Author: Dylan Gardner 
Date:   Thu Aug 29 04:43:42 2024 -0700

Infer crate name after file opening

Fixes #3129.

gcc/rust/ChangeLog:

* rust-session-manager.cc (Session::handle_crate_name): Remove
crate name inference
(Session::compile_crate): Add crate name inference and error if
inferred name is empty. Remove CompileOptions::get_instance ()
that returned a local copy of the options. Rename
crate_name_changed to crate_name_found to match semantics.
(rust_crate_name_validation_test): Test inferring ".rs" name
* rust-session-manager.h: Modify handle_crate_name definition to
include filename.

Diff:
---
 gcc/rust/rust-session-manager.cc | 61 ++--
 gcc/rust/rust-session-manager.h  |  2 +-
 2 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 7d61ad4c770a..b4b95167c528 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -401,31 +401,16 @@ Session::handle_input_files (int num_files, const char 
**files)
 
   const auto &file = files[0];
 
-  if (options.crate_name.empty ())
-{
-  auto filename = "-";
-  if (num_files > 0)
-   filename = files[0];
-
-  auto crate_name = infer_crate_name (filename);
-  rust_debug ("inferred crate name: %s", crate_name.c_str ());
-  // set the preliminary crate name here
-  // we will figure out the real crate name in `handle_crate_name`
-  options.set_crate_name (crate_name);
-}
-
-  CrateNum crate_num = mappings.get_next_crate_num (options.get_crate_name ());
-  mappings.set_current_crate (crate_num);
-
   rust_debug ("Attempting to parse file: %s", file);
   compile_crate (file);
 }
 
 void
-Session::handle_crate_name (const AST::Crate &parsed_crate)
+Session::handle_crate_name (const char *filename,
+   const AST::Crate &parsed_crate)
 {
   auto &mappings = Analysis::Mappings::get ();
-  auto crate_name_changed = false;
+  auto crate_name_found = false;
   auto error = Error (UNDEF_LOCATION, std::string ());
 
   for (const auto &attr : parsed_crate.inner_attrs)
@@ -449,7 +434,6 @@ Session::handle_crate_name (const AST::Crate &parsed_crate)
  continue;
}
 
-  auto options = Session::get_instance ().options;
   if (options.crate_name_set_manually && (options.crate_name != msg_str))
{
  rust_error_at (attr.get_locus (),
@@ -457,19 +441,39 @@ Session::handle_crate_name (const AST::Crate 
&parsed_crate)
 "required to match, but %qs does not match %qs",
 options.crate_name.c_str (), msg_str.c_str ());
}
-  crate_name_changed = true;
+  crate_name_found = true;
   options.set_crate_name (msg_str);
-  mappings.set_crate_name (mappings.get_current_crate (), msg_str);
 }
 
-  options.crate_name_set_manually |= crate_name_changed;
-  if (!options.crate_name_set_manually
-  && !validate_crate_name (options.crate_name, error))
+  options.crate_name_set_manually |= crate_name_found;
+  if (!options.crate_name_set_manually)
 {
-  error.emit ();
-  rust_inform (linemap_position_for_column (line_table, 0),
-  "crate name inferred from this file");
+  auto crate_name = infer_crate_name (filename);
+  if (crate_name.empty ())
+   {
+ rust_error_at (UNDEF_LOCATION, "crate name is empty");
+ rust_inform (linemap_position_for_column (line_table, 0),
+  "crate name inferred from this file");
+ return;
+   }
+
+  rust_debug ("inferred crate name: %s", crate_name.c_str ());
+  options.set_crate_name (crate_name);
+
+  if (!validate_crate_name (options.get_crate_name (), error))
+   {
+ error.emit ();
+ rust_inform (linemap_position_for_column (line_table, 0),
+  "crate name inferred from this file");
+ return;
+   }
 }
+
+  if (saw_errors ())
+return;
+
+  CrateNum crate_num = mappings.get_next_crate_num (options.get_crate_name ());
+  mappings.set_current_crate (crate_num);
 }
 
 // Parses a single file with filename filename.
@@ -539,7 +543,7 @@ Session::compile_crate (const char *filename)
   std::unique_ptr ast_crate = parser.parse_crate ();
 
   // handle crate name
-  handle_crate_name (*ast_crate.get ());
+  handle_crate_name (filename, *ast_crate.get ());
 
   // dump options except lexer dump
   if (options.dump_option_enabled (CompileOptions::AST_DUMP_PRETTY))
@@ -1400,6 +1404,7 @@ rust_crate_name_validation_test (void)
   ASSERT_FALSE (Rust::validate_crate_name ("∀", error));
 
   /* Tests for crate name inference */
+  ASSERT_EQ (Rust::infer_crate_name (".

[gcc/devel/rust/master] lang-items: Collect enum variants as lang items

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:33d901d12755e16ee70ace5c9afc07e9aac0c4af

commit 33d901d12755e16ee70ace5c9afc07e9aac0c4af
Author: Arthur Cohen 
Date:   Mon Dec 23 10:27:13 2024 +

lang-items: Collect enum variants as lang items

gcc/rust/ChangeLog:

* ast/rust-collect-lang-items.h: Declare visitor.
* ast/rust-collect-lang-items.cc (CollectLangItems::visit): New.

Diff:
---
 gcc/rust/ast/rust-collect-lang-items.cc | 9 +
 gcc/rust/ast/rust-collect-lang-items.h  | 1 +
 2 files changed, 10 insertions(+)

diff --git a/gcc/rust/ast/rust-collect-lang-items.cc 
b/gcc/rust/ast/rust-collect-lang-items.cc
index 11c3297d2a9d..cd6be7fbeb9c 100644
--- a/gcc/rust/ast/rust-collect-lang-items.cc
+++ b/gcc/rust/ast/rust-collect-lang-items.cc
@@ -19,6 +19,7 @@
 #include "rust-collect-lang-items.h"
 #include "optional.h"
 #include "rust-ast-collector.h"
+#include "rust-ast-visitor.h"
 #include "rust-ast.h"
 #include "rust-attribute-values.h"
 #include "rust-attributes.h"
@@ -100,5 +101,13 @@ CollectLangItems::visit (AST::StructStruct &item)
   DefaultASTVisitor::visit (item);
 }
 
+void
+CollectLangItems::visit (AST::EnumItem &item)
+{
+  maybe_add_lang_item (item);
+
+  DefaultASTVisitor::visit (item);
+}
+
 } // namespace AST
 } // namespace Rust
diff --git a/gcc/rust/ast/rust-collect-lang-items.h 
b/gcc/rust/ast/rust-collect-lang-items.h
index 39cb4be31a0c..ddb34a914ed7 100644
--- a/gcc/rust/ast/rust-collect-lang-items.h
+++ b/gcc/rust/ast/rust-collect-lang-items.h
@@ -49,6 +49,7 @@ public:
   void visit (AST::TraitItemType &item) override;
   void visit (AST::Function &item) override;
   void visit (AST::StructStruct &item) override;
+  void visit (AST::EnumItem &item) override;
 
 private:
   template  void maybe_add_lang_item (const T &item);


[gcc/devel/rust/master] Update upload-artifact to v4

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:dd750c98ceab1d511587edbceb78dbc5b7cfb15c

commit dd750c98ceab1d511587edbceb78dbc5b7cfb15c
Author: Owen Avery 
Date:   Thu Jan 23 12:34:01 2025 -0500

Update upload-artifact to v4

ChangeLog:

* .github/workflows/ccpp.yml: Update actions/upload-artifact
from v3 to v4, handle any artifact name conflicts.
* .github/workflows/ccpp32alpine.yml: Likewise.

Signed-off-by: Owen Avery 

Diff:
---
 .github/workflows/ccpp.yml | 20 ++--
 .github/workflows/ccpp32alpine.yml |  4 ++--
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml
index 4e9a629fb027..bdc2d8df3e5e 100644
--- a/.github/workflows/ccpp.yml
+++ b/.github/workflows/ccpp.yml
@@ -80,9 +80,9 @@ jobs:
cd gccrs-build; \
make check-rust RUNTESTFLAGS="--target_board=unix\{-m64}"
 - name: Archive check-rust results
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
-name: check-rust-logs
+name: check-rust-logs-ubuntu-64
 path: |
   gccrs-build/gcc/testsuite/rust/
 - name: Check regressions
@@ -165,9 +165,9 @@ jobs:
cd gccrs-build; \
make check-rust RUNTESTFLAGS="--target_board=unix\{-m64}"
 - name: Archive check-rust results
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
-name: check-rust-logs
+name: check-rust-logs-ubuntu-64-glibcxx
 path: |
   gccrs-build/gcc/testsuite/rust/
 - name: Check regressions
@@ -249,9 +249,9 @@ jobs:
cd gccrs-build; \
make check-rust RUNTESTFLAGS="--target_board=unix\{-m32}"
 - name: Archive check-rust results
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
-name: check-rust-logs
+name: check-rust-logs-ubuntu-32
 path: |
   gccrs-build/gcc/testsuite/rust/
 - name: Check regressions
@@ -350,7 +350,7 @@ jobs:
  make check-rust RUNTESTFLAGS="--target_board=unix\{-m32,-m64}"
 
 - name: Archive check-rust results
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
 name: check-rust-logs-5
 path: |
@@ -410,7 +410,7 @@ jobs:
   #  cd gccrs-build; \
   #  make check-rust
   #   - name: Archive check-rust results
-  # uses: actions/upload-artifact@v3
+  # uses: actions/upload-artifact@v4
   # with:
   #   name: check-rust-logs-macos
   #   path: |
@@ -496,9 +496,9 @@ jobs:
ASAN_OPTIONS=detect_leaks=0:use_odr_indicator=1 \
make check-rust RUNTESTFLAGS="--target_board=unix\{-m64}"
 - name: Archive check-rust results
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
-name: check-rust-logs
+name: check-rust-logs-asan
 path: |
   gccrs-build/gcc/testsuite/rust/
 - name: Check regressions
diff --git a/.github/workflows/ccpp32alpine.yml 
b/.github/workflows/ccpp32alpine.yml
index d4f6e04faac0..e2817d4f6cde 100644
--- a/.github/workflows/ccpp32alpine.yml
+++ b/.github/workflows/ccpp32alpine.yml
@@ -84,7 +84,7 @@ jobs:
cat log_warnings
   shell: alpine.sh {0}
 - name: Archive warnings logs
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
 name: warnings 
 path: |
@@ -108,7 +108,7 @@ jobs:
   shell: alpine.sh {0}
 
 - name: Archive check-rust results
-  uses: actions/upload-artifact@v3
+  uses: actions/upload-artifact@v4
   with:
 name: check-rust-logs
 path: |


[gcc/devel/rust/master] gccrs: catch missing guard for optional result

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:60e0d0453fbddef7cf8495f896f9ac189f4d7e17

commit 60e0d0453fbddef7cf8495f896f9ac189f4d7e17
Author: Philip Herron 
Date:   Tue Jan 21 17:19:13 2025 +

gccrs: catch missing guard for optional result

When we lookup here it returns an optional which can lead to a crash
because it needs a guard if it has a value.

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc 
(HIRCompileBase::query_compile): add guard

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: these tests now work it seems

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/backend/rust-compile-resolve-path.cc | 7 ++-
 gcc/testsuite/rust/compile/nr2/exclude| 2 --
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 049b0d86b523..74e95e545d77 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -248,12 +248,9 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
return CompileInherentImplItem::Compile (resolved_item->first, ctx,
 lookup, true, expr_locus);
}
-  else
+  else if (auto trait_item
+  = ctx->get_mappings ().lookup_hir_trait_item (ref))
{
- // it might be resolved to a trait item
- tl::optional trait_item
-   = ctx->get_mappings ().lookup_hir_trait_item (ref);
-
  HIR::Trait *trait = ctx->get_mappings ().lookup_trait_item_mapping (
trait_item.value ()->get_mappings ().get_hirid ());
 
diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index 5cc7cf7d64c5..8bcc8aed0306 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -63,7 +63,6 @@ macros/mbe/macro23.rs
 macros/mbe/macro40.rs
 macros/mbe/macro43.rs
 macros/mbe/macro44.rs
-macros/mbe/macro50.rs
 macros/mbe/macro54.rs
 macros/mbe/macro6.rs
 macros/mbe/macro_rules_macro_rules.rs
@@ -141,7 +140,6 @@ issue-2423.rs
 issue-266.rs
 additional-trait-bounds2.rs
 auto_traits2.rs
-auto_traits4.rs
 issue-3140.rs
 cmp1.rs
 derive_clone_enum1.rs


[gcc/devel/rust/master] Add captures for ClosureExprInnerTyped with nr2

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:98d89d58d7c6b25fc6e29735c3b6d051fd45ae8a

commit 98d89d58d7c6b25fc6e29735c3b6d051fd45ae8a
Author: Pierre-Emmanuel Patry 
Date:   Fri Jan 24 16:02:10 2025 +0100

Add captures for ClosureExprInnerTyped with nr2

Captures were only processed for regular ClosureExprInner.

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Add
ClosureExprInnerTyped visit implementation.
(add_captures): Add a function to avoid code duplication.
* resolve/rust-late-name-resolver-2.0.h: Add function prototype.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 17 +++--
 gcc/rust/resolve/rust-late-name-resolver-2.0.h  |  1 +
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index 1e7f9f1546cf..b0364d10725c 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -390,8 +390,9 @@ Late::visit (AST::GenericArg &arg)
   DefaultResolver::visit (arg);
 }
 
-void
-Late::visit (AST::ClosureExprInner &closure)
+template 
+static void
+add_captures (Closure &closure, NameResolutionContext &ctx)
 {
   auto vals = ctx.values.peek ().get_values ();
   for (auto &val : vals)
@@ -399,7 +400,19 @@ Late::visit (AST::ClosureExprInner &closure)
   ctx.mappings.add_capture (closure.get_node_id (),
val.second.get_node_id ());
 }
+}
 
+void
+Late::visit (AST::ClosureExprInner &closure)
+{
+  add_captures (closure, ctx);
+  DefaultResolver::visit (closure);
+}
+
+void
+Late::visit (AST::ClosureExprInnerTyped &closure)
+{
+  add_captures (closure, ctx);
   DefaultResolver::visit (closure);
 }
 
diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
index 3030261f10bf..c77993596243 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h
@@ -57,6 +57,7 @@ public:
   void visit (AST::GenericArgs &) override;
   void visit (AST::GenericArg &);
   void visit (AST::ClosureExprInner &) override;
+  void visit (AST::ClosureExprInnerTyped &) override;
 
 private:
   /* Setup Rust's builtin types (u8, i32, !...) in the resolver */


[gcc/devel/rust/master] ast: Add Path::is_lang_item()

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:9f2c6de83509ae6ca1679a66b0b1be158386fc4e

commit 9f2c6de83509ae6ca1679a66b0b1be158386fc4e
Author: Arthur Cohen 
Date:   Wed Jan 22 16:52:26 2025 +

ast: Add Path::is_lang_item()

gcc/rust/ChangeLog:

* ast/rust-path.h: New function.

Diff:
---
 gcc/rust/ast/rust-path.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index c5afc8f883d4..0ecd822e50ee 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -622,6 +622,8 @@ public:
 
   std::string as_string () const override;
 
+  bool is_lang_item () const { return kind == Kind::LangItem; }
+
   // TODO: this seems kinda dodgy
   std::vector &get_segments ()
   {


[gcc/devel/rust/master] backend: Improve handling of lang-item PathInExpressions

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:6a2346279bb49a8c8cd81496186c6c98764614e7

commit 6a2346279bb49a8c8cd81496186c6c98764614e7
Author: Arthur Cohen 
Date:   Fri Jan 24 10:42:54 2025 +0100

backend: Improve handling of lang-item PathInExpressions

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (ResolvePathRef::visit): 
Call into
resolve_path_like instead.
(ResolvePathRef::resolve_path_like): New.
(ResolvePathRef::resolve): Call into resolve_with_node_id.
* backend/rust-compile-resolve-path.h: Declare new functions and 
document them.

Diff:
---
 gcc/rust/backend/rust-compile-resolve-path.cc | 116 +++---
 gcc/rust/backend/rust-compile-resolve-path.h  |  16 
 2 files changed, 84 insertions(+), 48 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 3af3431f8490..d4e8fbe3b99d 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -32,32 +32,37 @@
 namespace Rust {
 namespace Compile {
 
-void
-ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
+template 
+tree
+ResolvePathRef::resolve_path_like (T &expr)
 {
-  auto final_segment = HIR::PathIdentSegment::create_error ();
   if (expr.is_lang_item ())
-final_segment
-  = HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
-  else
-final_segment = expr.get_final_segment ().get_segment ();
+{
+  auto lang_item
+   = Analysis::Mappings::get ().get_lang_item_node (expr.get_lang_item ());
+
+  // FIXME: Is that correct? :/
+  auto final_segment
+   = HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
 
-  resolved
-= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
+  return resolve_with_node_id (final_segment, expr.get_mappings (),
+  expr.get_locus (), true, lang_item);
+}
+
+  return resolve (expr.get_final_segment ().get_segment (),
+ expr.get_mappings (), expr.get_locus (), true);
 }
 
 void
-ResolvePathRef::visit (HIR::PathInExpression &expr)
+ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
 {
-  auto final_segment = HIR::PathIdentSegment::create_error ();
-  if (expr.is_lang_item ())
-final_segment
-  = HIR::PathIdentSegment (LangItem::ToString (expr.get_lang_item ()));
-  else
-final_segment = expr.get_final_segment ().get_segment ();
+  resolved = resolve_path_like (expr);
+}
 
-  resolved
-= resolve (final_segment, expr.get_mappings (), expr.get_locus (), true);
+void
+ResolvePathRef::visit (HIR::PathInExpression &expr)
+{
+  resolved = resolve_path_like (expr);
 }
 
 tree
@@ -106,42 +111,17 @@ ResolvePathRef::attempt_constructor_expression_lookup (
 }
 
 tree
-ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
-const Analysis::NodeMapping &mappings,
-location_t expr_locus, bool is_qualified_path)
+ResolvePathRef::resolve_with_node_id (
+  const HIR::PathIdentSegment &final_segment,
+  const Analysis::NodeMapping &mappings, location_t expr_locus,
+  bool is_qualified_path, NodeId resolved_node_id)
 {
   TyTy::BaseType *lookup = nullptr;
   bool ok = ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &lookup);
   rust_assert (ok);
 
-  // need to look up the reference for this identifier
-
-  // this can fail because it might be a Constructor for something
-  // in that case the caller should attempt ResolvePathType::Compile
-  NodeId ref_node_id = UNKNOWN_NODEID;
-  if (flag_name_resolution_2_0)
-{
-  auto nr_ctx
-   = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
-
-  auto resolved = nr_ctx.lookup (mappings.get_nodeid ());
-
-  if (!resolved)
-   return attempt_constructor_expression_lookup (lookup, ctx, mappings,
- expr_locus);
-
-  ref_node_id = *resolved;
-}
-  else
-{
-  if (!ctx->get_resolver ()->lookup_resolved_name (mappings.get_nodeid (),
-  &ref_node_id))
-   return attempt_constructor_expression_lookup (lookup, ctx, mappings,
- expr_locus);
-}
-
   tl::optional hid
-= ctx->get_mappings ().lookup_node_to_hir (ref_node_id);
+= ctx->get_mappings ().lookup_node_to_hir (resolved_node_id);
   if (!hid.has_value ())
 {
   rust_error_at (expr_locus, "reverse call path lookup failure");
@@ -207,9 +187,49 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment 
&final_segment,
 {
   TREE_USED (resolved_item) = 1;
 }
+
   return resolved_item;
 }
 
+tree
+ResolvePathRef::resolve (const HIR::PathIdentSegment &final_segment,
+const Analysis::NodeMapping &mappings,
+location_t expr_lo

[gcc r15-8932] testsuite: i386: Fix c-c++-common/gomp/metadirective-device.c etc. with i?86 compiler

2025-03-26 Thread Rainer Orth via Gcc-cvs
https://gcc.gnu.org/g:acc1ea0cbfb125658ca1d7488e5b1e5e3ae3dee2

commit r15-8932-gacc1ea0cbfb125658ca1d7488e5b1e5e3ae3dee2
Author: Rainer Orth 
Date:   Wed Mar 26 14:52:19 2025 +0100

testsuite: i386: Fix c-c++-common/gomp/metadirective-device.c etc. with 
i?86 compiler

Two new tests FAIL on both Solaris/x86 and Linux/i686 with a
32-bit-default compiler:

FAIL: c-c++-common/gomp/metadirective-device.c -std=c++17
scan-tree-dump-not optimized "__builtin_GOMP_error"
FAIL: c-c++-common/gomp/metadirective-device.c -std=c++26
scan-tree-dump-not optimized "__builtin_GOMP_error"
FAIL: c-c++-common/gomp/metadirective-device.c -std=c++98
scan-tree-dump-not optimized "__builtin_GOMP_error"
FAIL: c-c++-common/gomp/metadirective-target-device-1.c -std=c++17
scan-tree-dump-times optimized "GOMP_error" 0
FAIL: c-c++-common/gomp/metadirective-target-device-1.c -std=c++26
scan-tree-dump-times optimized "GOMP_error" 0
FAIL: c-c++-common/gomp/metadirective-target-device-1.c -std=c++98
scan-tree-dump-times optimized "GOMP_error" 0

FAIL: c-c++-common/gomp/metadirective-device.c scan-tree-dump-not optimized
"__builtin_GOMP_error"
FAIL: c-c++-common/gomp/metadirective-target-device-1.c
scan-tree-dump-times optimized "GOMP_error" 0

They also FAIL on Linux/x86_64 with -mx32.

The problem is two-fold: restricting a test to target x86_64-*-* is
always wrong: an i?86-*-* compiler can produce 64-bit code with -m64
just as well, so it should always be both.

In addition, the -mx32 failure shows that the test seems to be 64-bit
only.

To fix both issues, this patch uses the new x86 effective-target keyword
and restricts the tests to lp64 instead of ! ia32.

Tested on i386-pc-solaris2.11 and x86_64-pc-linux-gnu.

2025-03-25  Rainer Orth  

gcc/testsuite:
* c-c++-common/gomp/metadirective-device.c
(dg-additional-options): Use on all x86 targets.  Restrict to lp64.
* c-c++-common/gomp/metadirective-target-device-1.c: Likewise.

Diff:
---
 gcc/testsuite/c-c++-common/gomp/metadirective-device.c  | 2 +-
 gcc/testsuite/c-c++-common/gomp/metadirective-target-device-1.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/c-c++-common/gomp/metadirective-device.c 
b/gcc/testsuite/c-c++-common/gomp/metadirective-device.c
index 380762477b07..d7f736d70b3e 100644
--- a/gcc/testsuite/c-c++-common/gomp/metadirective-device.c
+++ b/gcc/testsuite/c-c++-common/gomp/metadirective-device.c
@@ -1,6 +1,6 @@
 /* { dg-do compile }  */
 /* { dg-additional-options "-foffload=disable -fdump-tree-optimized" } */
-/* { dg-additional-options "-DDEVICE_ARCH=x86_64 -DDEVICE_ISA=sse -msse" { 
target { x86_64-*-* && { ! ia32 } } } } */
+/* { dg-additional-options "-DDEVICE_ARCH=x86_64 -DDEVICE_ISA=sse -msse" { 
target { x86 && lp64 } } } */
 
 #include 
 
diff --git a/gcc/testsuite/c-c++-common/gomp/metadirective-target-device-1.c 
b/gcc/testsuite/c-c++-common/gomp/metadirective-target-device-1.c
index 5d3a4c3ff9be..284f35f6d847 100644
--- a/gcc/testsuite/c-c++-common/gomp/metadirective-target-device-1.c
+++ b/gcc/testsuite/c-c++-common/gomp/metadirective-target-device-1.c
@@ -1,6 +1,6 @@
 /* { dg-do compile }  */
 /* { dg-additional-options "-fdump-tree-optimized" } */
-/* { dg-additional-options "-DDEVICE_ARCH=x86_64 -DDEVICE_ISA=mmx -mmmx" { 
target { x86_64-*-* && { ! ia32 } } } } */
+/* { dg-additional-options "-DDEVICE_ARCH=x86_64 -DDEVICE_ISA=mmx -mmmx" { 
target { x86 && lp64 } } } */
 
 #include 


[gcc/devel/rust/master] hir: Add LangItem paths to PathPattern class

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:068e33b4f8724c76c9515e890ec064dd49faa0f1

commit 068e33b4f8724c76c9515e890ec064dd49faa0f1
Author: Arthur Cohen 
Date:   Mon Dec 16 13:01:13 2024 +0100

hir: Add LangItem paths to PathPattern class

gcc/rust/ChangeLog:

* hir/tree/rust-hir-path.h: Adapt PathPattern to accept lang-item 
paths.
* hir/tree/rust-hir-path.cc: Assert we are dealing with a segmented 
path, create lang-item
constructors.
* hir/tree/rust-hir.cc (PathPattern::convert_to_simple_path): 
Likewise.

Diff:
---
 gcc/rust/hir/tree/rust-hir-path.cc | 20 +
 gcc/rust/hir/tree/rust-hir-path.h  | 86 ++
 gcc/rust/hir/tree/rust-hir.cc  |  6 +++
 3 files changed, 104 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-path.cc 
b/gcc/rust/hir/tree/rust-hir-path.cc
index 7db2b25b5aaf..ee4a57294db2 100644
--- a/gcc/rust/hir/tree/rust-hir-path.cc
+++ b/gcc/rust/hir/tree/rust-hir-path.cc
@@ -133,6 +133,8 @@ PathExprSegment::operator= (PathExprSegment const &other)
 void
 PathPattern::iterate_path_segments (std::function cb)
 {
+  rust_assert (kind == Kind::Segmented);
+
   for (auto it = segments.begin (); it != segments.end (); it++)
 {
   if (!cb (*it))
@@ -150,6 +152,15 @@ PathInExpression::PathInExpression (Analysis::NodeMapping 
mappings,
 has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
 {}
 
+PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
+   LangItem::Kind lang_item, location_t locus,
+   bool has_opening_scope_resolution,
+   std::vector outer_attrs)
+  : PathPattern (lang_item),
+PathExpr (std::move (mappings), std::move (outer_attrs)),
+has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
+{}
+
 bool
 PathInExpression::is_self () const
 
@@ -358,6 +369,15 @@ QualifiedPathInExpression::QualifiedPathInExpression (
 path_type (std::move (qual_path_type)), locus (locus)
 {}
 
+QualifiedPathInExpression::QualifiedPathInExpression (
+  Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
+  LangItem::Kind lang_item, location_t locus,
+  std::vector outer_attrs)
+  : PathPattern (lang_item),
+PathExpr (std::move (mappings), std::move (outer_attrs)),
+path_type (std::move (qual_path_type)), locus (locus)
+{}
+
 QualifiedPathInType::QualifiedPathInType (
   Analysis::NodeMapping mappings, QualifiedPathType qual_path_type,
   std::unique_ptr associated_segment,
diff --git a/gcc/rust/hir/tree/rust-hir-path.h 
b/gcc/rust/hir/tree/rust-hir-path.h
index f622addcc64b..a8859b8686d9 100644
--- a/gcc/rust/hir/tree/rust-hir-path.h
+++ b/gcc/rust/hir/tree/rust-hir-path.h
@@ -19,6 +19,7 @@
 #ifndef RUST_HIR_PATH_H
 #define RUST_HIR_PATH_H
 
+#include "rust-hir-map.h"
 #include "rust-hir-simple-path.h"
 #include "rust-hir-type-no-bounds.h"
 #include "rust-hir-pattern-abstract.h"
@@ -230,15 +231,34 @@ public:
 // HIR node representing a pattern that involves a "path" - abstract base class
 class PathPattern : public Pattern
 {
+public:
+  enum class Kind
+  {
+Segmented,
+LangItem
+  };
+
+private:
   std::vector segments;
+  tl::optional lang_item;
+  Kind kind;
 
 protected:
   PathPattern (std::vector segments)
-: segments (std::move (segments))
+: segments (std::move (segments)), lang_item (tl::nullopt),
+  kind (Kind::Segmented)
+  {}
+
+  PathPattern (LangItem::Kind lang_item)
+: segments ({}), lang_item (lang_item), kind (Kind::LangItem)
   {}
 
   // Returns whether path has segments.
-  bool has_segments () const { return !segments.empty (); }
+  bool has_segments () const
+  {
+rust_assert (kind == Kind::Segmented);
+return !segments.empty ();
+  }
 
   /* Converts path segments to their equivalent SimplePath segments if 
possible,
* and creates a SimplePath from them. */
@@ -248,26 +268,61 @@ protected:
 public:
   /* Returns whether the path is a single segment (excluding qualified path
* initial as segment). */
-  bool is_single_segment () const { return segments.size () == 1; }
+  bool is_single_segment () const
+  {
+rust_assert (kind == Kind::Segmented);
+return segments.size () == 1;
+  }
 
   std::string as_string () const override;
 
   void iterate_path_segments (std::function cb);
 
-  size_t get_num_segments () const { return segments.size (); }
+  size_t get_num_segments () const
+  {
+rust_assert (kind == Kind::Segmented);
+return segments.size ();
+  }
 
-  std::vector &get_segments () { return segments; }
+  std::vector &get_segments ()
+  {
+rust_assert (kind == Kind::Segmented);
+return segments;
+  }
 
-  const std::vector &get_segments () const { return segments; 
}
+  const std::vector &get_segments () const
+  {
+rust_assert (kind == Kind::Segmented);
+return segments;
+  }
 
-  PathExprSegment &get_root_se

[gcc/devel/rust/master] Remove query mode on CompileItem

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:e9659126e74d003fb704e23c01120e23d484bddb

commit e9659126e74d003fb704e23c01120e23d484bddb
Author: Pierre-Emmanuel Patry 
Date:   Thu Dec 12 13:16:14 2024 +0100

Remove query mode on CompileItem

Query mode was a hack to catch up some compile errors early, it was
deemed to be removed at some time. Recent changes to NR1 highlighted
an incompatibility with it hence it's removal.

gcc/rust/ChangeLog:

* backend/rust-compile-item.h: Remove query mode.
* backend/rust-compile-resolve-path.cc 
(HIRCompileBase::query_compile):
Likewise.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/backend/rust-compile-item.h  | 6 ++
 gcc/rust/backend/rust-compile-resolve-path.cc | 6 ++
 2 files changed, 4 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-item.h 
b/gcc/rust/backend/rust-compile-item.h
index 1c877fa328dc..70660e16147c 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -31,15 +31,13 @@ protected:
 public:
   static tree compile (HIR::Item *item, Context *ctx,
   TyTy::BaseType *concrete = nullptr,
-  bool is_query_mode = false,
   location_t ref_locus = UNDEF_LOCATION)
   {
 CompileItem compiler (ctx, concrete, ref_locus);
 item->accept_vis (compiler);
 
-if (is_query_mode && compiler.reference == error_mark_node)
-  rust_internal_error_at (ref_locus, "failed to compile item: %s",
- item->as_string ().c_str ());
+if (compiler.reference == error_mark_node)
+  rust_debug ("failed to compile item: %s", item->as_string ().c_str ());
 
 return compiler.reference;
   }
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index d4e8fbe3b99d..8752d8fa75fc 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -240,11 +240,9 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType 
*lookup,
   if (auto resolved_item = ctx->get_mappings ().lookup_hir_item (ref))
 {
   if (!lookup->has_substitutions_defined ())
-   return CompileItem::compile (*resolved_item, ctx, nullptr, true,
-expr_locus);
+   return CompileItem::compile (*resolved_item, ctx, nullptr, expr_locus);
   else
-   return CompileItem::compile (*resolved_item, ctx, lookup, true,
-expr_locus);
+   return CompileItem::compile (*resolved_item, ctx, lookup, expr_locus);
 }
   else if (auto hir_extern_item
   = ctx->get_mappings ().lookup_hir_extern_item (ref))


[gcc/devel/rust/master] Resolved item type shall be differentiated later

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:05348933bd4708a91bfce94720890f7d6fac615f

commit 05348933bd4708a91bfce94720890f7d6fac615f
Author: Pierre-Emmanuel Patry 
Date:   Mon Jan 6 15:55:53 2025 +0100

Resolved item type shall be differentiated later

We need to query all namespaces and error out at a later stage if the
retrieved item is wrong.

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc 
(TraitResolver::resolve_path_to_trait):
Query all namespaces.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/typecheck/rust-hir-trait-resolve.cc | 6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc 
b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index 8b90039f2835..0048ff55b445 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -127,8 +127,10 @@ TraitResolver::resolve_path_to_trait (const HIR::TypePath 
&path,
 }
   else
 {
-  ok = resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (),
-  &ref);
+  auto path_nodeid = path.get_mappings ().get_nodeid ();
+  ok = resolver->lookup_resolved_type (path_nodeid, &ref)
+  || resolver->lookup_resolved_name (path_nodeid, &ref)
+  || resolver->lookup_resolved_macro (path_nodeid, &ref);
 }
 
   if (!ok)


[gcc/devel/rust/master] Add unit struct to name namespace in old resolver

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:a7419e6998ca7db4b87a479ad2376c4b1b34d4be

commit a7419e6998ca7db4b87a479ad2376c4b1b34d4be
Author: Pierre-Emmanuel Patry 
Date:   Wed Dec 11 15:12:00 2024 +0100

Add unit struct to name namespace in old resolver

We missed the name namespace for unit struct in the old resolver.

gcc/rust/ChangeLog:

* resolve/rust-ast-resolve-toplevel.h: Add struct to name namespace.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/resolve/rust-ast-resolve-toplevel.h | 23 +++
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h 
b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
index ceac7edd3415..491fa690eb50 100644
--- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h
+++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
@@ -242,14 +242,21 @@ public:
 auto path = prefix.append (decl);
 auto cpath = canonical_prefix.append (decl);
 
-resolver->get_type_scope ().insert (
-  path, struct_decl.get_node_id (), struct_decl.get_locus (), false,
-  Rib::ItemType::Type,
-  [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
-   rich_location r (line_table, struct_decl.get_locus ());
-   r.add_range (locus);
-   rust_error_at (r, "redefined multiple times");
-  });
+auto duplicate_item
+  = [&] (const CanonicalPath &, NodeId, location_t locus) -> void {
+  rich_location r (line_table, struct_decl.get_locus ());
+  r.add_range (locus);
+  rust_error_at (r, "redefined multiple times");
+};
+
+resolver->get_type_scope ().insert (path, struct_decl.get_node_id (),
+   struct_decl.get_locus (), false,
+   Rib::ItemType::Type, duplicate_item);
+
+if (struct_decl.is_unit_struct ())
+  resolver->get_name_scope ().insert (path, struct_decl.get_node_id (),
+ struct_decl.get_locus (), false,
+ Rib::ItemType::Type, duplicate_item);
 
 NodeId current_module = resolver->peek_current_module_scope ();
 mappings.insert_module_child_item (current_module, decl);


[gcc/devel/rust/master] Remove some tests from nr2 exclusion file

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:88fa2ca42f90be216f53fceaa5b008113a64991b

commit 88fa2ca42f90be216f53fceaa5b008113a64991b
Author: Pierre-Emmanuel Patry 
Date:   Thu Dec 12 14:39:12 2024 +0100

Remove some tests from nr2 exclusion file

Those test are now passing.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove passing tests.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/testsuite/rust/compile/nr2/exclude | 11 ---
 1 file changed, 11 deletions(-)

diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index 8bcc8aed0306..641222a8d7f1 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -2,17 +2,12 @@ bounds1.rs
 break-rust2.rs
 canonical_paths1.rs
 cfg1.rs
-cfg3.rs
-cfg4.rs
-cfg5.rs
 closure_no_type_anno.rs
 complex-path1.rs
 const-issue1440.rs
 const_generics_3.rs
 const_generics_4.rs
-const_generics_5.rs
 const_generics_7.rs
-derive_empty.rs
 derive_macro1.rs
 expected_type_args2.rs
 feature_rust_attri0.rs
@@ -40,7 +35,6 @@ issue-1901.rs
 issue-1981.rs
 issue-2036.rs
 issue-2043.rs
-issue-2136-2.rs
 issue-2142.rs
 issue-2238.rs
 issue-2330.rs
@@ -56,16 +50,12 @@ issue-852.rs
 issue-855.rs
 iterators1.rs
 lookup_err1.rs
-macros/mbe/macro13.rs
-macros/mbe/macro15.rs
-macros/mbe/macro20.rs
 macros/mbe/macro23.rs
 macros/mbe/macro40.rs
 macros/mbe/macro43.rs
 macros/mbe/macro44.rs
 macros/mbe/macro54.rs
 macros/mbe/macro6.rs
-macros/mbe/macro_rules_macro_rules.rs
 macros/mbe/macro_use1.rs
 match-never-ltype.rs
 match-never-rtype.rs
@@ -103,7 +93,6 @@ pub_restricted_3.rs
 redef_error2.rs
 redef_error4.rs
 redef_error5.rs
-self-path1.rs
 self-path2.rs
 sizeof-stray-infer-var-bug.rs
 struct-expr-parse.rs


[gcc/devel/rust/master] Add debug dump to old name resolver

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:55c047ebe117ec97c8d9040035d73113ae0d6fd2

commit 55c047ebe117ec97c8d9040035d73113ae0d6fd2
Author: Pierre-Emmanuel Patry 
Date:   Wed Dec 11 14:19:44 2024 +0100

Add debug dump to old name resolver

It might be necessary to compare both name resolution' internal states
during the transition. This new debug representation could help with
that.

gcc/rust/ChangeLog:

* resolve/rust-name-resolver.h: Add new degug dump for old name
resolver.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/resolve/rust-name-resolver.h | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/gcc/rust/resolve/rust-name-resolver.h 
b/gcc/rust/resolve/rust-name-resolver.h
index 5e1901adfd5a..bf5ef7786960 100644
--- a/gcc/rust/resolve/rust-name-resolver.h
+++ b/gcc/rust/resolve/rust-name-resolver.h
@@ -204,6 +204,41 @@ public:
   void insert_captured_item (NodeId id);
   const std::set &get_captures (NodeId id) const;
 
+  std::string as_debug_string () const
+  {
+std::stringstream ss;
+
+ss << "Names:\n";
+for (auto &n : name_ribs)
+  {
+   ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
+  << "\n";
+  }
+ss << "Types:\n";
+for (auto &n : type_ribs)
+  {
+   ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
+  << "\n";
+  }
+ss << "Macros:\n";
+
+for (auto &n : macro_ribs)
+  {
+   ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
+  << "\n";
+  }
+
+ss << "Labels:\n";
+
+for (auto &n : label_ribs)
+  {
+   ss << "\tNodeID: " << n.first << " Rib: " << n.second->debug_str ()
+  << "\n";
+  }
+
+return ss.str ();
+  }
+
 protected:
   bool decl_needs_capture (NodeId decl_rib_node_id, NodeId closure_rib_node_id,
   const Scope &scope);


[gcc/devel/rust/master] Add option_env! support

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:761d424d2a7af38aacad9ffd5b5132fcf286ac2e

commit 761d424d2a7af38aacad9ffd5b5132fcf286ac2e
Author: Liam Naddell 
Date:   Thu Jan 30 20:51:21 2025 -0500

Add option_env! support

gcc/rust/ChangeLog:
* expand/rust-macro-builtins-utility.cc: Add macro expansion for
option_env with eager expansion
* expand/rust-macro-builtins.cc: Add option_env to builtin list
* expand/rust-macro-builtins.h: Add option_env handler to header
file
* resolve/rust-late-name-resolver-2.0.cc: Prevent NR2.0 from
recursing into lang-item segments

gcc/testsuite/ChangeLog:
* rust/compile/macros/builtin/option_env1.rs: Add success case for 
option_env
* rust/compile/macros/builtin/option_env2.rs: Add failure case for 
option_env
* rust/execute/torture/builtin_macro_option_env.rs: Add
execution case for option_env

Diff:
---
 gcc/rust/expand/rust-macro-builtins-utility.cc | 78 ++
 gcc/rust/expand/rust-macro-builtins.cc |  2 +-
 gcc/rust/expand/rust-macro-builtins.h  |  4 ++
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc|  2 +
 .../rust/compile/macros/builtin/option_env1.rs | 29 
 .../rust/compile/macros/builtin/option_env2.rs | 27 
 .../rust/compile/macros/builtin/option_env3.rs | 28 
 .../execute/torture/builtin_macro_option_env.rs| 65 ++
 8 files changed, 234 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/expand/rust-macro-builtins-utility.cc 
b/gcc/rust/expand/rust-macro-builtins-utility.cc
index ff64879c5eae..28829a18f956 100644
--- a/gcc/rust/expand/rust-macro-builtins-utility.cc
+++ b/gcc/rust/expand/rust-macro-builtins-utility.cc
@@ -17,6 +17,7 @@
 // .
 
 #include "rust-fmt.h"
+#include "rust-ast-builder.h"
 #include "rust-macro-builtins.h"
 #include "rust-macro-builtins-helpers.h"
 
@@ -226,6 +227,83 @@ MacroBuiltin::env_handler (location_t invoc_locus, 
AST::MacroInvocData &invoc,
   return AST::Fragment ({node}, std::move (tok));
 }
 
+/* Expand builtin macro option_env!(), which inspects an environment variable 
at
+   compile time. */
+tl::optional
+MacroBuiltin::option_env_handler (location_t invoc_locus,
+ AST::MacroInvocData &invoc,
+ AST::InvocKind semicolon)
+{
+  auto invoc_token_tree = invoc.get_delim_tok_tree ();
+  MacroInvocLexer lex (invoc_token_tree.to_token_stream ());
+  Parser parser (lex);
+
+  auto last_token_id = macro_end_token (invoc_token_tree, parser);
+  std::unique_ptr lit_expr = nullptr;
+  bool has_error = false;
+
+  auto start = lex.get_offs ();
+  auto expanded_expr = try_expand_many_expr (parser, last_token_id,
+invoc.get_expander (), has_error);
+  auto end = lex.get_offs ();
+
+  auto tokens = lex.get_token_slice (start, end);
+
+  if (has_error)
+return AST::Fragment::create_error ();
+
+  auto pending = check_for_eager_invocations (expanded_expr);
+  if (!pending.empty ())
+return make_eager_builtin_invocation (BuiltinMacro::OptionEnv, invoc_locus,
+ invoc_token_tree,
+ std::move (pending));
+
+  if (expanded_expr.size () != 1)
+{
+  rust_error_at (invoc_locus, "% takes 1 argument");
+  return AST::Fragment::create_error ();
+}
+
+  if (expanded_expr.size () > 0)
+if (!(lit_expr
+ = try_extract_string_literal_from_fragment (invoc_locus,
+ expanded_expr[0])))
+  return AST::Fragment::create_error ();
+
+  parser.skip_token (last_token_id);
+
+  auto env_value = getenv (lit_expr->as_string ().c_str ());
+  AST::Builder b (invoc_locus);
+
+  if (env_value == nullptr)
+{
+  auto none_expr = std::unique_ptr (
+   new AST::PathInExpression (LangItem::Kind::OPTION_NONE, {},
+  invoc_locus));
+
+  auto node = AST::SingleASTNode (std::move (none_expr));
+  std::vector nodes;
+  nodes.push_back (node);
+
+  return AST::Fragment (nodes, std::vector> 
());
+}
+  std::vector> args;
+  args.push_back (b.literal_string (env_value));
+
+  std::unique_ptr some_expr
+= b.call (std::unique_ptr (
+   new AST::PathInExpression (LangItem::Kind::OPTION_SOME, {},
+  invoc_locus)),
+ std::move (args));
+
+  auto node = AST::SingleASTNode (std::move (some_expr));
+
+  std::vector nodes;
+  nodes.push_back (node);
+
+  return AST::Fragment (nodes, std::vector> ());
+}
+
 tl::optional
 MacroBuiltin::cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
   AST::InvocKind semicolon)
diff --git a/gcc/rust/expand/rust-macro-builtins.cc 
b/gcc/rust/expand/rust-macro-builtin

[gcc/devel/rust/master] ast: Add new Expr::Kinds

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:51a2da3d567dfbc70e9407276dfabc0b963ad420

commit 51a2da3d567dfbc70e9407276dfabc0b963ad420
Author: Arthur Cohen 
Date:   Thu Jan 23 11:43:31 2025 +

ast: Add new Expr::Kinds

Collapses all of the OperatorExprs into Expr instead of first having to 
check for OperatorExpr and
then check for each OperatorExpr::Kind.

gcc/rust/ChangeLog:

* ast/rust-ast.h: Add new Expr::Kinds.
* ast/rust-expr.h: Implement missing get_expr_kind(), Add 
get_function_expr_ptr()

Diff:
---
 gcc/rust/ast/rust-ast.h  | 10 ++
 gcc/rust/ast/rust-expr.h | 31 +++
 2 files changed, 41 insertions(+)

diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 2d907e2ca21a..e3999dbb141e 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1263,6 +1263,16 @@ public:
 Identifier,
 FormatArgs,
 MacroInvocation,
+Borrow,
+Dereference,
+ErrorPropagation,
+Negation,
+ArithmeticOrLogical,
+Comparison,
+LazyBoolean,
+TypeCast,
+Assignment,
+CompoundAssignment,
   };
 
   virtual Kind get_expr_kind () const = 0;
diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 852c3f3a3a45..cff09fe17d7b 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -407,6 +407,8 @@ public:
   bool get_is_double_borrow () const { return double_borrow; }
   bool is_raw_borrow () const { return raw_borrow; }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::Borrow; }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -437,6 +439,8 @@ public:
 return *main_or_left_expr;
   }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::Dereference; 
}
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -468,6 +472,11 @@ public:
 return *main_or_left_expr;
   }
 
+  Expr::Kind get_expr_kind () const override
+  {
+return Expr::Kind::ErrorPropagation;
+  }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -511,6 +520,8 @@ public:
 return *main_or_left_expr;
   }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::Negation; }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -599,6 +610,11 @@ public:
   void visit_lhs (ASTVisitor &vis) { main_or_left_expr->accept_vis (vis); }
   void visit_rhs (ASTVisitor &vis) { right_expr->accept_vis (vis); }
 
+  Expr::Kind get_expr_kind () const override
+  {
+return Expr::Kind::ArithmeticOrLogical;
+  }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -686,6 +702,8 @@ public:
 
   ExprType get_kind () { return expr_type; }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::Comparison; }
+
   /* TODO: implement via a function call to std::cmp::PartialEq::eq(&op1, &op2)
* maybe? */
 protected:
@@ -774,6 +792,8 @@ public:
 
   ExprType get_kind () { return expr_type; }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::LazyBoolean; 
}
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -836,6 +856,8 @@ public:
 return *type_to_convert_to;
   }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::TypeCast; }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -914,6 +936,8 @@ public:
 return *right_expr;
   }
 
+  Expr::Kind get_expr_kind () const override { return Expr::Kind::Assignment; }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -1000,6 +1024,11 @@ public:
 return right_expr;
   }
 
+  Expr::Kind get_expr_kind () const override
+  {
+return Expr::Kind::CompoundAssignment;
+  }
+
 protected:
   /* Use covariance to implement clone function as returning this object rather
* than base */
@@ -2139,6 +2168,8 @@ public:
 return *function;
   }
 
+  std::unique_ptr &get_function_expr_ptr () { return function; }
+
   const std::vector &get_outer_attrs () const { return outer_attrs; 
}
   std::vector &get_outer_attrs () override { return outer_attrs; }


[gcc/devel/rust/master] derive-visitor: Add method for setting up derived impl generics

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:8a768c72cc077a8d62a7966a7c95727fa760c291

commit 8a768c72cc077a8d62a7966a7c95727fa760c291
Author: Arthur Cohen 
Date:   Thu Jan 30 10:50:30 2025 +0100

derive-visitor: Add method for setting up derived impl generics

gcc/rust/ChangeLog:

* expand/rust-derive.cc (DeriveVisitor::setup_impl_generics): New 
method.
* expand/rust-derive.h: Declare it, define 
DeriveVisitor::ImplGenerics struct.

Diff:
---
 gcc/rust/expand/rust-derive.cc | 74 ++
 gcc/rust/expand/rust-derive.h  | 23 +
 2 files changed, 97 insertions(+)

diff --git a/gcc/rust/expand/rust-derive.cc b/gcc/rust/expand/rust-derive.cc
index e6778fea9729..c988412ebb01 100644
--- a/gcc/rust/expand/rust-derive.cc
+++ b/gcc/rust/expand/rust-derive.cc
@@ -50,5 +50,79 @@ DeriveVisitor::derive (Item &item, const Attribute &attr,
 };
 }
 
+DeriveVisitor::ImplGenerics
+DeriveVisitor::setup_impl_generics (
+  const std::string &type_name,
+  const std::vector> &type_generics,
+  tl::optional> &&extra_bound) const
+{
+  std::vector lifetime_args;
+  std::vector generic_args;
+  std::vector> impl_generics;
+  for (const auto &generic : type_generics)
+{
+  switch (generic->get_kind ())
+   {
+ case GenericParam::Kind::Lifetime: {
+   LifetimeParam &lifetime_param = (LifetimeParam &) *generic.get ();
+
+   Lifetime l = builder.new_lifetime (lifetime_param.get_lifetime ());
+   lifetime_args.push_back (std::move (l));
+
+   auto impl_lifetime_param
+ = builder.new_lifetime_param (lifetime_param);
+   impl_generics.push_back (std::move (impl_lifetime_param));
+ }
+ break;
+
+ case GenericParam::Kind::Type: {
+   TypeParam &type_param = (TypeParam &) *generic.get ();
+
+   std::unique_ptr associated_type = builder.single_type_path (
+ type_param.get_type_representation ().as_string ());
+
+   GenericArg type_arg
+ = GenericArg::create_type (std::move (associated_type));
+   generic_args.push_back (std::move (type_arg));
+
+   std::vector> extra_bounds;
+
+   if (extra_bound)
+ extra_bounds.emplace_back (std::move (*extra_bound));
+
+   auto impl_type_param
+ = builder.new_type_param (type_param, std::move (extra_bounds));
+
+   impl_generics.push_back (std::move (impl_type_param));
+ }
+ break;
+
+ case GenericParam::Kind::Const: {
+   rust_unreachable ();
+
+   // TODO
+   // const ConstGenericParam *const_param
+   //   = (const ConstGenericParam *) generic.get ();
+   // std::unique_ptr const_expr = nullptr;
+
+   // GenericArg type_arg
+   //   = GenericArg::create_const (std::move (const_expr));
+   // generic_args.push_back (std::move (type_arg));
+ }
+ break;
+   }
+}
+
+  auto generic_args_for_self
+= GenericArgs (lifetime_args, generic_args, {} /*binding args*/, loc);
+
+  std::unique_ptr self_type_path
+= impl_generics.empty ()
+   ? builder.single_type_path (type_name)
+   : builder.single_generic_type_path (type_name, generic_args_for_self);
+
+  return ImplGenerics{std::move (self_type_path), std::move (impl_generics)};
+}
+
 } // namespace AST
 } // namespace Rust
diff --git a/gcc/rust/expand/rust-derive.h b/gcc/rust/expand/rust-derive.h
index 517fee614f00..93025f1cb6d4 100644
--- a/gcc/rust/expand/rust-derive.h
+++ b/gcc/rust/expand/rust-derive.h
@@ -43,6 +43,29 @@ protected:
   location_t loc;
   Builder builder;
 
+  struct ImplGenerics
+  {
+/* The type we are deriving the impl for */
+std::unique_ptr self_type;
+
+/* Generics for the impl itself */
+std::vector> impl;
+  };
+
+  /**
+   * Create the generic parameters for a derive impl block. Derived impl blocks
+   * will often share the same structure of reusing the exact same bounds as
+   * their original type, plus adding an extra one for the trait we are
+   * deriving. For example, when deriving `Clone` on `Foo`, you want to make
+   * sure that you implement `Clone` only if `T: Clone` - so you add an extra
+   * `Clone` bound to all of your generics.
+   */
+  ImplGenerics setup_impl_generics (
+const std::string &type_name,
+const std::vector> &type_generics,
+tl::optional> &&extra_bound
+= tl::nullopt) const;
+
 private:
   // the 4 "allowed" visitors, which a derive-visitor can specify and override
   virtual void visit_struct (StructStruct &struct_item) = 0;


[gcc/devel/rust/master] gccrs: add support for ref literal patterns

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:ad46f0871d80966a1dd1adf7bf444c99f094d42a

commit ad46f0871d80966a1dd1adf7bf444c99f094d42a
Author: Philip Herron 
Date:   Tue Jan 21 17:20:06 2025 +

gccrs: add support for ref literal patterns

Fixes Rust-GCC#3174

gcc/rust/ChangeLog:

* backend/rust-compile-pattern.cc (CompilePatternBindings::visit): 
make recursive
* typecheck/rust-hir-type-check-pattern.cc 
(TypeCheckPattern::visit): handle ref flag

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-3174.rs: New test.

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/backend/rust-compile-pattern.cc  | 18 ++-
 gcc/rust/typecheck/rust-hir-type-check-pattern.cc | 13 +--
 gcc/testsuite/rust/compile/issue-3174.rs  | 28 +++
 gcc/testsuite/rust/compile/nr2/exclude|  1 +
 4 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-pattern.cc 
b/gcc/rust/backend/rust-compile-pattern.cc
index 726ade61639a..1e33c4899634 100644
--- a/gcc/rust/backend/rust-compile-pattern.cc
+++ b/gcc/rust/backend/rust-compile-pattern.cc
@@ -481,8 +481,7 @@ CompilePatternBindings::visit (HIR::TupleStructPattern 
&pattern)
  tuple_field_index++,
  pattern->get_locus ());
 
-   ctx->insert_pattern_binding (
- pattern->get_mappings ().get_hirid (), binding);
+   CompilePatternBindings::Compile (*pattern, binding, ctx);
  }
  }
else
@@ -497,8 +496,7 @@ CompilePatternBindings::visit (HIR::TupleStructPattern 
&pattern)
  tuple_field_index++,
  pattern->get_locus ());
 
-   ctx->insert_pattern_binding (
- pattern->get_mappings ().get_hirid (), binding);
+   CompilePatternBindings::Compile (*pattern, binding, ctx);
  }
  }
   }
@@ -607,8 +605,16 @@ CompilePatternBindings::visit (HIR::ReferencePattern 
&pattern)
 void
 CompilePatternBindings::visit (HIR::IdentifierPattern &pattern)
 {
-  ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
-  match_scrutinee_expr);
+  if (!pattern.get_is_ref ())
+{
+  ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
+  match_scrutinee_expr);
+  return;
+}
+
+  tree ref = address_expression (match_scrutinee_expr,
+EXPR_LOCATION (match_scrutinee_expr));
+  ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (), ref);
 }
 
 void
diff --git a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc 
b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
index 52d125354d53..765504fa785e 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-pattern.cc
@@ -519,9 +519,18 @@ TypeCheckPattern::visit (HIR::RangePattern &pattern)
 }
 
 void
-TypeCheckPattern::visit (HIR::IdentifierPattern &)
+TypeCheckPattern::visit (HIR::IdentifierPattern &pattern)
 {
-  infered = parent;
+  if (!pattern.get_is_ref ())
+{
+  infered = parent;
+  return;
+}
+
+  infered = new TyTy::ReferenceType (pattern.get_mappings ().get_hirid (),
+TyTy::TyVar (parent->get_ref ()),
+pattern.is_mut () ? Mutability::Mut
+  : Mutability::Imm);
 }
 
 void
diff --git a/gcc/testsuite/rust/compile/issue-3174.rs 
b/gcc/testsuite/rust/compile/issue-3174.rs
new file mode 100644
index ..87588e1ed241
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-3174.rs
@@ -0,0 +1,28 @@
+extern "C" {
+fn printf(s: *const i8, ...);
+}
+
+enum Option {
+Some(i32),
+None,
+}
+
+impl Option {
+fn add(&mut self) {
+match *self {
+Option::Some(ref mut a) => *a += 1,
+Option::None => {}
+}
+}
+}
+
+fn main() {
+unsafe {
+let mut a = Option::None;
+a.add();
+let _s = "%d\n\0";
+let _s = _s as *const str;
+let s = _s as *const i8;
+printf(s, a);
+}
+}
diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index c5c7326500d6..e5e5c12a978a 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -150,4 +150,5 @@ issue-2953-1.rs
 issue-3030.rs
 traits12.rs
 try-trait.rs
+issue-3174.rs
 # please don't delete the trailing newline


[gcc/devel/rust/master] Make foreverstack debug string const

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:47fd13da4f825706f59ffd039ce8c6cf9d4767d5

commit 47fd13da4f825706f59ffd039ce8c6cf9d4767d5
Author: Pierre-Emmanuel Patry 
Date:   Mon Jan 6 15:53:31 2025 +0100

Make foreverstack debug string const

Those function should not change anything within the foreverstack, it
can therefore be made const.

gcc/rust/ChangeLog:

* resolve/rust-forever-stack.h: Make debug functions const.
* resolve/rust-forever-stack.hxx: Likewise.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/resolve/rust-forever-stack.h   | 6 +++---
 gcc/rust/resolve/rust-forever-stack.hxx | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/resolve/rust-forever-stack.h 
b/gcc/rust/resolve/rust-forever-stack.h
index 8fc46ad603f5..278c1a885243 100644
--- a/gcc/rust/resolve/rust-forever-stack.h
+++ b/gcc/rust/resolve/rust-forever-stack.h
@@ -670,7 +670,7 @@ public:
   tl::optional to_rib (NodeId rib_id);
   tl::optional to_rib (NodeId rib_id) const;
 
-  std::string as_debug_string ();
+  std::string as_debug_string () const;
 
   /**
* Used to check if a module is a descendant of another module
@@ -752,9 +752,9 @@ private:
   std::reference_wrapper cursor_reference;
 
   void stream_rib (std::stringstream &stream, const Rib &rib,
-  const std::string &next, const std::string &next_next);
+  const std::string &next, const std::string &next_next) const;
   void stream_node (std::stringstream &stream, unsigned indentation,
-   const Node &node);
+   const Node &node) const;
 
   /* Helper types and functions for `resolve_path` */
 
diff --git a/gcc/rust/resolve/rust-forever-stack.hxx 
b/gcc/rust/resolve/rust-forever-stack.hxx
index 6181c05fc6c5..2b628e44aead 100644
--- a/gcc/rust/resolve/rust-forever-stack.hxx
+++ b/gcc/rust/resolve/rust-forever-stack.hxx
@@ -699,7 +699,7 @@ template 
 void
 ForeverStack::stream_rib (std::stringstream &stream, const Rib &rib,
 const std::string &next,
-const std::string &next_next)
+const std::string &next_next) const
 {
   if (rib.get_values ().empty ())
 {
@@ -718,7 +718,7 @@ ForeverStack::stream_rib (std::stringstream &stream, 
const Rib &rib,
 template 
 void
 ForeverStack::stream_node (std::stringstream &stream, unsigned indentation,
- const ForeverStack::Node &node)
+ const ForeverStack::Node &node) const
 {
   auto indent = std::string (indentation, ' ');
   auto next = std::string (indentation + 4, ' ');
@@ -750,7 +750,7 @@ ForeverStack::stream_node (std::stringstream &stream, 
unsigned indentation,
 
 template 
 std::string
-ForeverStack::as_debug_string ()
+ForeverStack::as_debug_string () const
 {
   std::stringstream stream;


[gcc/devel/rust/master] nr2.0: late: Add proper handling for lang item PathInExpressions

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:adf6afb8205de374e95d8fb08573428630289547

commit adf6afb8205de374e95d8fb08573428630289547
Author: Arthur Cohen 
Date:   Fri Jan 31 12:26:21 2025 +

nr2.0: late: Add proper handling for lang item PathInExpressions

gcc/rust/ChangeLog:

* resolve/rust-late-name-resolver-2.0.cc (Late::visit): Special 
case lang item paths.

Diff:
---
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 8 +++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
index ec20e9af340f..a0c3a051ea70 100644
--- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc
@@ -254,8 +254,14 @@ Late::visit (AST::PathInExpression &expr)
   // TODO: How do we have a nice error with `can't capture dynamic environment
   // in a function item` error here?
   // do we emit it in `get`?
+
   if (expr.is_lang_item ())
-return;
+{
+  ctx.map_usage (Usage (expr.get_node_id ()),
+Definition (Analysis::Mappings::get ().get_lang_item_node (
+  expr.get_lang_item (;
+  return;
+}
 
   auto resolved
 = ctx.values.resolve_path (expr.get_segments ()).or_else ([&] () {


[gcc/devel/rust/master] lang-items: Add structural_{peq, teq}

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:9d6bd59e86946aeea41551f8ab227f3954235029

commit 9d6bd59e86946aeea41551f8ab227f3954235029
Author: Arthur Cohen 
Date:   Thu Jan 30 14:19:03 2025 +0100

lang-items: Add structural_{peq, teq}

These lang items are used when deriving Eq and PartialEq, and will be 
checked when compiling pattern matching.

gcc/rust/ChangeLog:

* util/rust-lang-item.cc: New items.
* util/rust-lang-item.h: Likewise.

gcc/testsuite/ChangeLog:

* rust/compile/structural-eq-peq.rs: New test.

Diff:
---
 gcc/rust/util/rust-lang-item.cc | 3 +++
 gcc/rust/util/rust-lang-item.h  | 3 +++
 gcc/testsuite/rust/compile/structural-eq-peq.rs | 9 +
 3 files changed, 15 insertions(+)

diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index 091c6b67ea53..9fcd325dba50 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -112,6 +112,9 @@ const BiMap 
Rust::LangItem::lang_items = {{
   {"from_ok", Kind::TRY_FROM_OK},
 
   {"from", Kind::FROM_FROM},
+
+  {"structural_peq", Kind::STRUCTURAL_PEQ},
+  {"structural_teq", Kind::STRUCTURAL_TEQ},
 }};
 
 tl::optional
diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h
index b5abf615d594..61d6454e6606 100644
--- a/gcc/rust/util/rust-lang-item.h
+++ b/gcc/rust/util/rust-lang-item.h
@@ -144,6 +144,9 @@ public:
 
 // NOTE: This is not a lang item in later versions of Rust
 FROM_FROM,
+
+STRUCTURAL_PEQ,
+STRUCTURAL_TEQ,
   };
 
   static const BiMap lang_items;
diff --git a/gcc/testsuite/rust/compile/structural-eq-peq.rs 
b/gcc/testsuite/rust/compile/structural-eq-peq.rs
new file mode 100644
index ..d04c295037f5
--- /dev/null
+++ b/gcc/testsuite/rust/compile/structural-eq-peq.rs
@@ -0,0 +1,9 @@
+#[lang = "structural_peq"]
+pub trait StructuralPartialEq {
+// Empty.
+}
+
+#[lang = "structural_teq"]
+pub trait StructuralEq {
+// Empty.
+}


[gcc/devel/rust/master] scan-deadcode: Do not warn unused fields if they start with '_'

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:e42c64242b8f25dcf30aee35ee059bdd8873561f

commit e42c64242b8f25dcf30aee35ee059bdd8873561f
Author: Arthur Cohen 
Date:   Thu Jan 30 13:08:33 2025 +0100

scan-deadcode: Do not warn unused fields if they start with '_'

gcc/rust/ChangeLog:

* checks/lints/rust-lint-scan-deadcode.h: Check if the field name 
starts with an
underscore before warning.

Diff:
---
 gcc/rust/checks/lints/rust-lint-scan-deadcode.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h 
b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
index 672c7611f539..f6a208c939de 100644
--- a/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
+++ b/gcc/rust/checks/lints/rust-lint-scan-deadcode.h
@@ -93,10 +93,11 @@ public:
  {
HirId field_hir_id = field.get_mappings ().get_hirid ();
if (should_warn (field_hir_id)
-   && !field.get_visibility ().is_public ())
+   && !field.get_visibility ().is_public ()
+   && field.get_field_name ().as_string ().at (0) != '_')
  {
rust_warning_at (field.get_locus (), 0,
-"field is never read: %<%s%>",
+"field is never read: %qs",
 field.get_field_name ().as_string ().c_str ());
  }
  }


[gcc/devel/rust/master] Remove some passing test from exclude file

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:84bca6f2712a04eb8c0f951c16cafd11202ba950

commit 84bca6f2712a04eb8c0f951c16cafd11202ba950
Author: Pierre-Emmanuel Patry 
Date:   Sat Jan 11 22:35:08 2025 +0100

Remove some passing test from exclude file

Those tests are now passing.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove some tests.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/testsuite/rust/compile/nr2/exclude | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/rust/compile/nr2/exclude 
b/gcc/testsuite/rust/compile/nr2/exclude
index 641222a8d7f1..c5c7326500d6 100644
--- a/gcc/testsuite/rust/compile/nr2/exclude
+++ b/gcc/testsuite/rust/compile/nr2/exclude
@@ -36,7 +36,6 @@ issue-1981.rs
 issue-2036.rs
 issue-2043.rs
 issue-2142.rs
-issue-2238.rs
 issue-2330.rs
 issue-2479.rs
 issue-2723-1.rs
@@ -50,6 +49,7 @@ issue-852.rs
 issue-855.rs
 iterators1.rs
 lookup_err1.rs
+macros/mbe/macro20.rs
 macros/mbe/macro23.rs
 macros/mbe/macro40.rs
 macros/mbe/macro43.rs
@@ -68,8 +68,6 @@ match9.rs
 method2.rs
 multiple_bindings1.rs
 multiple_bindings2.rs
-name_resolution2.rs
-name_resolution4.rs
 nested_macro_use1.rs
 nested_macro_use2.rs
 nested_macro_use3.rs
@@ -93,6 +91,7 @@ pub_restricted_3.rs
 redef_error2.rs
 redef_error4.rs
 redef_error5.rs
+self-path1.rs
 self-path2.rs
 sizeof-stray-infer-var-bug.rs
 struct-expr-parse.rs
@@ -103,7 +102,6 @@ type-bindings1.rs
 unconstrained_type_param.rs
 undeclared_label.rs
 use_1.rs
-use_2.rs
 v0-mangle1.rs
 v0-mangle2.rs
 while_break_expr.rs
@@ -128,7 +126,6 @@ issue-2907.rs
 issue-2423.rs
 issue-266.rs
 additional-trait-bounds2.rs
-auto_traits2.rs
 issue-3140.rs
 cmp1.rs
 derive_clone_enum1.rs


[gcc/devel/rust/master] Compile unit struct with constructor

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:7ba77d01d77e58ed707fbc6ff6bd9f23e6dfeb22

commit 7ba77d01d77e58ed707fbc6ff6bd9f23e6dfeb22
Author: Pierre-Emmanuel Patry 
Date:   Mon Jan 13 18:26:37 2025 +0100

Compile unit struct with constructor

gcc/rust/ChangeLog:

* backend/rust-compile-resolve-path.cc (ResolvePathRef::resolve): Do
not use query system for unit struct but compile it's constructor
instead.

Signed-off-by: Pierre-Emmanuel Patry 

Diff:
---
 gcc/rust/backend/rust-compile-resolve-path.cc | 5 +
 1 file changed, 5 insertions(+)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 8752d8fa75fc..07fa9b2acbf4 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -180,6 +180,11 @@ ResolvePathRef::resolve_with_node_id (
}
 }
 
+  // Handle unit struct
+  if (lookup->get_kind () == TyTy::TypeKind::ADT)
+return attempt_constructor_expression_lookup (lookup, ctx, mappings,
+ expr_locus);
+
   // let the query system figure it out
   tree resolved_item = query_compile (ref, lookup, final_segment, mappings,
  expr_locus, is_qualified_path);


[gcc/devel/rust/master] gccrs: Fix ICE during path probe

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:218df42d73bb853c3ee080ffaafb12f6522fc31c

commit 218df42d73bb853c3ee080ffaafb12f6522fc31c
Author: Philip Herron 
Date:   Mon Feb 3 15:15:40 2025 +

gccrs: Fix ICE during path probe

It is valid for the query to fail here so its valid to not assert here.

gcc/rust/ChangeLog:

* typecheck/rust-hir-path-probe.cc (PathProbeType::visit): remove 
assertion

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/typecheck/rust-hir-path-probe.cc | 12 ++--
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-path-probe.cc 
b/gcc/rust/typecheck/rust-hir-path-probe.cc
index 16976c34989d..cdb2c58588bb 100644
--- a/gcc/rust/typecheck/rust-hir-path-probe.cc
+++ b/gcc/rust/typecheck/rust-hir-path-probe.cc
@@ -212,8 +212,8 @@ PathProbeType::visit (HIR::TypeAlias &alias)
 {
   HirId tyid = alias.get_mappings ().get_hirid ();
   TyTy::BaseType *ty = nullptr;
-  bool ok = query_type (tyid, &ty);
-  rust_assert (ok);
+  if (!query_type (tyid, &ty))
+   return;
 
   PathProbeCandidate::ImplItemCandidate impl_item_candidate{&alias,
current_impl};
@@ -232,8 +232,8 @@ PathProbeType::visit (HIR::ConstantItem &constant)
 {
   HirId tyid = constant.get_mappings ().get_hirid ();
   TyTy::BaseType *ty = nullptr;
-  bool ok = query_type (tyid, &ty);
-  rust_assert (ok);
+  if (!query_type (tyid, &ty))
+   return;
 
   PathProbeCandidate::ImplItemCandidate impl_item_candidate{&constant,
current_impl};
@@ -252,8 +252,8 @@ PathProbeType::visit (HIR::Function &function)
 {
   HirId tyid = function.get_mappings ().get_hirid ();
   TyTy::BaseType *ty = nullptr;
-  bool ok = query_type (tyid, &ty);
-  rust_assert (ok);
+  if (!query_type (tyid, &ty))
+   return;
 
   PathProbeCandidate::ImplItemCandidate impl_item_candidate{&function,
current_impl};


[gcc/devel/rust/master] parser: Add testcases for multiline strings

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:b33f6348b7bfd7d9060f0614d54af2fb98d8bc2a

commit b33f6348b7bfd7d9060f0614d54af2fb98d8bc2a
Author: Arthur Cohen 
Date:   Wed Dec 25 17:02:38 2024 +

parser: Add testcases for multiline strings

Regression checks for Rust-GCC#1399

gcc/testsuite/ChangeLog:

* rust/compile/multiline-string.rs: New test.
* rust/execute/torture/multiline-string.rs: New test.

Diff:
---
 gcc/testsuite/rust/compile/multiline-string.rs | 14 ++
 gcc/testsuite/rust/execute/torture/multiline-string.rs | 15 +++
 2 files changed, 29 insertions(+)

diff --git a/gcc/testsuite/rust/compile/multiline-string.rs 
b/gcc/testsuite/rust/compile/multiline-string.rs
new file mode 100644
index ..fcd6fa812ed2
--- /dev/null
+++ b/gcc/testsuite/rust/compile/multiline-string.rs
@@ -0,0 +1,14 @@
+fn main() {
+let _a = "gcc
+
+rs";
+
+let _b = "rust
+
+c
+gcc
+
+
+
+rs";
+}
diff --git a/gcc/testsuite/rust/execute/torture/multiline-string.rs 
b/gcc/testsuite/rust/execute/torture/multiline-string.rs
new file mode 100644
index ..4d22f991ad35
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/multiline-string.rs
@@ -0,0 +1,15 @@
+// { dg-output "gcc\n\nrs\n" }
+
+extern "C" {
+fn printf(fmt: *const i8, ...);
+}
+
+fn main() -> i32 {
+let a = "gcc
+
+rs\0";
+
+unsafe { printf("%s\n\0" as *const str as *const i8, a as *const str as 
*const i8); }
+
+0
+}


[gcc/devel/rust/master] lang-item: Add Sync trait

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:5c647947f411cdb00e753a31b101bcfef4aea134

commit 5c647947f411cdb00e753a31b101bcfef4aea134
Author: Arthur Cohen 
Date:   Wed Dec 25 11:08:25 2024 +

lang-item: Add Sync trait

gcc/rust/ChangeLog:

* util/rust-lang-item.h: Add Sync marker trait.
* util/rust-lang-item.cc: Likewise.

Diff:
---
 gcc/rust/util/rust-lang-item.cc | 1 +
 gcc/rust/util/rust-lang-item.h  | 1 +
 2 files changed, 2 insertions(+)

diff --git a/gcc/rust/util/rust-lang-item.cc b/gcc/rust/util/rust-lang-item.cc
index 0d8a98077d1e..5a7bfd5f79c0 100644
--- a/gcc/rust/util/rust-lang-item.cc
+++ b/gcc/rust/util/rust-lang-item.cc
@@ -62,6 +62,7 @@ const BiMap 
Rust::LangItem::lang_items = {{
   {"copy", Kind::COPY},
   {"clone", Kind::CLONE},
   {"sized", Kind::SIZED},
+  {"sync", Kind::SYNC},
   {"slice_alloc", Kind::SLICE_ALLOC},
   {"slice_u8_alloc", Kind::SLICE_U8_ALLOC},
   {"str_alloc", Kind::STR_ALLOC},
diff --git a/gcc/rust/util/rust-lang-item.h b/gcc/rust/util/rust-lang-item.h
index bcf41df559e5..18ba37dc22df 100644
--- a/gcc/rust/util/rust-lang-item.h
+++ b/gcc/rust/util/rust-lang-item.h
@@ -82,6 +82,7 @@ public:
 COPY,
 CLONE,
 SIZED,
+SYNC,
 
 // https://github.com/Rust-GCC/gccrs/issues/1896
 // 
https://github.com/rust-lang/rust/commit/afbecc0f68c4dcfc4878ba5bcb1ac942544a1bdc


[gcc/devel/rust/master] ast: builder: Add Return expression builder

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:4a053730c5c880984be1a7f8b6cfb19b62aa3331

commit 4a053730c5c880984be1a7f8b6cfb19b62aa3331
Author: Arthur Cohen 
Date:   Thu Jan 23 11:42:38 2025 +

ast: builder: Add Return expression builder

gcc/rust/ChangeLog:

* ast/rust-ast-builder.h: Declare it.
* ast/rust-ast-builder.cc (Builder::return_expr): Define it.

Diff:
---
 gcc/rust/ast/rust-ast-builder.cc | 7 +++
 gcc/rust/ast/rust-ast-builder.h  | 4 
 2 files changed, 11 insertions(+)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index 963ba99d05f4..0538998f0170 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -276,6 +276,13 @@ Builder::block (std::vector> &&stmts,
   LoopLabel::error (), loc, loc));
 }
 
+std::unique_ptr
+Builder::return_expr (std::unique_ptr &&to_return)
+{
+  return std::unique_ptr (
+new ReturnExpr (std::move (to_return), {}, loc));
+}
+
 std::unique_ptr
 Builder::let (std::unique_ptr pattern, std::unique_ptr type,
  std::unique_ptr init) const
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index 5096e83bd9c0..f67546fe8809 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -89,6 +89,10 @@ public:
   std::unique_ptr &&tail_expr
   = nullptr) const;
 
+  /* Create an early return expression with an optional expression */
+  std::unique_ptr return_expr (std::unique_ptr &&to_return
+= nullptr);
+
   /* Create a let binding with an optional type and initializer (`let  :
*  = `) */
   std::unique_ptr let (std::unique_ptr pattern,


[gcc/devel/rust/master] nr2.0: Remove accidental copies of resolver

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:a4a3183322dc195b3f235618d28ddca3e320a5fa

commit a4a3183322dc195b3f235618d28ddca3e320a5fa
Author: Owen Avery 
Date:   Mon Oct 21 18:02:48 2024 -0400

nr2.0: Remove accidental copies of resolver

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc
(CompileExpr::generate_closure_function): Take
NameResolutionContext by reference instead of by value.
* backend/rust-compile-item.cc
(CompileItem::visit): Likewise.
* backend/rust-compile-resolve-path.cc
(ResolvePathRef::resolve): Likewise.
* checks/lints/rust-lint-marklive.cc
(MarkLive::find_ref_node_id): Likewise.
* typecheck/rust-hir-type-check-enumitem.cc
(TypeCheckEnumItem::visit): Likewise.
* typecheck/rust-hir-type-check-implitem.cc
(TypeCheckImplItem::visit): Likewise.
* typecheck/rust-hir-type-check-item.cc
(TypeCheckItem::visit): Likewise.
* typecheck/rust-hir-type-check-path.cc
(TypeCheckExpr::resolve_root_path): Likewise.
* typecheck/rust-hir-type-check-type.cc
(TypeCheckType::resolve_root_path): Likewise.

Signed-off-by: Owen Avery 

Diff:
---
 gcc/rust/backend/rust-compile-expr.cc  |  2 +-
 gcc/rust/backend/rust-compile-item.cc  |  6 +++---
 gcc/rust/backend/rust-compile-resolve-path.cc  |  2 +-
 gcc/rust/checks/lints/rust-lint-marklive.cc|  2 +-
 gcc/rust/typecheck/rust-hir-type-check-enumitem.cc |  8 
 gcc/rust/typecheck/rust-hir-type-check-implitem.cc |  2 +-
 gcc/rust/typecheck/rust-hir-type-check-item.cc | 10 +-
 gcc/rust/typecheck/rust-hir-type-check-path.cc |  2 +-
 gcc/rust/typecheck/rust-hir-type-check-type.cc |  4 ++--
 9 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-expr.cc 
b/gcc/rust/backend/rust-compile-expr.cc
index 79383301f353..a72156b6aa36 100644
--- a/gcc/rust/backend/rust-compile-expr.cc
+++ b/gcc/rust/backend/rust-compile-expr.cc
@@ -2453,7 +2453,7 @@ CompileExpr::generate_closure_function (HIR::ClosureExpr 
&expr,
   auto body_mappings = function_body.get_mappings ();
   if (flag_name_resolution_2_0)
{
- auto nr_ctx
+ auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
 
  auto candidate = nr_ctx.values.to_rib (body_mappings.get_nodeid ());
diff --git a/gcc/rust/backend/rust-compile-item.cc 
b/gcc/rust/backend/rust-compile-item.cc
index 60159b63fd5b..2b6836ac3912 100644
--- a/gcc/rust/backend/rust-compile-item.cc
+++ b/gcc/rust/backend/rust-compile-item.cc
@@ -52,7 +52,7 @@ CompileItem::visit (HIR::StaticItem &var)
 
   if (flag_name_resolution_2_0)
 {
-  auto nr_ctx
+  auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
 
   canonical_path
@@ -119,7 +119,7 @@ CompileItem::visit (HIR::ConstantItem &constant)
 
   if (flag_name_resolution_2_0)
 {
-  auto nr_ctx
+  auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
 
   canonical_path
@@ -192,7 +192,7 @@ CompileItem::visit (HIR::Function &function)
 
   if (flag_name_resolution_2_0)
 {
-  auto nr_ctx
+  auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
 
   auto path = nr_ctx.values.to_canonical_path (
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc 
b/gcc/rust/backend/rust-compile-resolve-path.cc
index 07fa9b2acbf4..c862a81e2124 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -212,7 +212,7 @@ ResolvePathRef::resolve (const HIR::PathIdentSegment 
&final_segment,
   NodeId ref_node_id = UNKNOWN_NODEID;
   if (flag_name_resolution_2_0)
 {
-  auto nr_ctx
+  auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
 
   auto resolved = nr_ctx.lookup (mappings.get_nodeid ());
diff --git a/gcc/rust/checks/lints/rust-lint-marklive.cc 
b/gcc/rust/checks/lints/rust-lint-marklive.cc
index aacfc082cdf6..270d0221bdb8 100644
--- a/gcc/rust/checks/lints/rust-lint-marklive.cc
+++ b/gcc/rust/checks/lints/rust-lint-marklive.cc
@@ -287,7 +287,7 @@ MarkLive::find_ref_node_id (NodeId ast_node_id, NodeId 
&ref_node_id)
 {
   if (flag_name_resolution_2_0)
 {
-  auto nr_ctx
+  auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
 
   nr_ctx.lookup (ast_node_id).map ([&ref_node_id] (NodeId resolved) {
diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc 
b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
index ac850752f2d2..527c8dfbbeac 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
@@ -83,7 +83,7 @@ TypeCheckEnumItem::visit (HIR::Enum

[gcc/devel/rust/master] Fix bug in type resolution of paths

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:668c3bf7b7b45f8cea7a09515b6420cece877881

commit 668c3bf7b7b45f8cea7a09515b6420cece877881
Author: Owen Avery 
Date:   Sat Jan 4 14:59:54 2025 -0500

Fix bug in type resolution of paths

gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc
(Early::resolve_glob_import): Use
NameResolutionContext::resolve_path instead of
ForeverStack::resolve_path.
(Early::visit): Likewise.
(Early::visit_attributes): Likewise.
* resolve/rust-early-name-resolver-2.0.h
(Early::resolve_path_in_all_ns): Likewise.
* resolve/rust-late-name-resolver-2.0.cc
(Late::visit): Likewise, insert segment resolutions not
handled by NameResolutionContext::resolve_path, and avoid throwing
an error when path resolution could be finished by the typechecker.
* resolve/rust-name-resolution-context.h
(NameResolutionContext::resolve_path): Add.
* typecheck/rust-hir-type-check-path.cc
(TypeCheckExpr::resolve_root_path): Use segment node ids instead
of the path node id to look up segment resolutions when using
the 2.0 resolver, as is done with the 1.0 resolver.
* typecheck/rust-hir-type-check-type.cc
(TypeCheckType::resolve_root_path): Likewise.
* resolve/rust-forever-stack.h
(ForeverStack::resolve_path): Add callback parameter for
inserting segment resolutions.
(ForeverStack::find_starting_point): Likewise.
(ForeverStack::resolve_segments): Likewise.
* resolve/rust-forever-stack.hxx
(ForeverStack::find_starting_point): Likewise.
(ForeverStack::resolve_segments): Likewise.
(ForeverStack::resolve_path): Likewise and avoid resolving
inside TraitOrImpl ribs.

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: Remove entries.

Signed-off-by: Owen Avery 

Diff:
---
 gcc/rust/resolve/rust-early-name-resolver-2.0.cc | 13 ---
 gcc/rust/resolve/rust-early-name-resolver-2.0.h  |  9 +++--
 gcc/rust/resolve/rust-forever-stack.h| 18 +
 gcc/rust/resolve/rust-forever-stack.hxx  | 48 +++-
 gcc/rust/resolve/rust-late-name-resolver-2.0.cc  | 36 +++---
 gcc/rust/resolve/rust-name-resolution-context.h  | 40 
 gcc/rust/typecheck/rust-hir-type-check-path.cc   |  5 ++-
 gcc/rust/typecheck/rust-hir-type-check-type.cc   |  2 +-
 gcc/testsuite/rust/compile/nr2/exclude   | 29 --
 9 files changed, 128 insertions(+), 72 deletions(-)

diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc 
b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
index ae02a17de3bc..cdd8d1643472 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -74,7 +74,8 @@ Early::go (AST::Crate &crate)
 bool
 Early::resolve_glob_import (NodeId use_dec_id, TopLevel::ImportKind &&glob)
 {
-  auto resolved = ctx.types.resolve_path (glob.to_resolve.get_segments ());
+  auto resolved
+= ctx.resolve_path (glob.to_resolve.get_segments (), Namespace::Types);
   if (!resolved.has_value ())
 return false;
 
@@ -259,7 +260,7 @@ Early::visit (AST::MacroInvocation &invoc)
   // we won't have changed `definition` from `nullopt` if there are more
   // than one segments in our path
   if (!definition.has_value ())
-definition = ctx.macros.resolve_path (path.get_segments ());
+definition = ctx.resolve_path (path.get_segments (), Namespace::Macros);
 
   // if the definition still does not have a value, then it's an error
   if (!definition.has_value ())
@@ -300,8 +301,8 @@ Early::visit_attributes (std::vector &attrs)
  auto traits = attr.get_traits_to_derive ();
  for (auto &trait : traits)
{
- auto definition
-   = ctx.macros.resolve_path (trait.get ().get_segments ());
+ auto definition = ctx.resolve_path (trait.get ().get_segments (),
+ Namespace::Macros);
  if (!definition.has_value ())
{
  // FIXME: Change to proper error message
@@ -324,8 +325,8 @@ Early::visit_attributes (std::vector &attrs)
 ->lookup_builtin (name)
 .is_error ()) // Do not resolve builtins
{
- auto definition
-   = ctx.macros.resolve_path (attr.get_path ().get_segments ());
+ auto definition = ctx.resolve_path (attr.get_path ().get_segments (),
+ Namespace::Macros);
  if (!definition.has_value ())
{
  // FIXME: Change to proper error message
diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.h 
b/gcc/rust/resolve/rust-early-name-resol

[gcc/devel/rust/master] ast-builder: Add new functions to create type paths.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:b57ab7aaa7a9472ec7204a5d5c539f05c93e121f

commit b57ab7aaa7a9472ec7204a5d5c539f05c93e121f
Author: Arthur Cohen 
Date:   Wed Jan 29 18:11:28 2025 +

ast-builder: Add new functions to create type paths.

gcc/rust/ChangeLog:

* ast/rust-ast-builder.cc (Builder::type_path): New functions.
* ast/rust-ast-builder.h: Declare them.

Diff:
---
 gcc/rust/ast/rust-ast-builder.cc | 21 -
 gcc/rust/ast/rust-ast-builder.h  |  4 
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc
index b3ee387ef9c1..4afe32910764 100644
--- a/gcc/rust/ast/rust-ast-builder.cc
+++ b/gcc/rust/ast/rust-ast-builder.cc
@@ -158,13 +158,32 @@ Builder::single_generic_type_path (LangItem::Kind 
lang_item,
   return std::unique_ptr (new TypePath (std::move (segments), loc));
 }
 
+TypePath
+Builder::type_path (std::vector> &&segments,
+   bool opening_scope) const
+{
+  return TypePath (std::move (segments), loc, opening_scope);
+}
+
+TypePath
+Builder::type_path (std::vector &&segments,
+   bool opening_scope) const
+{
+  auto type_segments = std::vector> ();
+
+  for (auto &&segment : segments)
+type_segments.emplace_back (type_path_segment (segment));
+
+  return TypePath (std::move (type_segments), loc, opening_scope);
+}
+
 TypePath
 Builder::type_path (std::unique_ptr &&segment) const
 {
   auto segments = std::vector> ();
   segments.emplace_back (std::move (segment));
 
-  return TypePath (std::move (segments), loc);
+  return type_path (std::move (segments));
 }
 
 TypePath
diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h
index 372e355fa9b6..333b6791ad2c 100644
--- a/gcc/rust/ast/rust-ast-builder.h
+++ b/gcc/rust/ast/rust-ast-builder.h
@@ -132,6 +132,10 @@ public:
   std::unique_ptr single_generic_type_path (LangItem::Kind lang_item,
  GenericArgs args) const;
 
+  TypePath type_path (std::vector> &&segment,
+ bool opening_scope = false) const;
+  TypePath type_path (std::vector &&segments,
+ bool opening_scope = false) const;
   TypePath type_path (std::unique_ptr &&segment) const;
   TypePath type_path (std::string type) const;
   TypePath type_path (LangItem::Kind lang_item) const;


[gcc/devel/rust/master] gccrs: Fix crash in privay reporter for placeholder types

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:842c1cf95d6e731e72449b36539d85624fa77161

commit 842c1cf95d6e731e72449b36539d85624fa77161
Author: Philip Herron 
Date:   Mon Feb 3 15:14:48 2025 +

gccrs: Fix crash in privay reporter for placeholder types

This guards against a crash but i think this should actually be treated
as if its a generic type like below. But for now this addresses a crash 
which can occur.

gcc/rust/ChangeLog:

* checks/errors/privacy/rust-privacy-reporter.cc 
(PrivacyReporter::check_base_type_privacy):
Add guard for placeholder

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc | 10 ++
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc 
b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
index 9c9f2cf850ed..77b03f59c728 100644
--- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
+++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
@@ -243,10 +243,12 @@ PrivacyReporter::check_base_type_privacy 
(Analysis::NodeMapping &node_mappings,
   static_cast (ty)->get_fields ())
recursive_check (param.get_tyty ());
   return;
-case TyTy::PLACEHOLDER:
-  return recursive_check (
-   // FIXME: Can we use `resolve` here? Is that what we should do?
-   static_cast (ty)->resolve ());
+  case TyTy::PLACEHOLDER: {
+   const auto p = static_cast (ty);
+   if (!p->can_resolve ())
+ return;
+   return recursive_check (p->resolve ());
+  }
 case TyTy::PROJECTION:
   return recursive_check (
static_cast (ty)->get ());


[gcc/devel/rust/master] ast: Add DesugarForLoop class

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:0d4b15cb70f2ab20e9b8a533f592cbb6fc8efe87

commit 0d4b15cb70f2ab20e9b8a533f592cbb6fc8efe87
Author: Arthur Cohen 
Date:   Sun Dec 22 15:59:27 2024 +

ast: Add DesugarForLoop class

gcc/rust/ChangeLog:

* ast/rust-desugar-for-loops.cc: New file.
* ast/rust-desugar-for-loops.h: New file.
* hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Make 
lowering of for-loops an
unreachable.
* Make-lang.in: Compile it.

gcc/testsuite/ChangeLog:

* rust/compile/for-loop1.rs: New test.
* rust/compile/for-loop2.rs: New test.
* rust/execute/torture/for-loop1.rs: New test.
* rust/execute/torture/for-loop2.rs: New test.
* rust/compile/nr2/exclude: Exclude for-loop1.rs

Diff:
---
 gcc/rust/Make-lang.in   |   1 +
 gcc/rust/ast/rust-desugar-for-loops.cc  | 204 +
 gcc/rust/ast/rust-desugar-for-loops.h   | 108 +
 gcc/rust/hir/rust-ast-lower-expr.cc |   2 +-
 gcc/testsuite/rust/compile/for-loop1.rs | 543 +++
 gcc/testsuite/rust/compile/for-loop2.rs | 545 
 gcc/testsuite/rust/compile/nr2/exclude  |   2 +
 gcc/testsuite/rust/execute/torture/for-loop1.rs | 545 
 gcc/testsuite/rust/execute/torture/for-loop2.rs | 544 +++
 9 files changed, 2493 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 70cd64a2e94c..bb36063d1a73 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -235,6 +235,7 @@ GRS_OBJS = \
rust/rust-expand-format-args.o \
rust/rust-lang-item.o \
rust/rust-collect-lang-items.o \
+   rust/rust-desugar-for-loops.o \
 $(END)
 # removed object files from here
 
diff --git a/gcc/rust/ast/rust-desugar-for-loops.cc 
b/gcc/rust/ast/rust-desugar-for-loops.cc
new file mode 100644
index ..5e5cbbc6b383
--- /dev/null
+++ b/gcc/rust/ast/rust-desugar-for-loops.cc
@@ -0,0 +1,204 @@
+// Copyright (C) 2025 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// .
+
+#include "rust-desugar-for-loops.h"
+#include "rust-ast-visitor.h"
+#include "rust-ast.h"
+#include "rust-hir-map.h"
+#include "rust-path.h"
+#include "rust-pattern.h"
+#include "rust-stmt.h"
+#include "rust-expr.h"
+#include "rust-ast-builder.h"
+
+namespace Rust {
+namespace AST {
+
+DesugarForLoops::DesugarForLoops () {}
+
+void
+DesugarForLoops::go (AST::Crate &crate)
+{
+  DefaultASTVisitor::visit (crate);
+}
+
+static void
+replace_for_loop (std::unique_ptr &for_loop,
+ std::unique_ptr &&expanded)
+{
+  for_loop = std::move (expanded);
+}
+
+MatchArm
+DesugarForLoops::DesugarCtx::make_match_arm (std::unique_ptr &&path)
+{
+  auto patterns = std::vector> ();
+  patterns.emplace_back (std::move (path));
+
+  return MatchArm (std::move (patterns), loc);
+}
+
+MatchCase
+DesugarForLoops::DesugarCtx::make_break_arm ()
+{
+  auto arm = make_match_arm (std::unique_ptr (new PathInExpression (
+builder.path_in_expression (LangItem::Kind::OPTION_NONE;
+
+  auto break_expr = std::unique_ptr (
+new BreakExpr (Lifetime::error (), nullptr, {}, loc));
+
+  return MatchCase (std::move (arm), std::move (break_expr));
+}
+
+MatchCase
+DesugarForLoops::DesugarCtx::make_continue_arm ()
+{
+  auto val = builder.identifier_pattern (DesugarCtx::continue_pattern_id);
+
+  auto patterns = std::vector> ();
+  patterns.emplace_back (std::move (val));
+
+  auto pattern_item = std::unique_ptr (
+new TupleStructItemsNoRange (std::move (patterns)));
+  auto pattern = std::unique_ptr (new TupleStructPattern (
+builder.path_in_expression (LangItem::Kind::OPTION_SOME),
+std::move (pattern_item)));
+
+  auto val_arm = make_match_arm (std::move (pattern));
+
+  auto next = builder.identifier (DesugarCtx::next_value_id);
+
+  auto assignment = std::unique_ptr (
+new AssignmentExpr (std::move (next),
+   builder.identifier (DesugarCtx::continue_pattern_id),
+   {}, loc));
+
+  return MatchCase (std::move (val_arm), std::move (assignment));
+}
+
+std::unique_ptr
+DesugarForLoops::DesugarCtx::statementify (std::unique_ptr &&expr)
+{
+  return std::unique_

[gcc r15-8546] gccrs: improve handling of Self Type paths

2025-03-26 Thread Arthur Cohen via Gcc-cvs
https://gcc.gnu.org/g:b10f33764e690158cbec10765f9209470c271cd6

commit r15-8546-gb10f33764e690158cbec10765f9209470c271cd6
Author: Philip Herron 
Date:   Mon Nov 25 21:37:19 2024 +

gccrs: improve handling of Self Type paths

TypePaths have special handling for Self where we can look at the current 
ctx
for more acurate TypeAlias information if required. We cant do this for Impl
contexts but not for Traits as we might as well fall back to the 
TypePathProbe.

The other issue was the dyn type comming in because Foo::foo and Foo is a 
trait
reference we represent this as a dyn type as the root resolved path but 
then find
the associated impl block for this but we cannot do this when we have 
resolved to
a Dyn because this is just a representation that we know we are talking 
about a
trait not because we are actually working with a real Dyn type.

Fixes Rust-GCC#2907

gcc/rust/ChangeLog:

* typecheck/rust-hir-trait-resolve.cc 
(TraitResolver::resolve_trait): track trait
* typecheck/rust-hir-type-check-implitem.cc: trait block
* typecheck/rust-hir-type-check-path.cc 
(TypeCheckExpr::resolve_segments): dont when dyn
* typecheck/rust-hir-type-check-type.cc (TypeCheckType::visit): 
look at Self contenxt
(TypeCheckType::resolve_root_path): track if Self
(TypeCheckType::resolve_associated_type): look at current context 
for associated types
* typecheck/rust-hir-type-check-type.h: change prototype
* typecheck/rust-hir-type-check.h (class TypeCheckBlockContextItem):
new context system to track current state
* typecheck/rust-typecheck-context.cc 
(TypeCheckContext::have_block_context): likewise
(TypeCheckContext::peek_block_context): likewise
(TypeCheckContext::push_block_context): likewise
(TypeCheckContext::pop_block_context): likewise
(TypeCheckBlockContextItem::Item::Item): likewise
(TypeCheckBlockContextItem::TypeCheckBlockContextItem): likewise
(TypeCheckBlockContextItem::is_impl_block): likewise
(TypeCheckBlockContextItem::is_trait_block): likewise
(TypeCheckBlockContextItem::get_impl_block): likewise
(TypeCheckBlockContextItem::get_trait): likewise

gcc/testsuite/ChangeLog:

* rust/compile/nr2/exclude: nr2 cant handle this
* rust/compile/issue-2907.rs: New test.

Signed-off-by: Philip Herron 

Diff:
---
 gcc/rust/typecheck/rust-hir-trait-resolve.cc   |   2 +
 gcc/rust/typecheck/rust-hir-type-check-implitem.cc |   3 +
 gcc/rust/typecheck/rust-hir-type-check-path.cc |   3 +-
 gcc/rust/typecheck/rust-hir-type-check-type.cc | 195 +++--
 gcc/rust/typecheck/rust-hir-type-check-type.h  |   9 +-
 gcc/rust/typecheck/rust-hir-type-check.h   |  38 
 gcc/rust/typecheck/rust-typecheck-context.cc   |  64 +++
 gcc/testsuite/rust/compile/issue-2907.rs   |  33 
 gcc/testsuite/rust/compile/nr2/exclude |   1 +
 9 files changed, 287 insertions(+), 61 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-trait-resolve.cc 
b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
index 6f2589a0d2ba..2fbf123aa774 100644
--- a/gcc/rust/typecheck/rust-hir-trait-resolve.cc
+++ b/gcc/rust/typecheck/rust-hir-trait-resolve.cc
@@ -278,6 +278,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
 }
   self->inherit_bounds (specified_bounds);
 
+  context->push_block_context (TypeCheckBlockContextItem (trait_reference));
   std::vector item_refs;
   for (auto &item : trait_reference->get_trait_items ())
 {
@@ -307,6 +308,7 @@ TraitResolver::resolve_trait (HIR::Trait *trait_reference)
   // resolve the blocks of functions etc because it can end up in a recursive
   // loop of trying to resolve traits as required by the types
   tref->on_resolved ();
+  context->pop_block_context ();
 
   return tref;
 }
diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc 
b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
index 602076811c35..5da88b890f39 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
@@ -335,7 +335,10 @@ TypeCheckImplItem::Resolve (
 
   // resolve
   TypeCheckImplItem resolver (parent, self, substitutions);
+  resolver.context->push_block_context (TypeCheckBlockContextItem (&parent));
   item.accept_vis (resolver);
+  resolver.context->pop_block_context ();
+
   return resolver.result;
 }
 
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc 
b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index cd0e941edd6d..4746e7d730da 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -355,6 +355,7 @@ TypeCheckExpr::resolve_segments (NodeId 
root_resolved_node_id,
   NodeId resolv

[gcc/devel/rust/master] derive(Debug): Add stub implementation.

2025-03-26 Thread Thomas Schwinge via Gcc-cvs
https://gcc.gnu.org/g:df1a65d6d275e097f73bb0c6652061e4dca240ab

commit df1a65d6d275e097f73bb0c6652061e4dca240ab
Author: Arthur Cohen 
Date:   Wed Jan 29 18:10:01 2025 +

derive(Debug): Add stub implementation.

gcc/rust/ChangeLog:

* expand/rust-derive-debug.cc: New file.
* expand/rust-derive-debug.h: New file.
* Make-lang.in: Compile them.
* expand/rust-derive.cc (DeriveVisitor::derive): Call into 
DeriveDebug.

gcc/testsuite/ChangeLog:

* rust/compile/derive-debug1.rs: New test.
* rust/compile/nr2/exclude: Exclude it.

Diff:
---
 gcc/rust/Make-lang.in   |   1 +
 gcc/rust/expand/rust-derive-debug.cc| 130 
 gcc/rust/expand/rust-derive-debug.h |  55 
 gcc/rust/expand/rust-derive.cc  |   6 ++
 gcc/testsuite/rust/compile/derive-debug1.rs |  41 +
 gcc/testsuite/rust/compile/nr2/exclude  |   1 +
 6 files changed, 234 insertions(+)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index 751ae874def8..3b9f7e9a9e62 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -96,6 +96,7 @@ GRS_OBJS = \
 rust/rust-derive.o \
 rust/rust-derive-clone.o \
 rust/rust-derive-copy.o \
+rust/rust-derive-debug.o \
 rust/rust-proc-macro.o \
 rust/rust-macro-invoc-lexer.o \
 rust/rust-proc-macro-invoc-lexer.o \
diff --git a/gcc/rust/expand/rust-derive-debug.cc 
b/gcc/rust/expand/rust-derive-debug.cc
new file mode 100644
index ..910f27c67b2f
--- /dev/null
+++ b/gcc/rust/expand/rust-derive-debug.cc
@@ -0,0 +1,130 @@
+// Copyright (C) 2025 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// .
+
+#include "rust-derive-debug.h"
+#include "rust-ast.h"
+#include "rust-hir-map.h"
+#include "rust-system.h"
+
+namespace Rust {
+namespace AST {
+
+DeriveDebug::DeriveDebug (location_t loc)
+  : DeriveVisitor (loc), expanded (nullptr)
+{}
+
+std::unique_ptr
+DeriveDebug::go (Item &item)
+{
+  item.accept_vis (*this);
+
+  rust_assert (expanded);
+
+  return std::move (expanded);
+}
+
+/* Pointer-ify something */
+template 
+static std::unique_ptr
+ptrify (T value)
+{
+  return std::unique_ptr (new T (value));
+}
+
+std::unique_ptr
+DeriveDebug::stub_debug_fn ()
+{
+  auto unit_expr = builder.tuple ();
+  auto ok_expr
+= ptrify (builder.path_in_expression (LangItem::Kind::RESULT_OK));
+
+  auto stub_return = builder.call (std::move (ok_expr), std::move (unit_expr));
+
+  // we can't use builder.block() here as it returns a unique_ptr and
+  // Function's constructor expects a unique_ptr
+  auto block = std::unique_ptr (
+new BlockExpr ({}, std::move (stub_return), {}, {},
+  AST::LoopLabel::error (), loc, loc));
+
+  auto self = builder.self_ref_param ();
+
+  auto return_type
+= ptrify (builder.type_path ({"core", "fmt", "Result"}, true));
+
+  auto mut_fmt_type_inner
+= ptrify (builder.type_path ({"core", "fmt", "Formatter"}, true));
+
+  auto mut_fmt_type
+= builder.reference_type (std::move (mut_fmt_type_inner), true);
+
+  auto fmt = builder.function_param (builder.identifier_pattern ("_fmt"),
+std::move (mut_fmt_type));
+
+  auto params = vec (std::move (self), std::move (fmt));
+
+  auto function = builder.function ({"fmt"}, std::move (params),
+   std::move (return_type), std::move (block));
+
+  return ptrify (function);
+}
+
+std::unique_ptr
+DeriveDebug::stub_derive_impl (
+  std::string name,
+  const std::vector> &type_generics)
+{
+  auto trait_items = vec (stub_debug_fn ());
+
+  auto debug = builder.type_path ({"core", "fmt", "Debug"}, true);
+  auto generics
+= setup_impl_generics (name, type_generics, builder.trait_bound (debug));
+
+  return builder.trait_impl (debug, std::move (generics.self_type),
+std::move (trait_items),
+std::move (generics.impl));
+}
+
+void
+DeriveDebug::visit_struct (StructStruct &struct_item)
+{
+  expanded = stub_derive_impl (struct_item.get_identifier ().as_string (),
+  struct_item.get_generic_params ());
+}
+
+void
+DeriveDebug::visit_tuple (TupleStruct &tuple_item)
+{
+  expanded = stub_derive_impl (tuple_item.

  1   2   3   >