diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs index f5a64ede398ea..0a2946323c7b3 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs @@ -9,6 +9,7 @@ use rustc_middle::ty::{ }; use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS; use rustc_span::kw; +use rustc_trait_selection::traits; use smallvec::SmallVec; use tracing::{debug, instrument}; @@ -535,9 +536,26 @@ pub(crate) fn check_generic_arg_count( .map(|param| param.name) .collect(); if constraint_names == param_names { + let has_assoc_ty_with_same_name = + if let DefKind::Trait = cx.tcx().def_kind(def_id) { + gen_args.constraints.iter().any(|constraint| { + traits::supertrait_def_ids(cx.tcx(), def_id).any(|trait_did| { + cx.probe_trait_that_defines_assoc_item( + trait_did, + ty::AssocTag::Type, + constraint.ident, + ) + }) + }) + } else { + false + }; // We set this to true and delay emitting `WrongNumberOfGenericArgs` - // to provide a succinct error for cases like issue #113073 - all_params_are_binded = true; + // to provide a succinct error for cases like issue #113073, + // but only if when we don't have any assoc type with the same name with a + // generic arg. Otherwise it will cause an ICE due to a delayed error because we + // don't have any error other than `WrongNumberOfGenericArgs`. + all_params_are_binded = !has_assoc_ty_with_same_name; }; } diff --git a/tests/run-make/dump-ice-to-disk/rmake.rs b/tests/run-make/dump-ice-to-disk/rmake.rs index 09a34cdeb5e70..319cbc6e0ca89 100644 --- a/tests/run-make/dump-ice-to-disk/rmake.rs +++ b/tests/run-make/dump-ice-to-disk/rmake.rs @@ -18,13 +18,16 @@ //! # Test history //! //! The previous rmake.rs iteration of this test was flaky for unknown reason on -//! `i686-pc-windows-gnu` *specifically*, so assertion failures in this test was made extremely -//! verbose to help diagnose why the ICE messages was different. It appears that backtraces on -//! `i686-pc-windows-gnu` specifically are quite unpredictable in how many backtrace frames are -//! involved. +//! `i686-pc-windows-gnu`, so assertion failures in this test was made extremely verbose to help +//! diagnose why the ICE messages was different. It appears that backtraces on `i686-pc-windows-gnu` +//! specifically are quite unpredictable in how many backtrace frames are involved. +//! +//! Disabled on `i686-pc-windows-msvc` as well, because sometimes the middle portion of the ICE +//! backtrace becomes ``. //@ ignore-cross-compile (exercising ICE dump on host) //@ ignore-i686-pc-windows-gnu (unwind mechanism produces unpredictable backtraces) +//@ ignore-i686-pc-windows-msvc (sometimes partial backtrace becomes ``) use std::cell::OnceCell; use std::path::{Path, PathBuf}; diff --git a/tests/ui/traits/associated_type_bound/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs b/tests/ui/traits/associated_type_bound/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs new file mode 100644 index 0000000000000..f1dffc0ff6b4e --- /dev/null +++ b/tests/ui/traits/associated_type_bound/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs @@ -0,0 +1,17 @@ +// A regression test for https://github.com/rust-lang/rust/issues/148121 + +pub trait Super { + type X; +} + +pub trait Zelf: Super {} + +pub trait A {} + +impl A for dyn Super {} +//~^ ERROR: trait takes 1 generic argument but 0 generic arguments were supplied + +impl A for dyn Zelf {} +//~^ ERROR: trait takes 1 generic argument but 0 generic arguments were supplied + +fn main() {} diff --git a/tests/ui/traits/associated_type_bound/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.stderr b/tests/ui/traits/associated_type_bound/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.stderr new file mode 100644 index 0000000000000..5a7969193b553 --- /dev/null +++ b/tests/ui/traits/associated_type_bound/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.stderr @@ -0,0 +1,35 @@ +error[E0107]: trait takes 1 generic argument but 0 generic arguments were supplied + --> $DIR/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs:11:16 + | +LL | impl A for dyn Super {} + | ^^^^^ expected 1 generic argument + | +note: trait defined here, with 1 generic parameter: `X` + --> $DIR/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs:3:11 + | +LL | pub trait Super { + | ^^^^^ - +help: add missing generic argument + | +LL | impl A for dyn Super {} + | ++ + +error[E0107]: trait takes 1 generic argument but 0 generic arguments were supplied + --> $DIR/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs:14:16 + | +LL | impl A for dyn Zelf {} + | ^^^^ expected 1 generic argument + | +note: trait defined here, with 1 generic parameter: `X` + --> $DIR/assoc-type-bounds-with-the-same-name-with-lacking-generic-arg-148121.rs:7:11 + | +LL | pub trait Zelf: Super {} + | ^^^^ - +help: add missing generic argument + | +LL | impl A for dyn Zelf {} + | ++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0107`.