Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,29 +563,6 @@ pub enum Res<Id = hir::HirId> {
/// to get the underlying type.
alias_to: DefId,

/// Whether the `Self` type is disallowed from mentioning generics (i.e. when used in an
/// anonymous constant).
///
/// HACK(min_const_generics): self types also have an optional requirement to **not**
/// mention any generic parameters to allow the following with `min_const_generics`:
/// ```
/// # struct Foo;
/// impl Foo { fn test() -> [u8; size_of::<Self>()] { todo!() } }
///
/// struct Bar([u8; baz::<Self>()]);
/// const fn baz<T>() -> usize { 10 }
/// ```
/// We do however allow `Self` in repeat expression even if it is generic to not break code
/// which already works on stable while causing the `const_evaluatable_unchecked` future
/// compat lint:
/// ```
/// fn foo<T>() {
/// let _bar = [1_u8; size_of::<*mut T>()];
/// }
/// ```
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
forbid_generic: bool,

/// Is this within an `impl Foo for bar`?
is_trait_impl: bool,
},
Expand Down Expand Up @@ -910,8 +887,8 @@ impl<Id> Res<Id> {
Res::PrimTy(id) => Res::PrimTy(id),
Res::Local(id) => Res::Local(map(id)),
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
Res::SelfTyAlias { alias_to, is_trait_impl } => {
Res::SelfTyAlias { alias_to, is_trait_impl }
}
Res::ToolMod => Res::ToolMod,
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
Expand All @@ -926,8 +903,8 @@ impl<Id> Res<Id> {
Res::PrimTy(id) => Res::PrimTy(id),
Res::Local(id) => Res::Local(map(id)?),
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
Res::SelfTyAlias { alias_to, is_trait_impl } => {
Res::SelfTyAlias { alias_to, is_trait_impl }
}
Res::ToolMod => Res::ToolMod,
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
self.check_param_uses_if_mcg(tcx.types.self_param, span, false)
}
Res::SelfTyAlias { alias_to: def_id, forbid_generic: _, .. } => {
Res::SelfTyAlias { alias_to: def_id, .. } => {
// `Self` in impl (we know the concrete type).
assert_eq!(opt_self_ty, None);
// Try to evaluate any array length constants.
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,15 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
)
},
duplicate_crate_names: |tcx, c: CrateNum| {
let name = tcx.crate_name(c);
tcx.arena.alloc_from_iter(
tcx.crates(())
.into_iter()
.filter(|k| tcx.crate_name(**k) == name && **k != c)
.map(|c| *c),
)
},
..providers.queries
};
provide_extern(&mut providers.extern_queries);
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2334,6 +2334,7 @@ rustc_queries! {
eval_always
desc { "fetching all foreign CrateNum instances" }
}

// Crates that are loaded non-speculatively (not for diagnostics or doc links).
// FIXME: This is currently only used for collecting lang items, but should be used instead of
// `crates` in most other cases too.
Expand All @@ -2342,6 +2343,14 @@ rustc_queries! {
desc { "fetching `CrateNum`s for all crates loaded non-speculatively" }
}

/// All crates that share the same name as crate `c`.
///
/// This normally occurs when multiple versions of the same dependency are present in the
/// dependency tree.
query duplicate_crate_names(c: CrateNum) -> &'tcx [CrateNum] {
desc { "fetching `CrateNum`s with same name as `{c:?}`" }
}

/// A list of all traits in a crate, used by rustdoc and error reporting.
query traits(_: CrateNum) -> &'tcx [DefId] {
desc { "fetching all traits in a crate" }
Expand Down
56 changes: 20 additions & 36 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&mut self,
rib_index: usize,
rib_ident: Ident,
mut res: Res,
res: Res,
finalize: Option<Span>,
original_rib_ident_def: Ident,
all_ribs: &[Rib<'ra>],
Expand Down Expand Up @@ -1439,44 +1439,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

RibKind::ConstantItem(trivial, _) => {
if let ConstantHasGenerics::No(cause) = trivial {
// HACK(min_const_generics): If we encounter `Self` in an anonymous
// constant we can't easily tell if it's generic at this stage, so
// we instead remember this and then enforce the self type to be
// concrete later on.
if let Res::SelfTyAlias {
alias_to: def,
forbid_generic: _,
is_trait_impl,
} = res
{
res = Res::SelfTyAlias {
alias_to: def,
forbid_generic: true,
is_trait_impl,
}
} else {
if let Some(span) = finalize {
let error = match cause {
NoConstantGenericsReason::IsEnumDiscriminant => {
ResolutionError::ParamInEnumDiscriminant {
name: rib_ident.name,
param_kind: ParamKindInEnumDiscriminant::Type,
}
if let ConstantHasGenerics::No(cause) = trivial
&& !matches!(res, Res::SelfTyAlias { .. })
{
if let Some(span) = finalize {
let error = match cause {
NoConstantGenericsReason::IsEnumDiscriminant => {
ResolutionError::ParamInEnumDiscriminant {
name: rib_ident.name,
param_kind: ParamKindInEnumDiscriminant::Type,
}
NoConstantGenericsReason::NonTrivialConstArg => {
ResolutionError::ParamInNonTrivialAnonConst {
name: rib_ident.name,
param_kind:
ParamKindInNonTrivialAnonConst::Type,
}
}
NoConstantGenericsReason::NonTrivialConstArg => {
ResolutionError::ParamInNonTrivialAnonConst {
name: rib_ident.name,
param_kind: ParamKindInNonTrivialAnonConst::Type,
}
};
let _: ErrorGuaranteed = self.report_error(span, error);
}

return Res::Err;
}
};
let _: ErrorGuaranteed = self.report_error(span, error);
}

return Res::Err;
}

continue;
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2655,11 +2655,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
|this| {
let item_def_id = this.r.local_def_id(item.id).to_def_id();
this.with_self_rib(
Res::SelfTyAlias {
alias_to: item_def_id,
forbid_generic: false,
is_trait_impl: false,
},
Res::SelfTyAlias { alias_to: item_def_id, is_trait_impl: false },
|this| {
visit::walk_item(this, item);
},
Expand Down Expand Up @@ -3368,8 +3364,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
let item_def_id = item_def_id.to_def_id();
let res = Res::SelfTyAlias {
alias_to: item_def_id,
forbid_generic: false,
is_trait_impl: trait_id.is_some()
is_trait_impl: trait_id.is_some(),
};
this.with_self_rib(res, |this| {
if let Some(of_trait) = of_trait {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use rustc_middle::ty::{
TypeVisitableExt, Upcast,
};
use rustc_middle::{bug, span_bug};
use rustc_span::def_id::CrateNum;
use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
use tracing::{debug, instrument};

Expand Down Expand Up @@ -1287,10 +1288,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
self.tcx.def_span(def.did()),
format!("`{self_ty}` needs to implement `From<{ty}>`"),
);
err.span_note(
self.tcx.def_span(found.did()),
format!("alternatively, `{ty}` needs to implement `Into<{self_ty}>`"),
);
}
(ty::Adt(def, _), None) if def.did().is_local() => {
let trait_path = self.tcx.short_string(
Expand Down Expand Up @@ -2172,6 +2169,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
self.suggest_function_pointers_impl(None, &exp_found, err);
}

if let ty::Adt(def, _) = trait_pred.self_ty().skip_binder().peel_refs().kind()
&& let crates = self.tcx.duplicate_crate_names(def.did().krate)
&& !crates.is_empty()
{
self.note_two_crate_versions(def.did().krate, MultiSpan::new(), err);
err.help("you can use `cargo tree` to explore your dependency tree");
}
true
})
}) {
Expand Down Expand Up @@ -2282,6 +2286,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
));
}

if let ty::Adt(def, _) = trait_pred.self_ty().skip_binder().peel_refs().kind()
&& let crates = self.tcx.duplicate_crate_names(def.did().krate)
&& !crates.is_empty()
{
self.note_two_crate_versions(def.did().krate, MultiSpan::new(), err);
err.help("you can use `cargo tree` to explore your dependency tree");
}
true
};

Expand Down Expand Up @@ -2445,11 +2457,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {

pub fn note_two_crate_versions(
&self,
did: DefId,
krate: CrateNum,
sp: impl Into<MultiSpan>,
err: &mut Diag<'_>,
) {
let crate_name = self.tcx.crate_name(did.krate);
let crate_name = self.tcx.crate_name(krate);
let crate_msg = format!(
"there are multiple different versions of crate `{crate_name}` in the dependency graph"
);
Expand Down Expand Up @@ -2514,7 +2526,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {

for (similar_item, _) in similar_items {
err.span_help(self.tcx.def_span(similar_item), "item with same name found");
self.note_two_crate_versions(similar_item, MultiSpan::new(), err);
self.note_two_crate_versions(similar_item.krate, MultiSpan::new(), err);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
);
}
}
self.note_two_crate_versions(expected_did, span, err);
self.note_two_crate_versions(expected_did.krate, span, err);
err.help("you can use `cargo tree` to explore your dependency tree");
}
suggested
Expand Down
16 changes: 15 additions & 1 deletion tests/run-make/crate-loading/dep-2-reexport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
#![crate_type = "rlib"]

extern crate dependency;
pub use dependency::{Trait2, Type, do_something_trait, do_something_type};
pub use dependency::{Error, OtherError, Trait2, Type, do_something_trait, do_something_type};
pub struct OtherType;
impl dependency::Trait for OtherType {
fn foo(&self) {}
fn bar() {}
}
#[derive(Debug)]
pub struct Error2;

impl From<Error> for Error2 {
fn from(_: Error) -> Error2 {
Error2
}
}

impl From<OtherError> for Error2 {
fn from(_: OtherError) -> Error2 {
Error2
}
}
24 changes: 24 additions & 0 deletions tests/run-make/crate-loading/dependency-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,27 @@ impl Trait for Type {
pub fn do_something<X: Trait>(_: X) {}
pub fn do_something_type(_: Type) {}
pub fn do_something_trait(_: Box<dyn Trait2>) {}

#[derive(Debug)]
pub struct Error;

impl From<()> for Error {
fn from(t: ()) -> Error {
Error
}
}

#[derive(Debug)]
pub struct OtherError;

impl From<()> for OtherError {
fn from(t: ()) -> OtherError {
OtherError
}
}

impl From<i32> for OtherError {
fn from(t: i32) -> OtherError {
OtherError
}
}
24 changes: 24 additions & 0 deletions tests/run-make/crate-loading/dependency-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,27 @@ impl Trait for Type {
pub fn do_something<X: Trait>(_: X) {}
pub fn do_something_type(_: Type) {}
pub fn do_something_trait(_: Box<dyn Trait2>) {}

#[derive(Debug)]
pub struct Error;

impl From<()> for Error {
fn from(t: ()) -> Error {
Error
}
}

#[derive(Debug)]
pub struct OtherError;

impl From<()> for OtherError {
fn from(_: ()) -> OtherError {
OtherError
}
}

impl From<i32> for OtherError {
fn from(_: i32) -> OtherError {
OtherError
}
}
13 changes: 10 additions & 3 deletions tests/run-make/crate-loading/multiple-dep-versions.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
extern crate dep_2_reexport;
extern crate dependency;
use dep_2_reexport::{OtherType, Trait2, Type};
use dependency::{Trait, do_something, do_something_trait, do_something_type};
use dep_2_reexport::{Error2, OtherType, Trait2, Type};
use dependency::{Error, OtherError, Trait, do_something, do_something_trait, do_something_type};

fn main() {
fn main() -> Result<(), Error> {
do_something(Type);
Type.foo();
Type::bar();
do_something(OtherType);
do_something_type(Type);
do_something_trait(Box::new(Type) as Box<dyn Trait2>);
Err(Error2)?;
Ok(())
}

fn foo() -> Result<(), OtherError> {
Err(Error2)?;
Ok(())
}
Loading
Loading