Skip to content

Commit 4c3310a

Browse files
committed
Auto merge of #150638 - matthiaskrgr:rollup-924nozj, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #150558 (Detect cases where `?` is applied on a type that could be coming from a different crate version than expected) - #150570 (Removed confusing diagnostics note for trait required for `?` operator use) - #150589 (Remove unneeded `forbid_generic` field from `Res::SelfTyAlias`) - #150635 (triagebot: Add a mention for `dec2flt`, `flt2dec`, and `fmt/num.rs`) - #150637 (Fix broken edit links in Unstable and rustc books) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 00f49d2 + 689e74d commit 4c3310a

18 files changed

Lines changed: 200 additions & 95 deletions

File tree

compiler/rustc_hir/src/def.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -563,29 +563,6 @@ pub enum Res<Id = hir::HirId> {
563563
/// to get the underlying type.
564564
alias_to: DefId,
565565

566-
/// Whether the `Self` type is disallowed from mentioning generics (i.e. when used in an
567-
/// anonymous constant).
568-
///
569-
/// HACK(min_const_generics): self types also have an optional requirement to **not**
570-
/// mention any generic parameters to allow the following with `min_const_generics`:
571-
/// ```
572-
/// # struct Foo;
573-
/// impl Foo { fn test() -> [u8; size_of::<Self>()] { todo!() } }
574-
///
575-
/// struct Bar([u8; baz::<Self>()]);
576-
/// const fn baz<T>() -> usize { 10 }
577-
/// ```
578-
/// We do however allow `Self` in repeat expression even if it is generic to not break code
579-
/// which already works on stable while causing the `const_evaluatable_unchecked` future
580-
/// compat lint:
581-
/// ```
582-
/// fn foo<T>() {
583-
/// let _bar = [1_u8; size_of::<*mut T>()];
584-
/// }
585-
/// ```
586-
// FIXME(generic_const_exprs): Remove this bodge once that feature is stable.
587-
forbid_generic: bool,
588-
589566
/// Is this within an `impl Foo for bar`?
590567
is_trait_impl: bool,
591568
},
@@ -910,8 +887,8 @@ impl<Id> Res<Id> {
910887
Res::PrimTy(id) => Res::PrimTy(id),
911888
Res::Local(id) => Res::Local(map(id)),
912889
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
913-
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
914-
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
890+
Res::SelfTyAlias { alias_to, is_trait_impl } => {
891+
Res::SelfTyAlias { alias_to, is_trait_impl }
915892
}
916893
Res::ToolMod => Res::ToolMod,
917894
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
@@ -926,8 +903,8 @@ impl<Id> Res<Id> {
926903
Res::PrimTy(id) => Res::PrimTy(id),
927904
Res::Local(id) => Res::Local(map(id)?),
928905
Res::SelfTyParam { trait_ } => Res::SelfTyParam { trait_ },
929-
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl } => {
930-
Res::SelfTyAlias { alias_to, forbid_generic, is_trait_impl }
906+
Res::SelfTyAlias { alias_to, is_trait_impl } => {
907+
Res::SelfTyAlias { alias_to, is_trait_impl }
931908
}
932909
Res::ToolMod => Res::ToolMod,
933910
Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2149,7 +2149,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
21492149
);
21502150
self.check_param_uses_if_mcg(tcx.types.self_param, span, false)
21512151
}
2152-
Res::SelfTyAlias { alias_to: def_id, forbid_generic: _, .. } => {
2152+
Res::SelfTyAlias { alias_to: def_id, .. } => {
21532153
// `Self` in impl (we know the concrete type).
21542154
assert_eq!(opt_self_ty, None);
21552155
// Try to evaluate any array length constants.

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,15 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
559559
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
560560
)
561561
},
562+
duplicate_crate_names: |tcx, c: CrateNum| {
563+
let name = tcx.crate_name(c);
564+
tcx.arena.alloc_from_iter(
565+
tcx.crates(())
566+
.into_iter()
567+
.filter(|k| tcx.crate_name(**k) == name && **k != c)
568+
.map(|c| *c),
569+
)
570+
},
562571
..providers.queries
563572
};
564573
provide_extern(&mut providers.extern_queries);

compiler/rustc_middle/src/query/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,7 @@ rustc_queries! {
23342334
eval_always
23352335
desc { "fetching all foreign CrateNum instances" }
23362336
}
2337+
23372338
// Crates that are loaded non-speculatively (not for diagnostics or doc links).
23382339
// FIXME: This is currently only used for collecting lang items, but should be used instead of
23392340
// `crates` in most other cases too.
@@ -2342,6 +2343,14 @@ rustc_queries! {
23422343
desc { "fetching `CrateNum`s for all crates loaded non-speculatively" }
23432344
}
23442345

2346+
/// All crates that share the same name as crate `c`.
2347+
///
2348+
/// This normally occurs when multiple versions of the same dependency are present in the
2349+
/// dependency tree.
2350+
query duplicate_crate_names(c: CrateNum) -> &'tcx [CrateNum] {
2351+
desc { "fetching `CrateNum`s with same name as `{c:?}`" }
2352+
}
2353+
23452354
/// A list of all traits in a crate, used by rustdoc and error reporting.
23462355
query traits(_: CrateNum) -> &'tcx [DefId] {
23472356
desc { "fetching all traits in a crate" }

compiler/rustc_resolve/src/ident.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12861286
&mut self,
12871287
rib_index: usize,
12881288
rib_ident: Ident,
1289-
mut res: Res,
1289+
res: Res,
12901290
finalize: Option<Span>,
12911291
original_rib_ident_def: Ident,
12921292
all_ribs: &[Rib<'ra>],
@@ -1439,44 +1439,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14391439
}
14401440

14411441
RibKind::ConstantItem(trivial, _) => {
1442-
if let ConstantHasGenerics::No(cause) = trivial {
1443-
// HACK(min_const_generics): If we encounter `Self` in an anonymous
1444-
// constant we can't easily tell if it's generic at this stage, so
1445-
// we instead remember this and then enforce the self type to be
1446-
// concrete later on.
1447-
if let Res::SelfTyAlias {
1448-
alias_to: def,
1449-
forbid_generic: _,
1450-
is_trait_impl,
1451-
} = res
1452-
{
1453-
res = Res::SelfTyAlias {
1454-
alias_to: def,
1455-
forbid_generic: true,
1456-
is_trait_impl,
1457-
}
1458-
} else {
1459-
if let Some(span) = finalize {
1460-
let error = match cause {
1461-
NoConstantGenericsReason::IsEnumDiscriminant => {
1462-
ResolutionError::ParamInEnumDiscriminant {
1463-
name: rib_ident.name,
1464-
param_kind: ParamKindInEnumDiscriminant::Type,
1465-
}
1442+
if let ConstantHasGenerics::No(cause) = trivial
1443+
&& !matches!(res, Res::SelfTyAlias { .. })
1444+
{
1445+
if let Some(span) = finalize {
1446+
let error = match cause {
1447+
NoConstantGenericsReason::IsEnumDiscriminant => {
1448+
ResolutionError::ParamInEnumDiscriminant {
1449+
name: rib_ident.name,
1450+
param_kind: ParamKindInEnumDiscriminant::Type,
14661451
}
1467-
NoConstantGenericsReason::NonTrivialConstArg => {
1468-
ResolutionError::ParamInNonTrivialAnonConst {
1469-
name: rib_ident.name,
1470-
param_kind:
1471-
ParamKindInNonTrivialAnonConst::Type,
1472-
}
1452+
}
1453+
NoConstantGenericsReason::NonTrivialConstArg => {
1454+
ResolutionError::ParamInNonTrivialAnonConst {
1455+
name: rib_ident.name,
1456+
param_kind: ParamKindInNonTrivialAnonConst::Type,
14731457
}
1474-
};
1475-
let _: ErrorGuaranteed = self.report_error(span, error);
1476-
}
1477-
1478-
return Res::Err;
1458+
}
1459+
};
1460+
let _: ErrorGuaranteed = self.report_error(span, error);
14791461
}
1462+
1463+
return Res::Err;
14801464
}
14811465

14821466
continue;

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,11 +2655,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
26552655
|this| {
26562656
let item_def_id = this.r.local_def_id(item.id).to_def_id();
26572657
this.with_self_rib(
2658-
Res::SelfTyAlias {
2659-
alias_to: item_def_id,
2660-
forbid_generic: false,
2661-
is_trait_impl: false,
2662-
},
2658+
Res::SelfTyAlias { alias_to: item_def_id, is_trait_impl: false },
26632659
|this| {
26642660
visit::walk_item(this, item);
26652661
},
@@ -3368,8 +3364,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
33683364
let item_def_id = item_def_id.to_def_id();
33693365
let res = Res::SelfTyAlias {
33703366
alias_to: item_def_id,
3371-
forbid_generic: false,
3372-
is_trait_impl: trait_id.is_some()
3367+
is_trait_impl: trait_id.is_some(),
33733368
};
33743369
this.with_self_rib(res, |this| {
33753370
if let Some(of_trait) = of_trait {

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use rustc_middle::ty::{
3333
TypeVisitableExt, Upcast,
3434
};
3535
use rustc_middle::{bug, span_bug};
36+
use rustc_span::def_id::CrateNum;
3637
use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
3738
use tracing::{debug, instrument};
3839

@@ -1287,10 +1288,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
12871288
self.tcx.def_span(def.did()),
12881289
format!("`{self_ty}` needs to implement `From<{ty}>`"),
12891290
);
1290-
err.span_note(
1291-
self.tcx.def_span(found.did()),
1292-
format!("alternatively, `{ty}` needs to implement `Into<{self_ty}>`"),
1293-
);
12941291
}
12951292
(ty::Adt(def, _), None) if def.did().is_local() => {
12961293
let trait_path = self.tcx.short_string(
@@ -2172,6 +2169,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21722169
self.suggest_function_pointers_impl(None, &exp_found, err);
21732170
}
21742171

2172+
if let ty::Adt(def, _) = trait_pred.self_ty().skip_binder().peel_refs().kind()
2173+
&& let crates = self.tcx.duplicate_crate_names(def.did().krate)
2174+
&& !crates.is_empty()
2175+
{
2176+
self.note_two_crate_versions(def.did().krate, MultiSpan::new(), err);
2177+
err.help("you can use `cargo tree` to explore your dependency tree");
2178+
}
21752179
true
21762180
})
21772181
}) {
@@ -2282,6 +2286,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
22822286
}
22832287
));
22842288
}
2289+
2290+
if let ty::Adt(def, _) = trait_pred.self_ty().skip_binder().peel_refs().kind()
2291+
&& let crates = self.tcx.duplicate_crate_names(def.did().krate)
2292+
&& !crates.is_empty()
2293+
{
2294+
self.note_two_crate_versions(def.did().krate, MultiSpan::new(), err);
2295+
err.help("you can use `cargo tree` to explore your dependency tree");
2296+
}
22852297
true
22862298
};
22872299

@@ -2445,11 +2457,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
24452457

24462458
pub fn note_two_crate_versions(
24472459
&self,
2448-
did: DefId,
2460+
krate: CrateNum,
24492461
sp: impl Into<MultiSpan>,
24502462
err: &mut Diag<'_>,
24512463
) {
2452-
let crate_name = self.tcx.crate_name(did.krate);
2464+
let crate_name = self.tcx.crate_name(krate);
24532465
let crate_msg = format!(
24542466
"there are multiple different versions of crate `{crate_name}` in the dependency graph"
24552467
);
@@ -2514,7 +2526,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
25142526

25152527
for (similar_item, _) in similar_items {
25162528
err.span_help(self.tcx.def_span(similar_item), "item with same name found");
2517-
self.note_two_crate_versions(similar_item, MultiSpan::new(), err);
2529+
self.note_two_crate_versions(similar_item.krate, MultiSpan::new(), err);
25182530
}
25192531
}
25202532

compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
442442
);
443443
}
444444
}
445-
self.note_two_crate_versions(expected_did, span, err);
445+
self.note_two_crate_versions(expected_did.krate, span, err);
446446
err.help("you can use `cargo tree` to explore your dependency tree");
447447
}
448448
suggested

src/doc/rustc/book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title = "The rustc book"
33

44
[output.html]
55
git-repository-url = "https://github.com/rust-lang/rust/tree/HEAD/src/doc/rustc"
6-
edit-url-template = "https://github.com/rust-lang/rust/edit/HEAD/src/doc/rustc/{path}"
6+
edit-url-template = "https://github.com/rust-lang/rust/edit/main/src/doc/rustc/{path}"
77

88
[output.html.search]
99
use-boolean-and = true

src/doc/unstable-book/book.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ title = "The Rust Unstable Book"
33

44
[output.html]
55
git-repository-url = "https://github.com/rust-lang/rust/tree/HEAD/src/doc/unstable-book"
6-
edit-url-template = "https://github.com/rust-lang/rust/edit/HEAD/src/doc/unstable-book/{path}"
6+
edit-url-template = "https://github.com/rust-lang/rust/edit/main/src/doc/unstable-book/{path}"

0 commit comments

Comments
 (0)