From: Philip Herron <[email protected]>
We need to force braces on the default expression in the parser but also
the typecheck cant const eval when the typecheck fails otherwise it will
crash because invalid error mark node.
Fixes Rust-GCC/gccrs#4173
gcc/rust/ChangeLog:
* parse/rust-parse-impl.hxx:
* typecheck/rust-hir-type-check-base.cc:
gcc/testsuite/ChangeLog:
* rust/compile/issue-4173-1.rs: New test.
* rust/compile/issue-4173-2.rs: New test.
* rust/compile/issue-4173-3.rs: New test.
Signed-off-by: Philip Herron <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.
Commit on github: https://github.com/Rust-GCC/gccrs/commit/a8487d81aea7c528225ad3b50157c06b1e155d3c
The commit has been mentioned in the following issue(s):
- Rust-GCC/gccrs#4173: https://github.com/Rust-GCC/gccrs/issues/4173
The commit has been mentioned in the following pull-request(s):
- https://github.com/Rust-GCC/gccrs/pull/4781
gcc/rust/parse/rust-parse-impl.hxx | 10 ++++++++++
gcc/rust/typecheck/rust-hir-type-check-base.cc | 4 ++++
gcc/testsuite/rust/compile/issue-4173-1.rs | 8 ++++++++
gcc/testsuite/rust/compile/issue-4173-2.rs | 9 +++++++++
gcc/testsuite/rust/compile/issue-4173-3.rs | 9 +++++++++
5 files changed, 40 insertions(+)
create mode 100644 gcc/testsuite/rust/compile/issue-4173-1.rs
create mode 100644 gcc/testsuite/rust/compile/issue-4173-2.rs
create mode 100644 gcc/testsuite/rust/compile/issue-4173-3.rs
diff --git a/gcc/rust/parse/rust-parse-impl.hxx b/gcc/rust/parse/rust-parse-impl.hxx
index 9d7d529d4..49a5a45b9 100644
--- a/gcc/rust/parse/rust-parse-impl.hxx
+++ b/gcc/rust/parse/rust-parse-impl.hxx
@@ -2021,6 +2021,16 @@ Parser<ManagedTokenSource>::parse_generic_param (EndTokenPred is_end_token)
if (default_expr.value ().get_kind ()
== AST::GenericArg::Kind::Either)
default_expr = default_expr.value ().disambiguate_to_const ();
+ else if (default_expr.value ().get_kind ()
+ != AST::GenericArg::Kind::Const)
+ {
+ Error error (
+ default_expr.value ().get_locus (),
+ "expressions must be enclosed in braces to be used as const "
+ "generic arguments");
+ add_error (std::move (error));
+ default_expr = tl::nullopt;
+ }
}
param = std::unique_ptr<AST::ConstGenericParam> (
diff --git a/gcc/rust/typecheck/rust-hir-type-check-base.cc b/gcc/rust/typecheck/rust-hir-type-check-base.cc
index 4c276ce83..399b95946 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-base.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-base.cc
@@ -683,6 +683,10 @@ TypeCheckBase::resolve_generic_params (
auto expr_type
= TypeCheckExpr::Resolve (param.get_default_expression ());
+ if (specified_type->is<TyTy::ErrorType> ()
+ || expr_type->is<TyTy::ErrorType> ())
+ break;
+
coercion_site (param.get_mappings ().get_hirid (),
TyTy::TyWithLocation (specified_type),
TyTy::TyWithLocation (
diff --git a/gcc/testsuite/rust/compile/issue-4173-1.rs b/gcc/testsuite/rust/compile/issue-4173-1.rs
new file mode 100644
index 000000000..f5ec82bbc
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4173-1.rs
@@ -0,0 +1,8 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<const N: u32 = { 1 }>;
diff --git a/gcc/testsuite/rust/compile/issue-4173-2.rs b/gcc/testsuite/rust/compile/issue-4173-2.rs
new file mode 100644
index 000000000..d6e26bb54
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4173-2.rs
@@ -0,0 +1,9 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<const N: u32 = { u32::MAX }>;
+// { dg-error "failed to resolve path segment using an impl Probe" "" { target *-*-* } .-1 }
diff --git a/gcc/testsuite/rust/compile/issue-4173-3.rs b/gcc/testsuite/rust/compile/issue-4173-3.rs
new file mode 100644
index 000000000..c4933c4ff
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-4173-3.rs
@@ -0,0 +1,9 @@
+#![feature(no_core)]
+#![no_core]
+#![feature(lang_items)]
+
+#[lang = "sized"]
+trait Sized {}
+
+pub struct S<const N: u32 = u32::MAX>;
+// { dg-error "expressions must be enclosed in braces to be used as const generic arguments" "" { target *-*-* } .-1 }
base-commit: 7620b4e8c45fef22b046a3c299ffcd8a56443553
--
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.