From ba2c9baccbb22e97aefe46724a36f2f95908250e Mon Sep 17 00:00:00 2001 From: joboet Date: Thu, 25 Jun 2026 15:51:06 +0200 Subject: [PATCH 1/6] std: truncate thread names on NetBSD --- library/std/src/sys/thread/unix.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/std/src/sys/thread/unix.rs b/library/std/src/sys/thread/unix.rs index eb3fcff05ea9e..dd7b7940de510 100644 --- a/library/std/src/sys/thread/unix.rs +++ b/library/std/src/sys/thread/unix.rs @@ -392,6 +392,7 @@ pub fn current_os_id() -> Option { target_os = "vxworks", target_os = "cygwin", target_vendor = "apple", + target_os = "netbsd", ))] fn truncate_cstr(cstr: &CStr) -> [libc::c_char; MAX_WITH_NUL] { let mut result = [0; MAX_WITH_NUL]; @@ -463,7 +464,12 @@ pub fn set_name(name: &CStr) { #[cfg(target_os = "netbsd")] pub fn set_name(name: &CStr) { + // See https://github.com/NetBSD/src/blob/8d40872b4c550a802379f3b9c22a40212d5e149d/lib/libpthread/pthread.h#L281 + // FIXME: move to libc. + const PTHREAD_MAX_NAMELEN_NP: usize = 32; + unsafe { + let name = truncate_cstr::<{ PTHREAD_MAX_NAMELEN_NP }>(name); let res = libc::pthread_setname_np( libc::pthread_self(), c"%s".as_ptr(), From 4affbc4833824d0766b26137e89be33840d5ca00 Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Thu, 5 Feb 2026 12:55:37 +0100 Subject: [PATCH 2/6] Add supertrait item shadowing for type-level path resolution --- .../src/hir_ty_lowering/errors.rs | 6 +- .../src/hir_ty_lowering/mod.rs | 76 ++++++++++++++++++- .../supertrait-shadowing/assoc-const2.rs | 32 ++++++++ .../assoc-const2.run.stdout | 2 + .../supertrait-shadowing/assoc-type.rs | 47 ++++++++++++ .../assoc-type.run.stdout | 4 + 6 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 tests/ui/methods/supertrait-shadowing/assoc-const2.rs create mode 100644 tests/ui/methods/supertrait-shadowing/assoc-const2.run.stdout create mode 100644 tests/ui/methods/supertrait-shadowing/assoc-type.rs create mode 100644 tests/ui/methods/supertrait-shadowing/assoc-type.run.stdout diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 39402cfc35dce..32678e8a5a12f 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -409,9 +409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { pub(super) fn report_ambiguous_assoc_item( &self, - bound1: ty::PolyTraitRef<'tcx>, - bound2: ty::PolyTraitRef<'tcx>, - matching_candidates: impl Iterator>, + matching_candidates: &[ty::PolyTraitRef<'tcx>], qself: AssocItemQSelf, assoc_tag: ty::AssocTag, assoc_ident: Ident, @@ -443,7 +441,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // predicates!). // FIXME: Turn this into a structured, translatable & more actionable suggestion. let mut where_bounds = vec![]; - for bound in [bound1, bound2].into_iter().chain(matching_candidates) { + for &bound in matching_candidates { let bound_id = bound.def_id(); let assoc_item = tcx.associated_items(bound_id).find_by_ident_and_kind( tcx, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 20c35a62ef5bb..0ea280b3733ca 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -24,6 +24,7 @@ use std::{assert_matches, slice}; use rustc_abi::FIRST_VARIANT; use rustc_ast::LitKind; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; +use rustc_data_structures::sso::SsoHashSet; use rustc_errors::codes::*; use rustc_errors::{ Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, FatalError, StashKey, @@ -1260,6 +1261,69 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ) } + /// When there are multiple traits which contain an identically named + /// associated item, this function eliminates any traits which are a + /// supertrait of another candidate trait. + /// + /// This implements RFC #3624. + fn collapse_candidates_to_subtrait_pick( + &self, + matching_candidates: &[ty::PolyTraitRef<'tcx>], + ) -> Option> { + if !self.tcx().features().supertrait_item_shadowing() { + return None; + } + + let mut child_trait = matching_candidates[0]; + let mut supertraits: SsoHashSet<_> = + traits::supertrait_def_ids(self.tcx(), child_trait.def_id()).collect(); + + let mut remaining_candidates: Vec<_> = matching_candidates[1..].iter().copied().collect(); + while !remaining_candidates.is_empty() { + let mut made_progress = false; + let mut next_round = vec![]; + + for remaining_trait in remaining_candidates { + if supertraits.contains(&remaining_trait.def_id()) { + made_progress = true; + continue; + } + + // This pick is not a supertrait of the `child_pick`. + // Check if it's a subtrait of the `child_pick`, instead. + // If it is, then it must have been a subtrait of every + // other pick we've eliminated at this point. It will + // take over at this point. + let remaining_trait_supertraits: SsoHashSet<_> = + traits::supertrait_def_ids(self.tcx(), remaining_trait.def_id()).collect(); + if remaining_trait_supertraits.contains(&child_trait.def_id()) { + child_trait = remaining_trait; + supertraits = remaining_trait_supertraits; + made_progress = true; + continue; + } + + // `child_pick` is not a supertrait of this pick. + // Don't bail here, since we may be comparing two supertraits + // of a common subtrait. These two supertraits won't be related + // at all, but we will pick them up next round when we find their + // child as we continue iterating in this round. + next_round.push(remaining_trait); + } + + if made_progress { + // If we've made progress, iterate again. + remaining_candidates = next_round; + } else { + // Otherwise, we must have at least two candidates which + // are not related to each other at all.; + return None; + } + } + + Some(child_trait) + } + /// Search for a single trait bound whose trait defines the associated item given by /// `assoc_ident`. /// @@ -1294,10 +1358,16 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { }; if let Some(bound2) = matching_candidates.next() { + let all_matching_candidates: Vec<_> = + [bound1, bound2].into_iter().chain(matching_candidates).collect(); + if let Some(bound) = + self.collapse_candidates_to_subtrait_pick(&all_matching_candidates) + { + return Ok(bound); + } + return Err(self.report_ambiguous_assoc_item( - bound1, - bound2, - matching_candidates, + &all_matching_candidates, qself, assoc_tag, assoc_ident, diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const2.rs b/tests/ui/methods/supertrait-shadowing/assoc-const2.rs new file mode 100644 index 0000000000000..ebd0875eac8ea --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/assoc-const2.rs @@ -0,0 +1,32 @@ +//@ run-pass +//@ check-run-results + +#![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] +#![allow(dead_code)] + +trait A { + const CONST: i32; +} +impl A for T { + const CONST: i32 = 1; +} + +trait B: A { + type const CONST: i32; +} +impl B for T { + type const CONST: i32 = 2; +} + +trait C: B {} +impl C for T {} + +fn main() { + println!("{}", i32::CONST); + generic::(); +} + +fn generic>() { + println!("{}", T::CONST); +} diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const2.run.stdout b/tests/ui/methods/supertrait-shadowing/assoc-const2.run.stdout new file mode 100644 index 0000000000000..51993f072d583 --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/assoc-const2.run.stdout @@ -0,0 +1,2 @@ +2 +2 diff --git a/tests/ui/methods/supertrait-shadowing/assoc-type.rs b/tests/ui/methods/supertrait-shadowing/assoc-type.rs new file mode 100644 index 0000000000000..7434fa7523119 --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/assoc-type.rs @@ -0,0 +1,47 @@ +//@ run-pass +//@ check-run-results + +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +use std::mem::size_of; + +trait A { + type T; +} +impl A for T { + type T = i8; +} + +trait B: A { + type T; +} +impl B for T { + type T = i16; +} + +trait C: B {} +impl C for T {} + +fn main() { + generic::(); + generic2::(); + generic3::(); + generic4::(); +} + +fn generic() { + println!("{}", size_of::()); +} + +fn generic2>() { + println!("{}", size_of::()); +} + +fn generic3>() { + println!("{}", size_of::()); +} + +fn generic4>() { + println!("{}", size_of::()); +} diff --git a/tests/ui/methods/supertrait-shadowing/assoc-type.run.stdout b/tests/ui/methods/supertrait-shadowing/assoc-type.run.stdout new file mode 100644 index 0000000000000..6f9071418f37e --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/assoc-type.run.stdout @@ -0,0 +1,4 @@ +2 +1 +2 +2 From ec7def34e0e260c988a6cb01b94d3ccc403ea0fd Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Wed, 3 Jun 2026 17:29:02 +0100 Subject: [PATCH 3/6] Address review feedback --- .../src/hir_ty_lowering/mod.rs | 16 ++++++++++------ compiler/rustc_hir_typeck/src/method/probe.rs | 4 ++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 0ea280b3733ca..ef774c7f0b25a 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1265,6 +1265,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// associated item, this function eliminates any traits which are a /// supertrait of another candidate trait. /// + /// This is the type-level analogue of + /// `rustc_hir_typeck::method::probe::ProbeContext::collapse_candidates_to_subtrait_pick`; + /// keep both implementations in sync. + /// /// This implements RFC #3624. fn collapse_candidates_to_subtrait_pick( &self, @@ -1289,8 +1293,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { continue; } - // This pick is not a supertrait of the `child_pick`. - // Check if it's a subtrait of the `child_pick`, instead. + // This candidate is not a supertrait of the `child_trait`. + // Check if it's a subtrait of the `child_trait`, instead. // If it is, then it must have been a subtrait of every // other pick we've eliminated at this point. It will // take over at this point. @@ -1303,7 +1307,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { continue; } - // `child_pick` is not a supertrait of this pick. + // Neither `child_trait` or the current candidate are + // supertraits of each other. // Don't bail here, since we may be comparing two supertraits // of a common subtrait. These two supertraits won't be related // at all, but we will pick them up next round when we find their @@ -1316,7 +1321,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { remaining_candidates = next_round; } else { // Otherwise, we must have at least two candidates which - // are not related to each other at all.; + // are not related to each other at all. return None; } } @@ -1360,8 +1365,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { if let Some(bound2) = matching_candidates.next() { let all_matching_candidates: Vec<_> = [bound1, bound2].into_iter().chain(matching_candidates).collect(); - if let Some(bound) = - self.collapse_candidates_to_subtrait_pick(&all_matching_candidates) + if let Some(bound) = self.collapse_candidates_to_subtrait_pick(&all_matching_candidates) { return Ok(bound); } diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index e8921841c4405..ce187aa1aff14 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -2359,6 +2359,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { /// multiple conflicting picks if there is one pick whose trait container is a subtrait /// of the trait containers of all of the other picks. /// + /// This is the method-probe analogue of + /// `rustc_hir_analysis::hir_ty_lowering::HirTyLowerer::collapse_candidates_to_subtrait_pick`; + /// keep both implementations in sync. + /// /// This implements RFC #3624. fn collapse_candidates_to_subtrait_pick( &self, From 9e424acfd3942c9565d01993b3f16d6ae6a3d4ea Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Wed, 3 Jun 2026 17:33:44 +0100 Subject: [PATCH 4/6] Move supertrait shadowing tests to tests/ui/supertrait-shadowing --- tests/ui/{methods => }/supertrait-shadowing/assoc-const.rs | 0 tests/ui/{methods => }/supertrait-shadowing/assoc-const2.rs | 0 .../ui/{methods => }/supertrait-shadowing/assoc-const2.run.stdout | 0 tests/ui/{methods => }/supertrait-shadowing/assoc-type.rs | 0 tests/ui/{methods => }/supertrait-shadowing/assoc-type.run.stdout | 0 .../supertrait-shadowing/auxiliary/shadowed_stability.rs | 0 tests/ui/{methods => }/supertrait-shadowing/common-ancestor-2.rs | 0 .../{methods => }/supertrait-shadowing/common-ancestor-2.stderr | 0 tests/ui/{methods => }/supertrait-shadowing/common-ancestor-3.rs | 0 .../{methods => }/supertrait-shadowing/common-ancestor-3.stderr | 0 tests/ui/{methods => }/supertrait-shadowing/common-ancestor.rs | 0 .../ui/{methods => }/supertrait-shadowing/common-ancestor.stderr | 0 tests/ui/{methods => }/supertrait-shadowing/definition-site.rs | 0 .../ui/{methods => }/supertrait-shadowing/definition-site.stderr | 0 .../supertrait-shadowing/false-subtrait-after-inference.rs | 0 .../supertrait-shadowing/false-subtrait-after-inference.stderr | 0 .../ui/{methods => }/supertrait-shadowing/no-common-ancestor-2.rs | 0 .../supertrait-shadowing/no-common-ancestor-2.stderr | 0 tests/ui/{methods => }/supertrait-shadowing/no-common-ancestor.rs | 0 .../{methods => }/supertrait-shadowing/no-common-ancestor.stderr | 0 tests/ui/{methods => }/supertrait-shadowing/out-of-scope.rs | 0 .../supertrait-shadowing/trivially-false-subtrait.rs | 0 tests/ui/{methods => }/supertrait-shadowing/type-dependent.rs | 0 .../{methods => }/supertrait-shadowing/unstable.off_normal.stderr | 0 .../supertrait-shadowing/unstable.off_shadowing.stderr | 0 .../{methods => }/supertrait-shadowing/unstable.on_normal.stderr | 0 tests/ui/{methods => }/supertrait-shadowing/unstable.rs | 0 27 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/{methods => }/supertrait-shadowing/assoc-const.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/assoc-const2.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/assoc-const2.run.stdout (100%) rename tests/ui/{methods => }/supertrait-shadowing/assoc-type.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/assoc-type.run.stdout (100%) rename tests/ui/{methods => }/supertrait-shadowing/auxiliary/shadowed_stability.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/common-ancestor-2.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/common-ancestor-2.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/common-ancestor-3.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/common-ancestor-3.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/common-ancestor.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/common-ancestor.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/definition-site.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/definition-site.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/false-subtrait-after-inference.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/false-subtrait-after-inference.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/no-common-ancestor-2.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/no-common-ancestor-2.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/no-common-ancestor.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/no-common-ancestor.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/out-of-scope.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/trivially-false-subtrait.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/type-dependent.rs (100%) rename tests/ui/{methods => }/supertrait-shadowing/unstable.off_normal.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/unstable.off_shadowing.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/unstable.on_normal.stderr (100%) rename tests/ui/{methods => }/supertrait-shadowing/unstable.rs (100%) diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const.rs b/tests/ui/supertrait-shadowing/assoc-const.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/assoc-const.rs rename to tests/ui/supertrait-shadowing/assoc-const.rs diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const2.rs b/tests/ui/supertrait-shadowing/assoc-const2.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/assoc-const2.rs rename to tests/ui/supertrait-shadowing/assoc-const2.rs diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const2.run.stdout b/tests/ui/supertrait-shadowing/assoc-const2.run.stdout similarity index 100% rename from tests/ui/methods/supertrait-shadowing/assoc-const2.run.stdout rename to tests/ui/supertrait-shadowing/assoc-const2.run.stdout diff --git a/tests/ui/methods/supertrait-shadowing/assoc-type.rs b/tests/ui/supertrait-shadowing/assoc-type.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/assoc-type.rs rename to tests/ui/supertrait-shadowing/assoc-type.rs diff --git a/tests/ui/methods/supertrait-shadowing/assoc-type.run.stdout b/tests/ui/supertrait-shadowing/assoc-type.run.stdout similarity index 100% rename from tests/ui/methods/supertrait-shadowing/assoc-type.run.stdout rename to tests/ui/supertrait-shadowing/assoc-type.run.stdout diff --git a/tests/ui/methods/supertrait-shadowing/auxiliary/shadowed_stability.rs b/tests/ui/supertrait-shadowing/auxiliary/shadowed_stability.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/auxiliary/shadowed_stability.rs rename to tests/ui/supertrait-shadowing/auxiliary/shadowed_stability.rs diff --git a/tests/ui/methods/supertrait-shadowing/common-ancestor-2.rs b/tests/ui/supertrait-shadowing/common-ancestor-2.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/common-ancestor-2.rs rename to tests/ui/supertrait-shadowing/common-ancestor-2.rs diff --git a/tests/ui/methods/supertrait-shadowing/common-ancestor-2.stderr b/tests/ui/supertrait-shadowing/common-ancestor-2.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/common-ancestor-2.stderr rename to tests/ui/supertrait-shadowing/common-ancestor-2.stderr diff --git a/tests/ui/methods/supertrait-shadowing/common-ancestor-3.rs b/tests/ui/supertrait-shadowing/common-ancestor-3.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/common-ancestor-3.rs rename to tests/ui/supertrait-shadowing/common-ancestor-3.rs diff --git a/tests/ui/methods/supertrait-shadowing/common-ancestor-3.stderr b/tests/ui/supertrait-shadowing/common-ancestor-3.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/common-ancestor-3.stderr rename to tests/ui/supertrait-shadowing/common-ancestor-3.stderr diff --git a/tests/ui/methods/supertrait-shadowing/common-ancestor.rs b/tests/ui/supertrait-shadowing/common-ancestor.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/common-ancestor.rs rename to tests/ui/supertrait-shadowing/common-ancestor.rs diff --git a/tests/ui/methods/supertrait-shadowing/common-ancestor.stderr b/tests/ui/supertrait-shadowing/common-ancestor.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/common-ancestor.stderr rename to tests/ui/supertrait-shadowing/common-ancestor.stderr diff --git a/tests/ui/methods/supertrait-shadowing/definition-site.rs b/tests/ui/supertrait-shadowing/definition-site.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/definition-site.rs rename to tests/ui/supertrait-shadowing/definition-site.rs diff --git a/tests/ui/methods/supertrait-shadowing/definition-site.stderr b/tests/ui/supertrait-shadowing/definition-site.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/definition-site.stderr rename to tests/ui/supertrait-shadowing/definition-site.stderr diff --git a/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.rs b/tests/ui/supertrait-shadowing/false-subtrait-after-inference.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.rs rename to tests/ui/supertrait-shadowing/false-subtrait-after-inference.rs diff --git a/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.stderr b/tests/ui/supertrait-shadowing/false-subtrait-after-inference.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.stderr rename to tests/ui/supertrait-shadowing/false-subtrait-after-inference.stderr diff --git a/tests/ui/methods/supertrait-shadowing/no-common-ancestor-2.rs b/tests/ui/supertrait-shadowing/no-common-ancestor-2.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/no-common-ancestor-2.rs rename to tests/ui/supertrait-shadowing/no-common-ancestor-2.rs diff --git a/tests/ui/methods/supertrait-shadowing/no-common-ancestor-2.stderr b/tests/ui/supertrait-shadowing/no-common-ancestor-2.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/no-common-ancestor-2.stderr rename to tests/ui/supertrait-shadowing/no-common-ancestor-2.stderr diff --git a/tests/ui/methods/supertrait-shadowing/no-common-ancestor.rs b/tests/ui/supertrait-shadowing/no-common-ancestor.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/no-common-ancestor.rs rename to tests/ui/supertrait-shadowing/no-common-ancestor.rs diff --git a/tests/ui/methods/supertrait-shadowing/no-common-ancestor.stderr b/tests/ui/supertrait-shadowing/no-common-ancestor.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/no-common-ancestor.stderr rename to tests/ui/supertrait-shadowing/no-common-ancestor.stderr diff --git a/tests/ui/methods/supertrait-shadowing/out-of-scope.rs b/tests/ui/supertrait-shadowing/out-of-scope.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/out-of-scope.rs rename to tests/ui/supertrait-shadowing/out-of-scope.rs diff --git a/tests/ui/methods/supertrait-shadowing/trivially-false-subtrait.rs b/tests/ui/supertrait-shadowing/trivially-false-subtrait.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/trivially-false-subtrait.rs rename to tests/ui/supertrait-shadowing/trivially-false-subtrait.rs diff --git a/tests/ui/methods/supertrait-shadowing/type-dependent.rs b/tests/ui/supertrait-shadowing/type-dependent.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/type-dependent.rs rename to tests/ui/supertrait-shadowing/type-dependent.rs diff --git a/tests/ui/methods/supertrait-shadowing/unstable.off_normal.stderr b/tests/ui/supertrait-shadowing/unstable.off_normal.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/unstable.off_normal.stderr rename to tests/ui/supertrait-shadowing/unstable.off_normal.stderr diff --git a/tests/ui/methods/supertrait-shadowing/unstable.off_shadowing.stderr b/tests/ui/supertrait-shadowing/unstable.off_shadowing.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/unstable.off_shadowing.stderr rename to tests/ui/supertrait-shadowing/unstable.off_shadowing.stderr diff --git a/tests/ui/methods/supertrait-shadowing/unstable.on_normal.stderr b/tests/ui/supertrait-shadowing/unstable.on_normal.stderr similarity index 100% rename from tests/ui/methods/supertrait-shadowing/unstable.on_normal.stderr rename to tests/ui/supertrait-shadowing/unstable.on_normal.stderr diff --git a/tests/ui/methods/supertrait-shadowing/unstable.rs b/tests/ui/supertrait-shadowing/unstable.rs similarity index 100% rename from tests/ui/methods/supertrait-shadowing/unstable.rs rename to tests/ui/supertrait-shadowing/unstable.rs From 1ab68623f32de3abff87c56c47bb3753ef41eb8f Mon Sep 17 00:00:00 2001 From: Amanieu d'Antras Date: Thu, 4 Jun 2026 17:03:02 +0100 Subject: [PATCH 5/6] Address review feedback for tests --- tests/ui/README.md | 4 + tests/ui/supertrait-shadowing/assoc-const.rs | 15 ++- tests/ui/supertrait-shadowing/assoc-const2.rs | 32 ------- .../assoc-const2.run.stdout | 2 - .../supertrait-shadowing/assoc-type-fail.rs | 47 +++++++++ .../assoc-type-fail.stderr | 37 ++++++++ .../assoc-type-predicates.rs | 45 +++++++++ .../assoc-type-predicates.stderr | 32 +++++++ tests/ui/supertrait-shadowing/assoc-type.rs | 30 +++--- .../assoc-type.run.stdout | 4 - .../supertrait-shadowing/common-ancestor-2.rs | 32 ++++++- .../common-ancestor-2.stderr | 46 +++++++-- .../supertrait-shadowing/common-ancestor-3.rs | 41 +++++++- .../common-ancestor-3.stderr | 86 +++++++++++++++-- .../supertrait-shadowing/common-ancestor.rs | 25 ++++- .../common-ancestor.stderr | 40 ++++++-- .../supertrait-shadowing/definition-site.rs | 10 ++ .../definition-site.stderr | 60 +++++++++++- .../no-common-ancestor-2.rs | 39 +++++++- .../no-common-ancestor-2.stderr | 95 +++++++++++++++++-- .../no-common-ancestor.rs | 25 ++++- .../no-common-ancestor.stderr | 59 +++++++++++- tests/ui/supertrait-shadowing/out-of-scope.rs | 23 ++++- .../ui/supertrait-shadowing/type-dependent.rs | 29 +++++- 24 files changed, 744 insertions(+), 114 deletions(-) delete mode 100644 tests/ui/supertrait-shadowing/assoc-const2.rs delete mode 100644 tests/ui/supertrait-shadowing/assoc-const2.run.stdout create mode 100644 tests/ui/supertrait-shadowing/assoc-type-fail.rs create mode 100644 tests/ui/supertrait-shadowing/assoc-type-fail.stderr create mode 100644 tests/ui/supertrait-shadowing/assoc-type-predicates.rs create mode 100644 tests/ui/supertrait-shadowing/assoc-type-predicates.stderr delete mode 100644 tests/ui/supertrait-shadowing/assoc-type.run.stdout diff --git a/tests/ui/README.md b/tests/ui/README.md index 6ffef1a1cd699..d342f393d0550 100644 --- a/tests/ui/README.md +++ b/tests/ui/README.md @@ -1351,6 +1351,10 @@ Generic collection of tests for suggestions, when no more specific directories a **FIXME**: Some overlap with `tests/ui/did_you_mean/`, that directory should probably be moved under here. +## `tests/ui/supertrait-shadowing/` + +Tests for supertrait item shadowing (RFC 3624). + ## `tests/ui/svh/`: Strict Version Hash Tests on the *Strict Version Hash* (SVH, also known as the "crate hash"). diff --git a/tests/ui/supertrait-shadowing/assoc-const.rs b/tests/ui/supertrait-shadowing/assoc-const.rs index 01d045b9fb74f..be0d990284b0c 100644 --- a/tests/ui/supertrait-shadowing/assoc-const.rs +++ b/tests/ui/supertrait-shadowing/assoc-const.rs @@ -1,6 +1,7 @@ //@ run-pass #![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] #![allow(dead_code)] trait A { @@ -11,12 +12,20 @@ impl A for T { } trait B: A { - const CONST: i32; + type const CONST: i32; } impl B for T { - const CONST: i32 = 2; + type const CONST: i32 = 2; } +trait C: B {} +impl C for T {} + fn main() { - assert_eq!(i32::CONST, 2) + assert_eq!(i32::CONST, 2); + generic::(); +} + +fn generic>() { + assert_eq!(T::CONST, 2); } diff --git a/tests/ui/supertrait-shadowing/assoc-const2.rs b/tests/ui/supertrait-shadowing/assoc-const2.rs deleted file mode 100644 index ebd0875eac8ea..0000000000000 --- a/tests/ui/supertrait-shadowing/assoc-const2.rs +++ /dev/null @@ -1,32 +0,0 @@ -//@ run-pass -//@ check-run-results - -#![feature(supertrait_item_shadowing)] -#![feature(min_generic_const_args)] -#![allow(dead_code)] - -trait A { - const CONST: i32; -} -impl A for T { - const CONST: i32 = 1; -} - -trait B: A { - type const CONST: i32; -} -impl B for T { - type const CONST: i32 = 2; -} - -trait C: B {} -impl C for T {} - -fn main() { - println!("{}", i32::CONST); - generic::(); -} - -fn generic>() { - println!("{}", T::CONST); -} diff --git a/tests/ui/supertrait-shadowing/assoc-const2.run.stdout b/tests/ui/supertrait-shadowing/assoc-const2.run.stdout deleted file mode 100644 index 51993f072d583..0000000000000 --- a/tests/ui/supertrait-shadowing/assoc-const2.run.stdout +++ /dev/null @@ -1,2 +0,0 @@ -2 -2 diff --git a/tests/ui/supertrait-shadowing/assoc-type-fail.rs b/tests/ui/supertrait-shadowing/assoc-type-fail.rs new file mode 100644 index 0000000000000..5df4edb7e96e2 --- /dev/null +++ b/tests/ui/supertrait-shadowing/assoc-type-fail.rs @@ -0,0 +1,47 @@ +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +use std::mem::size_of; + +trait A { + type Assoc; +} +impl A for X { + type Assoc = i8; +} + +trait B: A { + type Assoc; +} +impl B for X { + type Assoc = i16; +} + +trait C: B {} +impl C for X {} + +fn main() { + b_unbound::(); + c_unbound::(); + + b_assoc_is_a::(); + //~^ ERROR type mismatch resolving `::Assoc == i8` + c_assoc_is_a::(); + //~^ ERROR type mismatch resolving `::Assoc == i8` +} + +fn b_unbound() { + let _ = size_of::(); +} + +fn c_unbound() { + let _ = size_of::(); +} + +fn b_assoc_is_a>() { + let _ = size_of::(); +} + +fn c_assoc_is_a>() { + let _ = size_of::(); +} diff --git a/tests/ui/supertrait-shadowing/assoc-type-fail.stderr b/tests/ui/supertrait-shadowing/assoc-type-fail.stderr new file mode 100644 index 0000000000000..36a9bd082aaf6 --- /dev/null +++ b/tests/ui/supertrait-shadowing/assoc-type-fail.stderr @@ -0,0 +1,37 @@ +error[E0271]: type mismatch resolving `::Assoc == i8` + --> $DIR/assoc-type-fail.rs:27:20 + | +LL | b_assoc_is_a::(); + | ^^^ type mismatch resolving `::Assoc == i8` + | +note: expected this to be `i8` + --> $DIR/assoc-type-fail.rs:17:18 + | +LL | type Assoc = i16; + | ^^^ +note: required by a bound in `b_assoc_is_a` + --> $DIR/assoc-type-fail.rs:41:22 + | +LL | fn b_assoc_is_a>() { + | ^^^^^^^^^^ required by this bound in `b_assoc_is_a` + +error[E0271]: type mismatch resolving `::Assoc == i8` + --> $DIR/assoc-type-fail.rs:29:20 + | +LL | c_assoc_is_a::(); + | ^^^ type mismatch resolving `::Assoc == i8` + | +note: expected this to be `i8` + --> $DIR/assoc-type-fail.rs:17:18 + | +LL | type Assoc = i16; + | ^^^ +note: required by a bound in `c_assoc_is_a` + --> $DIR/assoc-type-fail.rs:45:22 + | +LL | fn c_assoc_is_a>() { + | ^^^^^^^^^^ required by this bound in `c_assoc_is_a` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0271`. diff --git a/tests/ui/supertrait-shadowing/assoc-type-predicates.rs b/tests/ui/supertrait-shadowing/assoc-type-predicates.rs new file mode 100644 index 0000000000000..55d2b84a50f9a --- /dev/null +++ b/tests/ui/supertrait-shadowing/assoc-type-predicates.rs @@ -0,0 +1,45 @@ +//@ normalize-stderr: "assoc_type_predicates\[[^\]]+\]" -> "assoc_type_predicates[HASH]" + +#![feature(rustc_attrs)] +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +trait A { + type Assoc; +} +impl A for T { + type Assoc = i8; +} + +trait B: A { + type Assoc; +} +impl B for T { + type Assoc = i16; +} + +trait C: B {} +impl C for T {} + +#[rustc_dump_predicates] +fn a_bound>() {} +//~^ ERROR rustc_dump_predicates +//~| NOTE TraitPredicate( +//~| NOTE TraitPredicate( +//~| NOTE A::Assoc + +#[rustc_dump_predicates] +fn b_bound>() {} +//~^ ERROR rustc_dump_predicates +//~| NOTE TraitPredicate( +//~| NOTE TraitPredicate( +//~| NOTE B::Assoc + +#[rustc_dump_predicates] +fn c_bound>() {} +//~^ ERROR rustc_dump_predicates +//~| NOTE TraitPredicate( +//~| NOTE TraitPredicate( +//~| NOTE B::Assoc + +fn main() {} diff --git a/tests/ui/supertrait-shadowing/assoc-type-predicates.stderr b/tests/ui/supertrait-shadowing/assoc-type-predicates.stderr new file mode 100644 index 0000000000000..be765e737a6e1 --- /dev/null +++ b/tests/ui/supertrait-shadowing/assoc-type-predicates.stderr @@ -0,0 +1,32 @@ +error: rustc_dump_predicates + --> $DIR/assoc-type-predicates.rs:25:1 + | +LL | fn a_bound>() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: Binder { value: TraitPredicate(, polarity:Positive), bound_vars: [] } + = note: Binder { value: TraitPredicate(, polarity:Positive), bound_vars: [] } + = note: Binder { value: ProjectionPredicate(Alias { kind: ProjectionTy { def_id: DefId(0:4 ~ assoc_type_predicates[HASH]::A::Assoc) }, args: [T/#0], .. }, Term::Ty(i8)), bound_vars: [] } + +error: rustc_dump_predicates + --> $DIR/assoc-type-predicates.rs:32:1 + | +LL | fn b_bound>() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: Binder { value: TraitPredicate(, polarity:Positive), bound_vars: [] } + = note: Binder { value: TraitPredicate(, polarity:Positive), bound_vars: [] } + = note: Binder { value: ProjectionPredicate(Alias { kind: ProjectionTy { def_id: DefId(0:9 ~ assoc_type_predicates[HASH]::B::Assoc) }, args: [T/#0], .. }, Term::Ty(i16)), bound_vars: [] } + +error: rustc_dump_predicates + --> $DIR/assoc-type-predicates.rs:39:1 + | +LL | fn c_bound>() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: Binder { value: TraitPredicate(, polarity:Positive), bound_vars: [] } + = note: Binder { value: TraitPredicate(, polarity:Positive), bound_vars: [] } + = note: Binder { value: ProjectionPredicate(Alias { kind: ProjectionTy { def_id: DefId(0:9 ~ assoc_type_predicates[HASH]::B::Assoc) }, args: [T/#0], .. }, Term::Ty(i16)), bound_vars: [] } + +error: aborting due to 3 previous errors + diff --git a/tests/ui/supertrait-shadowing/assoc-type.rs b/tests/ui/supertrait-shadowing/assoc-type.rs index 7434fa7523119..9e9799397c571 100644 --- a/tests/ui/supertrait-shadowing/assoc-type.rs +++ b/tests/ui/supertrait-shadowing/assoc-type.rs @@ -7,17 +7,17 @@ use std::mem::size_of; trait A { - type T; + type Assoc; } impl A for T { - type T = i8; + type Assoc = i8; } trait B: A { - type T; + type Assoc; } impl B for T { - type T = i16; + type Assoc = i16; } trait C: B {} @@ -28,20 +28,26 @@ fn main() { generic2::(); generic3::(); generic4::(); + generic5::(); } -fn generic() { - println!("{}", size_of::()); +fn generic() { + assert_eq!(size_of::(), 2); } -fn generic2>() { - println!("{}", size_of::()); +fn generic2>() { + assert_eq!(size_of::(), 1); } -fn generic3>() { - println!("{}", size_of::()); +fn generic3>() { + assert_eq!(size_of::(), 2); } -fn generic4>() { - println!("{}", size_of::()); +fn generic4>() { + assert_eq!(size_of::(), 2); +} + +fn generic5() { + assert_eq!(size_of::<::Assoc>(), 1); + assert_eq!(size_of::<::Assoc>(), 2); } diff --git a/tests/ui/supertrait-shadowing/assoc-type.run.stdout b/tests/ui/supertrait-shadowing/assoc-type.run.stdout deleted file mode 100644 index 6f9071418f37e..0000000000000 --- a/tests/ui/supertrait-shadowing/assoc-type.run.stdout +++ /dev/null @@ -1,4 +0,0 @@ -2 -1 -2 -2 diff --git a/tests/ui/supertrait-shadowing/common-ancestor-2.rs b/tests/ui/supertrait-shadowing/common-ancestor-2.rs index ce56c25df19ea..908c5d2bf2199 100644 --- a/tests/ui/supertrait-shadowing/common-ancestor-2.rs +++ b/tests/ui/supertrait-shadowing/common-ancestor-2.rs @@ -1,33 +1,59 @@ //@ run-pass #![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] #![warn(resolving_to_items_shadowing_supertrait_items)] #![warn(shadowing_supertrait_items)] #![allow(dead_code)] +use std::mem::size_of; + trait A { fn hello(&self) -> &'static str { "A" } + type Assoc; + const CONST: i32; +} +impl A for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl A for T {} trait B { fn hello(&self) -> &'static str { "B" } + type Assoc; + const CONST: i32; +} +impl B for T { + type Assoc = i16; + const CONST: i32 = 2; } -impl B for T {} trait C: A + B { fn hello(&self) -> &'static str { //~^ WARN trait item `hello` from `C` shadows identically named item "C" } + type Assoc; + //~^ WARN trait item `Assoc` from `C` shadows identically named item + type const CONST: i32; + //~^ WARN trait item `CONST` from `C` shadows identically named item +} +impl C for T { + type Assoc = i32; + type const CONST: i32 = 3; } -impl C for T {} fn main() { assert_eq!(().hello(), "C"); //~^ WARN trait item `hello` from `C` shadows identically named item from supertrait + check::<()>(); +} + +fn check() { + assert_eq!(size_of::(), 4); + assert_eq!(T::CONST, 3); } diff --git a/tests/ui/supertrait-shadowing/common-ancestor-2.stderr b/tests/ui/supertrait-shadowing/common-ancestor-2.stderr index b0f61b46b6911..646726b835041 100644 --- a/tests/ui/supertrait-shadowing/common-ancestor-2.stderr +++ b/tests/ui/supertrait-shadowing/common-ancestor-2.stderr @@ -1,11 +1,11 @@ warning: trait item `hello` from `C` shadows identically named item from supertrait - --> $DIR/common-ancestor-2.rs:23:5 + --> $DIR/common-ancestor-2.rs:36:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: items from several supertraits are shadowed: `B` and `A` - --> $DIR/common-ancestor-2.rs:9:5 + --> $DIR/common-ancestor-2.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -13,24 +13,54 @@ LL | fn hello(&self) -> &'static str { LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: the lint level is defined here - --> $DIR/common-ancestor-2.rs:5:9 + --> $DIR/common-ancestor-2.rs:6:9 | LL | #![warn(shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: trait item `Assoc` from `C` shadows identically named item from supertrait + --> $DIR/common-ancestor-2.rs:40:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `B` and `A` + --> $DIR/common-ancestor-2.rs:15:5 + | +LL | type Assoc; + | ^^^^^^^^^^ +... +LL | type Assoc; + | ^^^^^^^^^^ + +warning: trait item `CONST` from `C` shadows identically named item from supertrait + --> $DIR/common-ancestor-2.rs:42:5 + | +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `B` and `A` + --> $DIR/common-ancestor-2.rs:16:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +... +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + warning: trait item `hello` from `C` shadows identically named item from supertrait - --> $DIR/common-ancestor-2.rs:31:19 + --> $DIR/common-ancestor-2.rs:51:19 | LL | assert_eq!(().hello(), "C"); | ^^^^^ | note: item from `C` shadows a supertrait item - --> $DIR/common-ancestor-2.rs:23:5 + --> $DIR/common-ancestor-2.rs:36:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: items from several supertraits are shadowed: `A` and `B` - --> $DIR/common-ancestor-2.rs:9:5 + --> $DIR/common-ancestor-2.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -38,10 +68,10 @@ LL | fn hello(&self) -> &'static str { LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: the lint level is defined here - --> $DIR/common-ancestor-2.rs:4:9 + --> $DIR/common-ancestor-2.rs:5:9 | LL | #![warn(resolving_to_items_shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -warning: 2 warnings emitted +warning: 4 warnings emitted diff --git a/tests/ui/supertrait-shadowing/common-ancestor-3.rs b/tests/ui/supertrait-shadowing/common-ancestor-3.rs index b29f3c8d014ec..ade23ca88aec2 100644 --- a/tests/ui/supertrait-shadowing/common-ancestor-3.rs +++ b/tests/ui/supertrait-shadowing/common-ancestor-3.rs @@ -1,31 +1,51 @@ //@ run-pass #![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] #![warn(resolving_to_items_shadowing_supertrait_items)] #![warn(shadowing_supertrait_items)] #![allow(dead_code)] +use std::mem::size_of; + trait A { fn hello(&self) -> &'static str { "A" } + type Assoc; + const CONST: i32; +} +impl A for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl A for T {} trait B { fn hello(&self) -> &'static str { "B" } + type Assoc; + const CONST: i32; +} +impl B for T { + type Assoc = i16; + const CONST: i32 = 2; } -impl B for T {} trait C: A + B { fn hello(&self) -> &'static str { //~^ WARN trait item `hello` from `C` shadows identically named item "C" } + type Assoc; + //~^ WARN trait item `Assoc` from `C` shadows identically named item + type const CONST: i32; + //~^ WARN trait item `CONST` from `C` shadows identically named item +} +impl C for T { + type Assoc = i32; + type const CONST: i32 = 3; } -impl C for T {} // `D` extends `C` which extends `B` and `A` @@ -34,10 +54,23 @@ trait D: C { //~^ WARN trait item `hello` from `D` shadows identically named item "D" } + type Assoc; + //~^ WARN trait item `Assoc` from `D` shadows identically named item + type const CONST: i32; + //~^ WARN trait item `CONST` from `D` shadows identically named item +} +impl D for T { + type Assoc = i64; + type const CONST: i32 = 4; } -impl D for T {} fn main() { assert_eq!(().hello(), "D"); //~^ WARN trait item `hello` from `D` shadows identically named item from supertrait + check::<()>(); +} + +fn check() { + assert_eq!(size_of::(), 8); + assert_eq!(T::CONST, 4); } diff --git a/tests/ui/supertrait-shadowing/common-ancestor-3.stderr b/tests/ui/supertrait-shadowing/common-ancestor-3.stderr index 28fe7f72f94c3..62132832da4ba 100644 --- a/tests/ui/supertrait-shadowing/common-ancestor-3.stderr +++ b/tests/ui/supertrait-shadowing/common-ancestor-3.stderr @@ -1,11 +1,11 @@ warning: trait item `hello` from `C` shadows identically named item from supertrait - --> $DIR/common-ancestor-3.rs:23:5 + --> $DIR/common-ancestor-3.rs:36:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: items from several supertraits are shadowed: `B` and `A` - --> $DIR/common-ancestor-3.rs:9:5 + --> $DIR/common-ancestor-3.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -13,19 +13,49 @@ LL | fn hello(&self) -> &'static str { LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: the lint level is defined here - --> $DIR/common-ancestor-3.rs:5:9 + --> $DIR/common-ancestor-3.rs:6:9 | LL | #![warn(shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: trait item `Assoc` from `C` shadows identically named item from supertrait + --> $DIR/common-ancestor-3.rs:40:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `B` and `A` + --> $DIR/common-ancestor-3.rs:15:5 + | +LL | type Assoc; + | ^^^^^^^^^^ +... +LL | type Assoc; + | ^^^^^^^^^^ + +warning: trait item `CONST` from `C` shadows identically named item from supertrait + --> $DIR/common-ancestor-3.rs:42:5 + | +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `B` and `A` + --> $DIR/common-ancestor-3.rs:16:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +... +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + warning: trait item `hello` from `D` shadows identically named item from supertrait - --> $DIR/common-ancestor-3.rs:33:5 + --> $DIR/common-ancestor-3.rs:53:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: items from several supertraits are shadowed: `C`, `B`, and `A` - --> $DIR/common-ancestor-3.rs:9:5 + --> $DIR/common-ancestor-3.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -36,19 +66,55 @@ LL | fn hello(&self) -> &'static str { LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: trait item `Assoc` from `D` shadows identically named item from supertrait + --> $DIR/common-ancestor-3.rs:57:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `C`, `B`, and `A` + --> $DIR/common-ancestor-3.rs:15:5 + | +LL | type Assoc; + | ^^^^^^^^^^ +... +LL | type Assoc; + | ^^^^^^^^^^ +... +LL | type Assoc; + | ^^^^^^^^^^ + +warning: trait item `CONST` from `D` shadows identically named item from supertrait + --> $DIR/common-ancestor-3.rs:59:5 + | +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `C`, `B`, and `A` + --> $DIR/common-ancestor-3.rs:16:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +... +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +... +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ + warning: trait item `hello` from `D` shadows identically named item from supertrait - --> $DIR/common-ancestor-3.rs:41:19 + --> $DIR/common-ancestor-3.rs:68:19 | LL | assert_eq!(().hello(), "D"); | ^^^^^ | note: item from `D` shadows a supertrait item - --> $DIR/common-ancestor-3.rs:33:5 + --> $DIR/common-ancestor-3.rs:53:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: items from several supertraits are shadowed: `A`, `B`, and `C` - --> $DIR/common-ancestor-3.rs:9:5 + --> $DIR/common-ancestor-3.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -59,10 +125,10 @@ LL | fn hello(&self) -> &'static str { LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: the lint level is defined here - --> $DIR/common-ancestor-3.rs:4:9 + --> $DIR/common-ancestor-3.rs:5:9 | LL | #![warn(resolving_to_items_shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -warning: 3 warnings emitted +warning: 7 warnings emitted diff --git a/tests/ui/supertrait-shadowing/common-ancestor.rs b/tests/ui/supertrait-shadowing/common-ancestor.rs index b288d6e22b8c3..903f85957870b 100644 --- a/tests/ui/supertrait-shadowing/common-ancestor.rs +++ b/tests/ui/supertrait-shadowing/common-ancestor.rs @@ -1,26 +1,47 @@ //@ run-pass #![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] #![warn(resolving_to_items_shadowing_supertrait_items)] #![warn(shadowing_supertrait_items)] #![allow(dead_code)] +use std::mem::size_of; + trait A { fn hello(&self) -> &'static str { "A" } + type Assoc; + const CONST: i32; +} +impl A for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl A for T {} trait B: A { fn hello(&self) -> &'static str { //~^ WARN trait item `hello` from `B` shadows identically named item "B" } + type Assoc; + //~^ WARN trait item `Assoc` from `B` shadows identically named item + type const CONST: i32; + //~^ WARN trait item `CONST` from `B` shadows identically named item +} +impl B for T { + type Assoc = i16; + type const CONST: i32 = 2; } -impl B for T {} fn main() { assert_eq!(().hello(), "B"); //~^ WARN trait item `hello` from `B` shadows identically named item from supertrait + check::<()>(); +} + +fn check() { + assert_eq!(size_of::(), 2); + assert_eq!(T::CONST, 2); } diff --git a/tests/ui/supertrait-shadowing/common-ancestor.stderr b/tests/ui/supertrait-shadowing/common-ancestor.stderr index 9afedeab5e2c2..9b13537cf800c 100644 --- a/tests/ui/supertrait-shadowing/common-ancestor.stderr +++ b/tests/ui/supertrait-shadowing/common-ancestor.stderr @@ -1,41 +1,65 @@ warning: trait item `hello` from `B` shadows identically named item from supertrait - --> $DIR/common-ancestor.rs:16:5 + --> $DIR/common-ancestor.rs:24:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: item from `A` is shadowed by a subtrait item - --> $DIR/common-ancestor.rs:9:5 + --> $DIR/common-ancestor.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: the lint level is defined here - --> $DIR/common-ancestor.rs:5:9 + --> $DIR/common-ancestor.rs:6:9 | LL | #![warn(shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +warning: trait item `Assoc` from `B` shadows identically named item from supertrait + --> $DIR/common-ancestor.rs:28:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + | +note: item from `A` is shadowed by a subtrait item + --> $DIR/common-ancestor.rs:15:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + +warning: trait item `CONST` from `B` shadows identically named item from supertrait + --> $DIR/common-ancestor.rs:30:5 + | +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ + | +note: item from `A` is shadowed by a subtrait item + --> $DIR/common-ancestor.rs:16:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + warning: trait item `hello` from `B` shadows identically named item from supertrait - --> $DIR/common-ancestor.rs:24:19 + --> $DIR/common-ancestor.rs:39:19 | LL | assert_eq!(().hello(), "B"); | ^^^^^ | note: item from `B` shadows a supertrait item - --> $DIR/common-ancestor.rs:16:5 + --> $DIR/common-ancestor.rs:24:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: item from `A` is shadowed by a subtrait item - --> $DIR/common-ancestor.rs:9:5 + --> $DIR/common-ancestor.rs:12:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: the lint level is defined here - --> $DIR/common-ancestor.rs:4:9 + --> $DIR/common-ancestor.rs:5:9 | LL | #![warn(resolving_to_items_shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -warning: 2 warnings emitted +warning: 4 warnings emitted diff --git a/tests/ui/supertrait-shadowing/definition-site.rs b/tests/ui/supertrait-shadowing/definition-site.rs index 248df032736f6..3309ee97e9f00 100644 --- a/tests/ui/supertrait-shadowing/definition-site.rs +++ b/tests/ui/supertrait-shadowing/definition-site.rs @@ -3,16 +3,26 @@ trait SuperSuper { fn method(); + const CONST: i32; + type Assoc; } trait Super: SuperSuper { fn method(); //~^ ERROR trait item `method` from `Super` shadows identically named item + const CONST: i32; + //~^ ERROR trait item `CONST` from `Super` shadows identically named item + type Assoc; + //~^ ERROR trait item `Assoc` from `Super` shadows identically named item } trait Sub: Super { fn method(); //~^ ERROR trait item `method` from `Sub` shadows identically named item + const CONST: i32; + //~^ ERROR trait item `CONST` from `Sub` shadows identically named item + type Assoc; + //~^ ERROR trait item `Assoc` from `Sub` shadows identically named item } fn main() {} diff --git a/tests/ui/supertrait-shadowing/definition-site.stderr b/tests/ui/supertrait-shadowing/definition-site.stderr index 1e35a753a9ebc..faf01cc711c6a 100644 --- a/tests/ui/supertrait-shadowing/definition-site.stderr +++ b/tests/ui/supertrait-shadowing/definition-site.stderr @@ -1,5 +1,5 @@ error: trait item `method` from `Super` shadows identically named item from supertrait - --> $DIR/definition-site.rs:9:5 + --> $DIR/definition-site.rs:11:5 | LL | fn method(); | ^^^^^^^^^^^^ @@ -15,8 +15,32 @@ note: the lint level is defined here LL | #![deny(shadowing_supertrait_items)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: trait item `CONST` from `Super` shadows identically named item from supertrait + --> $DIR/definition-site.rs:13:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + | +note: item from `SuperSuper` is shadowed by a subtrait item + --> $DIR/definition-site.rs:6:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + +error: trait item `Assoc` from `Super` shadows identically named item from supertrait + --> $DIR/definition-site.rs:15:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + | +note: item from `SuperSuper` is shadowed by a subtrait item + --> $DIR/definition-site.rs:7:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + error: trait item `method` from `Sub` shadows identically named item from supertrait - --> $DIR/definition-site.rs:14:5 + --> $DIR/definition-site.rs:20:5 | LL | fn method(); | ^^^^^^^^^^^^ @@ -30,5 +54,35 @@ LL | fn method(); LL | fn method(); | ^^^^^^^^^^^^ -error: aborting due to 2 previous errors +error: trait item `CONST` from `Sub` shadows identically named item from supertrait + --> $DIR/definition-site.rs:22:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `Super` and `SuperSuper` + --> $DIR/definition-site.rs:6:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +... +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ + +error: trait item `Assoc` from `Sub` shadows identically named item from supertrait + --> $DIR/definition-site.rs:24:5 + | +LL | type Assoc; + | ^^^^^^^^^^ + | +note: items from several supertraits are shadowed: `Super` and `SuperSuper` + --> $DIR/definition-site.rs:7:5 + | +LL | type Assoc; + | ^^^^^^^^^^ +... +LL | type Assoc; + | ^^^^^^^^^^ + +error: aborting due to 6 previous errors diff --git a/tests/ui/supertrait-shadowing/no-common-ancestor-2.rs b/tests/ui/supertrait-shadowing/no-common-ancestor-2.rs index b49476c7a4f68..957aabf5a51f8 100644 --- a/tests/ui/supertrait-shadowing/no-common-ancestor-2.rs +++ b/tests/ui/supertrait-shadowing/no-common-ancestor-2.rs @@ -1,25 +1,43 @@ #![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] + +use std::mem::size_of; trait A { fn hello(&self) -> &'static str { "A" } + type Assoc; + const CONST: i32; +} +impl A for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl A for T {} trait B { fn hello(&self) -> &'static str { "B" } + type Assoc; + const CONST: i32; +} +impl B for T { + type Assoc = i16; + const CONST: i32 = 2; } -impl B for T {} trait C: A + B { fn hello(&self) -> &'static str { "C" } + type Assoc; + type const CONST: i32; +} +impl C for T { + type Assoc = i32; + type const CONST: i32 = 3; } -impl C for T {} // Since `D` is not a subtrait of `C`, // we have no obvious lower bound. @@ -28,10 +46,23 @@ trait D: B { fn hello(&self) -> &'static str { "D" } + type Assoc; + type const CONST: i32; +} +impl D for T { + type Assoc = i64; + type const CONST: i32 = 4; } -impl D for T {} fn main() { ().hello(); //~^ ERROR multiple applicable items in scope + check::<()>(); +} + +fn check() { + let _ = size_of::(); + //~^ ERROR ambiguous associated type `Assoc` in bounds of `T` + let _ = T::CONST; + //~^ ERROR multiple applicable items in scope } diff --git a/tests/ui/supertrait-shadowing/no-common-ancestor-2.stderr b/tests/ui/supertrait-shadowing/no-common-ancestor-2.stderr index 745f61d00d660..987adcfbbe959 100644 --- a/tests/ui/supertrait-shadowing/no-common-ancestor-2.stderr +++ b/tests/ui/supertrait-shadowing/no-common-ancestor-2.stderr @@ -1,26 +1,26 @@ error[E0034]: multiple applicable items in scope - --> $DIR/no-common-ancestor-2.rs:35:8 + --> $DIR/no-common-ancestor-2.rs:58:8 | LL | ().hello(); | ^^^^^ multiple `hello` found | note: candidate #1 is defined in an impl of the trait `A` for the type `T` - --> $DIR/no-common-ancestor-2.rs:4:5 + --> $DIR/no-common-ancestor-2.rs:7:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: candidate #2 is defined in an impl of the trait `B` for the type `T` - --> $DIR/no-common-ancestor-2.rs:11:5 + --> $DIR/no-common-ancestor-2.rs:19:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: candidate #3 is defined in an impl of the trait `C` for the type `T` - --> $DIR/no-common-ancestor-2.rs:18:5 + --> $DIR/no-common-ancestor-2.rs:31:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: candidate #4 is defined in an impl of the trait `D` for the type `T` - --> $DIR/no-common-ancestor-2.rs:28:5 + --> $DIR/no-common-ancestor-2.rs:46:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -45,6 +45,87 @@ LL - ().hello(); LL + D::hello(&()); | -error: aborting due to 1 previous error +error[E0221]: ambiguous associated type `Assoc` in bounds of `T` + --> $DIR/no-common-ancestor-2.rs:64:23 + | +LL | type Assoc; + | ---------- ambiguous `Assoc` from `A` +... +LL | type Assoc; + | ---------- ambiguous `Assoc` from `B` +... +LL | type Assoc; + | ---------- ambiguous `Assoc` from `C` +... +LL | type Assoc; + | ---------- ambiguous `Assoc` from `D` +... +LL | let _ = size_of::(); + | ^^^^^^^^ ambiguous associated type `Assoc` + | +help: use fully-qualified syntax to disambiguate + | +LL - let _ = size_of::(); +LL + let _ = size_of::<::Assoc>(); + | +help: use fully-qualified syntax to disambiguate + | +LL - let _ = size_of::(); +LL + let _ = size_of::<::Assoc>(); + | +help: use fully-qualified syntax to disambiguate + | +LL - let _ = size_of::(); +LL + let _ = size_of::<::Assoc>(); + | +help: use fully-qualified syntax to disambiguate + | +LL - let _ = size_of::(); +LL + let _ = size_of::<::Assoc>(); + | + +error[E0034]: multiple applicable items in scope + --> $DIR/no-common-ancestor-2.rs:66:16 + | +LL | let _ = T::CONST; + | ^^^^^ multiple `CONST` found + | +note: candidate #1 is defined in the trait `A` + --> $DIR/no-common-ancestor-2.rs:11:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +note: candidate #2 is defined in the trait `B` + --> $DIR/no-common-ancestor-2.rs:23:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +note: candidate #3 is defined in the trait `C` + --> $DIR/no-common-ancestor-2.rs:35:5 + | +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ +note: candidate #4 is defined in the trait `D` + --> $DIR/no-common-ancestor-2.rs:50:5 + | +LL | type const CONST: i32; + | ^^^^^^^^^^^^^^^^^^^^^ +help: use fully-qualified syntax to disambiguate + | +LL - let _ = T::CONST; +LL + let _ = ::CONST; + | +LL - let _ = T::CONST; +LL + let _ = ::CONST; + | +LL - let _ = T::CONST; +LL + let _ = ::CONST; + | +LL - let _ = T::CONST; +LL + let _ = ::CONST; + | + +error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0034`. +Some errors have detailed explanations: E0034, E0221. +For more information about an error, try `rustc --explain E0034`. diff --git a/tests/ui/supertrait-shadowing/no-common-ancestor.rs b/tests/ui/supertrait-shadowing/no-common-ancestor.rs index 03c822eb7375e..73209c015f8f5 100644 --- a/tests/ui/supertrait-shadowing/no-common-ancestor.rs +++ b/tests/ui/supertrait-shadowing/no-common-ancestor.rs @@ -1,20 +1,41 @@ #![feature(supertrait_item_shadowing)] +#![feature(min_generic_const_args)] + +use std::mem::size_of; trait A { fn hello(&self) -> &'static str { "A" } + type Assoc; + const CONST: i32; +} +impl A for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl A for T {} trait B { fn hello(&self) -> &'static str { "B" } + type Assoc; + const CONST: i32; +} +impl B for T { + type Assoc = i16; + const CONST: i32 = 2; } -impl B for T {} fn main() { ().hello(); //~^ ERROR multiple applicable items in scope + check::<()>(); +} + +fn check() { + let _ = size_of::(); + //~^ ERROR ambiguous associated type `Assoc` in bounds of `T` + let _ = T::CONST; + //~^ ERROR multiple applicable items in scope } diff --git a/tests/ui/supertrait-shadowing/no-common-ancestor.stderr b/tests/ui/supertrait-shadowing/no-common-ancestor.stderr index 29cf7ff1dbb11..78fd8f4670577 100644 --- a/tests/ui/supertrait-shadowing/no-common-ancestor.stderr +++ b/tests/ui/supertrait-shadowing/no-common-ancestor.stderr @@ -1,16 +1,16 @@ error[E0034]: multiple applicable items in scope - --> $DIR/no-common-ancestor.rs:18:8 + --> $DIR/no-common-ancestor.rs:31:8 | LL | ().hello(); | ^^^^^ multiple `hello` found | note: candidate #1 is defined in an impl of the trait `A` for the type `T` - --> $DIR/no-common-ancestor.rs:4:5 + --> $DIR/no-common-ancestor.rs:7:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: candidate #2 is defined in an impl of the trait `B` for the type `T` - --> $DIR/no-common-ancestor.rs:11:5 + --> $DIR/no-common-ancestor.rs:19:5 | LL | fn hello(&self) -> &'static str { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -25,6 +25,55 @@ LL - ().hello(); LL + B::hello(&()); | -error: aborting due to 1 previous error +error[E0221]: ambiguous associated type `Assoc` in bounds of `T` + --> $DIR/no-common-ancestor.rs:37:23 + | +LL | type Assoc; + | ---------- ambiguous `Assoc` from `A` +... +LL | type Assoc; + | ---------- ambiguous `Assoc` from `B` +... +LL | let _ = size_of::(); + | ^^^^^^^^ ambiguous associated type `Assoc` + | +help: use fully-qualified syntax to disambiguate + | +LL - let _ = size_of::(); +LL + let _ = size_of::<::Assoc>(); + | +help: use fully-qualified syntax to disambiguate + | +LL - let _ = size_of::(); +LL + let _ = size_of::<::Assoc>(); + | + +error[E0034]: multiple applicable items in scope + --> $DIR/no-common-ancestor.rs:39:16 + | +LL | let _ = T::CONST; + | ^^^^^ multiple `CONST` found + | +note: candidate #1 is defined in the trait `A` + --> $DIR/no-common-ancestor.rs:11:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +note: candidate #2 is defined in the trait `B` + --> $DIR/no-common-ancestor.rs:23:5 + | +LL | const CONST: i32; + | ^^^^^^^^^^^^^^^^ +help: use fully-qualified syntax to disambiguate + | +LL - let _ = T::CONST; +LL + let _ = ::CONST; + | +LL - let _ = T::CONST; +LL + let _ = ::CONST; + | + +error: aborting due to 3 previous errors -For more information about this error, try `rustc --explain E0034`. +Some errors have detailed explanations: E0034, E0221. +For more information about an error, try `rustc --explain E0034`. diff --git a/tests/ui/supertrait-shadowing/out-of-scope.rs b/tests/ui/supertrait-shadowing/out-of-scope.rs index 4112634399b78..7b2322843f1c3 100644 --- a/tests/ui/supertrait-shadowing/out-of-scope.rs +++ b/tests/ui/supertrait-shadowing/out-of-scope.rs @@ -1,23 +1,42 @@ //@ run-pass +#![feature(min_generic_const_args)] #![allow(dead_code)] +use std::mem::size_of; + mod out_of_scope { pub trait Subtrait: super::Supertrait { fn hello(&self) -> &'static str { "subtrait" } + type Assoc; + type const CONST: i32; + } + impl Subtrait for T { + type Assoc = i16; + type const CONST: i32 = 2; } - impl Subtrait for T {} } trait Supertrait { fn hello(&self) -> &'static str { "supertrait" } + type Assoc; + const CONST: i32; +} +impl Supertrait for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl Supertrait for T {} fn main() { assert_eq!(().hello(), "supertrait"); + check::<()>(); +} + +fn check() { + assert_eq!(size_of::(), 1); + assert_eq!(T::CONST, 1); } diff --git a/tests/ui/supertrait-shadowing/type-dependent.rs b/tests/ui/supertrait-shadowing/type-dependent.rs index 4a5af1d598812..75272d101dd29 100644 --- a/tests/ui/supertrait-shadowing/type-dependent.rs +++ b/tests/ui/supertrait-shadowing/type-dependent.rs @@ -1,28 +1,51 @@ //@ run-pass -// Makes sure we can shadow with type-dependent method syntax. +// Makes sure we can shadow with type-dependent associated item syntax. +#![feature(min_generic_const_args)] #![feature(supertrait_item_shadowing)] #![allow(dead_code)] +use std::mem::size_of; + trait A { fn hello() -> &'static str { "A" } + type Assoc; + const CONST: i32; +} +impl A for T { + type Assoc = i8; + const CONST: i32 = 1; } -impl A for T {} trait B: A { fn hello() -> &'static str { "B" } + type Assoc; + type const CONST: i32; +} +impl B for T { + type Assoc = i16; + type const CONST: i32 = 2; } -impl B for T {} fn foo() -> &'static str { T::hello() } +fn assoc() -> usize { + size_of::() +} + +fn konst() -> i32 { + T::CONST +} + fn main() { assert_eq!(foo::<()>(), "B"); + assert_eq!(assoc::<()>(), 2); + assert_eq!(konst::<()>(), 2); } From 6b9de05805cc1301e26ca9214fc2e653873f382c Mon Sep 17 00:00:00 2001 From: Emmanuel Ugwu Date: Fri, 26 Jun 2026 03:23:03 +0000 Subject: [PATCH 6/6] Attribute docs `deprecated` , `warn`, `allow`, `cfg`, `deny`, and `forbid` * Added documentation for allow, cfg, deny, forbid, deprecated and warn attribute Signed-off-by: Emmanuel Ugwu * address review feedback Signed-off-by: Emmanuel Ugwu * address review feedback Signed-off-by: Emmanuel Ugwu * address review feedback Signed-off-by: Emmanuel Ugwu * address feedback Signed-off-by: Emmanuel Ugwu * add the right deprecated link Signed-off-by: Emmanuel Ugwu * add the right deprecated link Signed-off-by: Emmanuel Ugwu * fix typo Signed-off-by: Emmanuel Ugwu * fix phrasing Signed-off-by: Emmanuel Ugwu * fix phrasing Signed-off-by: Emmanuel Ugwu * address feedback Signed-off-by: Emmanuel Ugwu * address feedback and add link to rustc book Signed-off-by: Emmanuel Ugwu * address feedback Signed-off-by: Emmanuel Ugwu --- library/std/src/attribute_docs.rs | 250 ++++++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) diff --git a/library/std/src/attribute_docs.rs b/library/std/src/attribute_docs.rs index 1d8dddb7ca54a..e8d36483f3139 100644 --- a/library/std/src/attribute_docs.rs +++ b/library/std/src/attribute_docs.rs @@ -85,3 +85,253 @@ /// [`unused_must_use`]: ../rustc/lints/listing/warn-by-default.html#unused-must-use /// [the `must_use` attribute]: ../reference/attributes/diagnostics.html#the-must_use-attribute mod must_use_attribute {} + +#[doc(attribute = "allow")] +// +/// The `allow` attribute suppresses lint diagnostics that would otherwise produce +/// warnings or errors. It can be used on any lint or lint group (except those +/// set to `forbid`). +/// +/// ```rust +/// #[allow(dead_code)] +/// fn unused_function() { +/// // ... +/// } +/// +/// fn main() { +/// // `unused_function` does not generate a compiler warning. +/// } +/// ``` +/// +/// Without `#[allow(dead_code)]`, the example above would emit: +/// +/// ```text +/// warning: function `unused_function` is never used +/// --> main.rs:1:4 +/// | +/// 1 | fn unused_function() { +/// | ^^^^^^^^^^^^^^^ +/// | +/// = note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default +/// +/// warning: 1 warning emitted +/// ``` +/// +/// Multiple lints can be set to `allow` at once with commas: +/// +/// ```rust +/// #[allow(unused_variables, unused_mut)] +/// fn main() { +/// let mut x: u32 = 42; +/// } +/// ``` +/// +/// This is mostly used to prevent lint warnings or errors while still under development. +/// +/// It cannot override a lint that has been set to `forbid`. +/// +/// It's also important to consider that overusing `allow` could make code harder to maintain +/// and possibly hide issues. To mitigate this issue, using the `expect` attribute is preferred. +/// +/// `allow` can be overridden by `warn`, `deny`, and `forbid`. +/// +/// The lint checks supported by rustc can be found via `rustc -W help`, +/// along with their default settings and are documented in [the `rustc` book]. +/// +/// [the `rustc` book]: ../rustc/lints/listing/index.html +/// +/// For more information, see the Reference on [the `allow` attribute]. +/// +/// [the `allow` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes +mod allow_attribute {} + +#[doc(attribute = "cfg")] +// +/// Used for conditional compilation. +/// +/// The `cfg` attribute allows compiling an item under specific conditions, otherwise it +/// will be ignored. +/// +/// ```rust +/// // Only compiles this function for Linux. +/// #[cfg(target_os = "linux")] +/// fn platform_specific() { +/// println!("Running on Linux"); +/// } +/// +/// // Only compiles this function if not for Linux. +/// #[cfg(not(target_os = "linux"))] +/// fn platform_specific() { +/// println!("Running on something else"); +/// } +/// ``` +/// +/// Depending on the platform you're targeting, only one of these two functions will be considered +/// during the compilation. +/// +/// Conditions can also be combined with `all(...)`, `any(...)`, and `not(...)`. +/// +/// * `all`: True if all given predicates are true. +/// * `any`: True if at least one of the given predicates is true. +/// * `not`: True if the predicate is false and false if the predicate is true. +/// +/// ```rust +/// #[cfg(all(unix, target_pointer_width = "64"))] +/// fn unix_64bit() { +/// } +/// ``` +/// +/// If you want to use this mechanism in an `if` condition in your code, you +/// can use the [`cfg!`] macro. To conditionally apply an attribute, +/// see [`cfg_attr`]. +/// +/// For more information, see the Reference on [the `cfg` attribute]. +/// +/// [`cfg_attr`]: ../reference/conditional-compilation.html#the-cfg_attr-attribute +/// [the `cfg` attribute]: ../reference/conditional-compilation.html#the-cfg-attribute +mod cfg_attribute {} + +#[doc(attribute = "deny")] +// +/// Emits an error, preventing the compilation from finishing, when a lint check has failed. +/// This is useful for enforcing rules or preventing certain patterns: +/// +/// ```rust,compile_fail +/// #[deny(unused)] +/// fn foo() { +/// let x = 42; // Emits an error because x is unused. +/// } +/// ``` +/// +/// `deny` can be overridden by `allow`, `warn`, and `forbid`: +/// +/// ```rust +/// #![deny(unused)] +/// +/// #[allow(unused)] // We override the `deny` for this function. +/// fn foo() { +/// let x = 42; // No lint emitted even though `x` is unused. +/// } +/// ``` +/// +/// Multiple lints can also be set to `deny` at once: +/// +/// ```rust,compile_fail +/// #![deny(unused_imports, unused_variables)] +/// use std::collections::*; +/// +/// fn main() { +/// let mut x = 10; +/// } +/// ``` +/// +/// The lint checks supported by rustc can be found via `rustc -W help`, +/// along with their default settings and are documented in [the `rustc` book]. +/// +/// [the `rustc` book]: ../rustc/lints/listing/index.html +/// +/// For more information, see the Reference on [the `deny` attribute]. +/// +/// [the `deny` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes +mod deny_attribute {} + +#[doc(attribute = "forbid")] +// +/// Emits an error, preventing the compilation from finishing, when a lint check has failed. +/// +/// A lint set to `forbid` cannot be overridden by `allow` or `warn`. +/// Attempting either will result in a compilation error. Writing `#[deny(...)]` on the same lint inside a +/// `forbid` scope is permitted, but has no effect; the lint remains at the `forbid` level. +/// +/// This is useful for enforcing strict policies that should not be relaxed +/// anywhere in the codebase. Example: +/// +/// ```rust +/// #![forbid(unsafe_code)] +/// +/// // This would cause a compilation error if uncommented: +/// // #[allow(unsafe_code)] // error: cannot override `forbid` +/// ``` +/// +/// Multiple lints can be set to `forbid` at once: +/// +/// ```rust +/// #![forbid(unsafe_code, unused)] +/// ``` +/// +/// The lint checks supported by rustc can be found via `rustc -W help`, +/// along with their default settings and are documented in [the `rustc` book]. +/// +/// [the `rustc` book]: ../rustc/lints/listing/index.html +/// +/// For more information, see the Reference on [the `forbid` attribute]. +/// +/// [the `forbid` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes +mod forbid_attribute {} + +#[doc(attribute = "deprecated")] +// +/// Emits a warning during compilation when an item with this attribute is used. +/// `since` and `note` are optional fields giving more detail about why the item is deprecated. +/// +/// * `since`: the version since when the item is deprecated. +/// * `note`: the reason why an item is deprecated. +/// +/// Example: +/// +/// ```rust +/// #[deprecated(since = "1.0.0", note = "Use bar instead")] +/// struct Foo; +/// struct Bar; +/// ``` +/// +/// `deprecated` attribute helps developers transition away from old code by providing warnings when +/// deprecated items are used. Note that during `Cargo` builds, warnings on dependencies get silenced +/// by default, so you may not see a deprecation warning unless you build that dependency directly. +/// +/// For more information, see the Reference on [the `deprecated` attribute]. +/// +/// [the `deprecated` attribute]: ../reference/attributes/diagnostics.html#the-deprecated-attribute +mod deprecated_attribute {} + +#[doc(attribute = "warn")] +// +/// Emits a warning during compilation when a lint check failed. +/// +/// Unlike `deny` or `forbid`, `warn` does not produce a hard error: the compilation continues, but +/// the compiler emits a warning message. `warn` can be overridden by `allow`, `deny`, and `forbid`. +/// +/// Example: +/// +/// ```rust,compile_fail +/// #![allow(unused)] +/// +/// #[warn(unused)] // We override the allowed `unused` lint. +/// fn foo() { +/// // This lint warns by default even without #[warn(unused)] being explicitly set +/// let x = 42; // warning: unused variable `x` +/// } +/// ``` +/// +/// +/// Many lints, including `unused`, are already set to `warn` by default so this attribute is +/// mainly useful for lints that are normally `allow` by default. +/// +/// Multiple lints can be set to `warn` at once: +/// +/// ```rust,compile_fail +/// #[warn(unused_mut, unused_variables)] +/// fn main() { +/// let mut x = 42; +/// } +/// ``` +/// +/// The lint checks supported by rustc can be found via `rustc -W help`, +/// along with their default settings and are documented in [the `rustc` book]. +/// +/// [the `rustc` book]: ../rustc/lints/listing/index.html +/// +/// For more information, see the Reference on [the `warn` attribute]. +/// +/// [the `warn` attribute]: ../reference/attributes/diagnostics.html#lint-check-attributes +mod warn_attribute {}