From 446f78d05192ada54441f0a4ff237064d086e0e8 Mon Sep 17 00:00:00 2001 From: Gurinder Singh Date: Mon, 22 Apr 2024 10:31:32 +0530 Subject: [PATCH] Do not ICE on `AnonConst`s in `diagnostic_hir_wf_check` --- .../rustc_hir_analysis/src/hir_wf_check.rs | 14 +- tests/crashes/122989.rs | 8 - ...ce-hir-wf-check-anon-const-issue-122989.rs | 21 ++ ...ir-wf-check-anon-const-issue-122989.stderr | 179 ++++++++++++++++++ 4 files changed, 208 insertions(+), 14 deletions(-) delete mode 100644 tests/crashes/122989.rs create mode 100644 tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.rs create mode 100644 tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.stderr diff --git a/compiler/rustc_hir_analysis/src/hir_wf_check.rs b/compiler/rustc_hir_analysis/src/hir_wf_check.rs index 7014b23ff07..d6ba5fa9b5b 100644 --- a/compiler/rustc_hir_analysis/src/hir_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/hir_wf_check.rs @@ -163,15 +163,17 @@ fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) { kind: hir::GenericParamKind::Type { default: Some(ty), .. }, .. }) => vec![*ty], - hir::Node::AnonConst(_) - if let Some(const_param_id) = - tcx.hir().opt_const_param_default_param_def_id(hir_id) + hir::Node::AnonConst(_) => { + if let Some(const_param_id) = tcx.hir().opt_const_param_default_param_def_id(hir_id) && let hir::Node::GenericParam(hir::GenericParam { kind: hir::GenericParamKind::Const { ty, .. }, .. - }) = tcx.hir_node_by_def_id(const_param_id) => - { - vec![*ty] + }) = tcx.hir_node_by_def_id(const_param_id) + { + vec![*ty] + } else { + vec![] + } } ref node => bug!("Unexpected node {:?}", node), }, diff --git a/tests/crashes/122989.rs b/tests/crashes/122989.rs deleted file mode 100644 index 70ad7d3b65c..00000000000 --- a/tests/crashes/122989.rs +++ /dev/null @@ -1,8 +0,0 @@ -//@ known-bug: #122989 -trait Traitor = 1, const N: N<2> = N> { - fn N(&N) -> N<2> { - M - } -} - -trait N = 12> {} diff --git a/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.rs b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.rs new file mode 100644 index 00000000000..2995f26af4a --- /dev/null +++ b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.rs @@ -0,0 +1,21 @@ +// Regression test for ICE #122989 +trait Foo> { +//~^ WARN trait objects without an explicit `dyn` are deprecated +//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! +//~| ERROR cycle detected when computing type of `Foo::N` +//~| ERROR cycle detected when computing type of `Foo::N` +//~| ERROR the trait `Foo` cannot be made into an object +//~| ERROR the trait `Foo` cannot be made into an object +//~| ERROR the trait `Foo` cannot be made into an object +//~| ERROR `(dyn Bar<2> + 'static)` is forbidden as the type of a const generic parameter + fn func() { + } +} + +trait Bar> {} +//~^ WARN trait objects without an explicit `dyn` are deprecated +//~| WARN this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! +//~| ERROR the trait `Foo` cannot be made into an object +//~| ERROR `(dyn Foo<2> + 'static)` is forbidden as the type of a const generic parameter + +fn main() {} diff --git a/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.stderr b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.stderr new file mode 100644 index 00000000000..e6fdc440873 --- /dev/null +++ b/tests/ui/wf/ice-hir-wf-check-anon-const-issue-122989.stderr @@ -0,0 +1,179 @@ +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:20 + | +LL | trait Foo> { + | ^^^^^^ + | + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see + = note: `#[warn(bare_trait_objects)]` on by default +help: if this is an object-safe trait, use `dyn` + | +LL | trait Foo> { + | +++ + +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:20 + | +LL | trait Bar> {} + | ^^^^^^ + | + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see +help: if this is an object-safe trait, use `dyn` + | +LL | trait Bar> {} + | +++ + +error[E0391]: cycle detected when computing type of `Foo::N` + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11 + | +LL | trait Foo> { + | ^^^^^^^^^^^^^^^ + | +note: ...which requires computing type of `Bar::M`... + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:11 + | +LL | trait Bar> {} + | ^^^^^^^^^^^^^^^ + = note: ...which again requires computing type of `Foo::N`, completing the cycle +note: cycle used when computing explicit predicates of trait `Foo` + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:1 + | +LL | trait Foo> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information + +error[E0391]: cycle detected when computing type of `Foo::N` + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11 + | +LL | trait Foo> { + | ^^^^^^^^^^^^^^^ + | +note: ...which requires computing type of `Bar::M`... + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:11 + | +LL | trait Bar> {} + | ^^^^^^^^^^^^^^^ + = note: ...which again requires computing type of `Foo::N`, completing the cycle +note: cycle used when computing explicit predicates of trait `Foo` + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:1 + | +LL | trait Foo> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error[E0038]: the trait `Foo` cannot be made into an object + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:24 + | +LL | trait Foo> { + | ^ `Foo` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8 + | +LL | trait Foo> { + | --- this trait cannot be made into an object... +... +LL | fn func() { + | ^^^^ ...because associated function `func` has no `self` parameter +help: consider turning `func` into a method by giving it a `&self` argument + | +LL | fn func(&self) { + | +++++ +help: alternatively, consider constraining `func` so it does not apply to trait objects + | +LL | fn func() where Self: Sized { + | +++++++++++++++++ + +error[E0038]: the trait `Foo` cannot be made into an object + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11 + | +LL | trait Foo> { + | ^^^^^^^^^^^^^^^ `Foo` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8 + | +LL | trait Foo> { + | --- this trait cannot be made into an object... +... +LL | fn func() { + | ^^^^ ...because associated function `func` has no `self` parameter +help: consider turning `func` into a method by giving it a `&self` argument + | +LL | fn func(&self) { + | +++++ +help: alternatively, consider constraining `func` so it does not apply to trait objects + | +LL | fn func() where Self: Sized { + | +++++++++++++++++ + +error: `(dyn Bar<2> + 'static)` is forbidden as the type of a const generic parameter + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:20 + | +LL | trait Foo> { + | ^^^^^^ + | + = note: the only supported types are integers, `bool` and `char` + +error[E0038]: the trait `Foo` cannot be made into an object + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:11 + | +LL | trait Bar> {} + | ^^^^^^^^^^^^^^^ `Foo` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8 + | +LL | trait Foo> { + | --- this trait cannot be made into an object... +... +LL | fn func() { + | ^^^^ ...because associated function `func` has no `self` parameter +help: consider turning `func` into a method by giving it a `&self` argument + | +LL | fn func(&self) { + | +++++ +help: alternatively, consider constraining `func` so it does not apply to trait objects + | +LL | fn func() where Self: Sized { + | +++++++++++++++++ + +error: `(dyn Foo<2> + 'static)` is forbidden as the type of a const generic parameter + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:15:20 + | +LL | trait Bar> {} + | ^^^^^^ + | + = note: the only supported types are integers, `bool` and `char` + +error[E0038]: the trait `Foo` cannot be made into an object + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:2:11 + | +LL | trait Foo> { + | ^^^^^^^^^^^^^^^ `Foo` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/ice-hir-wf-check-anon-const-issue-122989.rs:11:8 + | +LL | trait Foo> { + | --- this trait cannot be made into an object... +... +LL | fn func() { + | ^^^^ ...because associated function `func` has no `self` parameter + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +help: consider turning `func` into a method by giving it a `&self` argument + | +LL | fn func(&self) { + | +++++ +help: alternatively, consider constraining `func` so it does not apply to trait objects + | +LL | fn func() where Self: Sized { + | +++++++++++++++++ + +error: aborting due to 8 previous errors; 2 warnings emitted + +Some errors have detailed explanations: E0038, E0391. +For more information about an error, try `rustc --explain E0038`.