Skip to content

Commit 912981a

Browse files
committed
Auto merge of rust-lang#141396 - matthiaskrgr:rollup-feg050g, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#135562 (Add ignore value suggestion in closure body) - rust-lang#139635 (Finalize repeat expr inference behaviour with inferred repeat counts) - rust-lang#139668 (Handle regions equivalent to 'static in non_local_bounds) - rust-lang#140218 (HIR ty lowering: Clean up & refactor the lowering of type-relative paths) - rust-lang#140435 (use uX::from instead of _ as uX in non - const contexts) - rust-lang#141130 (rustc_on_unimplemented cleanups) - rust-lang#141286 (Querify `coroutine_hidden_types`) Failed merges: - rust-lang#140247 (Don't build `ParamEnv` and do trait solving in `ItemCtxt`s when lowering IATs) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2eef478 + 8c25082 commit 912981a

File tree

88 files changed

+1685
-988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1685
-988
lines changed

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ impl UniversalRegionRelations<'_> {
131131
assert!(self.universal_regions.is_universal_region(fr0));
132132

133133
let mut external_parents = vec![];
134-
let mut queue = vec![fr0];
134+
135+
let mut queue = vec![relation.minimal_scc_representative(fr0)];
135136

136137
// Keep expanding `fr` into its parents until we reach
137138
// non-local regions.

compiler/rustc_data_structures/src/transitive_relation.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,20 @@ impl<T: Eq + Hash + Copy> TransitiveRelation<T> {
354354
.collect()
355355
}
356356

357+
/// Given an element A, elements B with the lowest index such that `A R B`
358+
/// and `B R A`, or `A` if no such element exists.
359+
pub fn minimal_scc_representative(&self, a: T) -> T {
360+
match self.index(a) {
361+
Some(a_i) => self.with_closure(|closure| {
362+
closure
363+
.iter(a_i.0)
364+
.find(|i| closure.contains(*i, a_i.0))
365+
.map_or(a, |i| self.elements[i])
366+
}),
367+
None => a,
368+
}
369+
}
370+
357371
fn with_closure<OP, R>(&self, op: OP) -> R
358372
where
359373
OP: FnOnce(&BitMatrix<usize, usize>) -> R,

compiler/rustc_data_structures/src/transitive_relation/tests.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,44 @@ fn parent() {
376376
let p = relation.postdom_parent(3);
377377
assert_eq!(p, Some(0));
378378
}
379+
380+
#[test]
381+
fn minimal_scc_representative_1() {
382+
// +---------+
383+
// v |
384+
// a -> c -> d -> e
385+
// ^ ^
386+
// | |
387+
// b ---+
388+
389+
// "digraph { a -> c -> d -> e -> c; b -> d; b -> e; }",
390+
let mut relation = TransitiveRelationBuilder::default();
391+
relation.add("a", "c");
392+
relation.add("c", "d");
393+
relation.add("d", "e");
394+
relation.add("e", "c");
395+
relation.add("b", "d");
396+
relation.add("b", "e");
397+
let relation = relation.freeze();
398+
399+
assert_eq!(relation.minimal_scc_representative("a"), "a");
400+
assert_eq!(relation.minimal_scc_representative("b"), "b");
401+
assert_eq!(relation.minimal_scc_representative("c"), "c");
402+
assert_eq!(relation.minimal_scc_representative("d"), "c");
403+
assert_eq!(relation.minimal_scc_representative("e"), "c");
404+
}
405+
406+
#[test]
407+
fn minimal_scc_representative_2() {
408+
// "digraph { a -> b; a -> a; b -> a; c -> c}",
409+
let mut relation = TransitiveRelationBuilder::default();
410+
relation.add("a", "b");
411+
relation.add("b", "a");
412+
relation.add("a", "a");
413+
relation.add("c", "c");
414+
let relation = relation.freeze();
415+
416+
assert_eq!(relation.minimal_scc_representative("a"), "a");
417+
assert_eq!(relation.minimal_scc_representative("b"), "a");
418+
assert_eq!(relation.minimal_scc_representative("c"), "c");
419+
}

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ hir_analysis_assoc_kind_mismatch = expected {$expected}, found {$got}
3737
3838
hir_analysis_assoc_kind_mismatch_wrap_in_braces_sugg = consider adding braces here
3939
40-
hir_analysis_associated_type_trait_uninferred_generic_params = cannot use the associated {$what} of a trait with uninferred generic parameters
40+
hir_analysis_associated_type_trait_uninferred_generic_params = cannot use the {$what} of a trait with uninferred generic parameters
4141
.suggestion = use a fully qualified path with inferred lifetimes
4242
4343
hir_analysis_associated_type_trait_uninferred_generic_params_multipart_suggestion = use a fully qualified path with explicit lifetimes

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ use rustc_trait_selection::traits::ObligationCtxt;
4444
use tracing::{debug, instrument};
4545

4646
use crate::errors;
47-
use crate::hir_ty_lowering::errors::assoc_tag_str;
4847
use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer, RegionInferReason};
4948

5049
pub(crate) mod dump;
@@ -444,13 +443,12 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
444443
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_ident))
445444
}
446445

447-
fn lower_assoc_shared(
446+
fn lower_assoc_item_path(
448447
&self,
449448
span: Span,
450449
item_def_id: DefId,
451450
item_segment: &rustc_hir::PathSegment<'tcx>,
452451
poly_trait_ref: ty::PolyTraitRef<'tcx>,
453-
assoc_tag: ty::AssocTag,
454452
) -> Result<(DefId, ty::GenericArgsRef<'tcx>), ErrorGuaranteed> {
455453
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
456454
let item_args = self.lowerer().lower_generic_args_of_assoc_item(
@@ -525,7 +523,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
525523
inferred_sugg,
526524
bound,
527525
mpart_sugg,
528-
what: assoc_tag_str(assoc_tag),
526+
what: self.tcx.def_descr(item_def_id),
529527
}))
530528
}
531529
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 39 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
44
use rustc_errors::codes::*;
55
use rustc_errors::struct_span_code_err;
66
use rustc_hir as hir;
7+
use rustc_hir::AmbigArg;
78
use rustc_hir::def::{DefKind, Res};
89
use rustc_hir::def_id::{DefId, LocalDefId};
9-
use rustc_hir::{AmbigArg, HirId};
1010
use rustc_middle::bug;
1111
use rustc_middle::ty::{
1212
self as ty, IsSuggestable, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable,
@@ -309,7 +309,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
309309
false => "`?Sized`",
310310
};
311311
// There was a `?Trait` bound, but it was neither `?Sized` nor `experimental_default_bounds`.
312-
tcx.dcx().span_err(
312+
self.dcx().span_err(
313313
unbound.span,
314314
format!(
315315
"relaxing a default bound only does something for {}; \
@@ -675,7 +675,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
675675

676676
// Good error for `where Trait::method(..): Send`.
677677
let Some(self_ty) = opt_self_ty else {
678-
let guar = self.error_missing_qpath_self_ty(
678+
let guar = self.report_missing_self_ty_for_resolved_path(
679679
trait_def_id,
680680
hir_ty.span,
681681
item_segment,
@@ -713,120 +713,51 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
713713
Err(guar) => Ty::new_error(tcx, guar),
714714
}
715715
}
716-
hir::QPath::TypeRelative(qself, item_segment)
717-
if item_segment.args.is_some_and(|args| {
716+
hir::QPath::TypeRelative(hir_self_ty, segment)
717+
if segment.args.is_some_and(|args| {
718718
matches!(args.parenthesized, hir::GenericArgsParentheses::ReturnTypeNotation)
719719
}) =>
720720
{
721-
match self
722-
.resolve_type_relative_return_type_notation(
723-
qself,
724-
item_segment,
725-
hir_ty.hir_id,
726-
hir_ty.span,
727-
)
728-
.and_then(|(candidate, item_def_id)| {
729-
self.lower_return_type_notation_ty(candidate, item_def_id, hir_ty.span)
730-
}) {
731-
Ok(ty) => Ty::new_alias(tcx, ty::Projection, ty),
732-
Err(guar) => Ty::new_error(tcx, guar),
733-
}
734-
}
735-
_ => self.lower_ty(hir_ty),
736-
}
737-
}
738-
739-
/// Perform type-dependent lookup for a *method* for return type notation.
740-
/// This generally mirrors `<dyn HirTyLowerer>::lower_assoc_path`.
741-
fn resolve_type_relative_return_type_notation(
742-
&self,
743-
qself: &'tcx hir::Ty<'tcx>,
744-
item_segment: &'tcx hir::PathSegment<'tcx>,
745-
qpath_hir_id: HirId,
746-
span: Span,
747-
) -> Result<(ty::PolyTraitRef<'tcx>, DefId), ErrorGuaranteed> {
748-
let tcx = self.tcx();
749-
let qself_ty = self.lower_ty(qself);
750-
let assoc_ident = item_segment.ident;
751-
let qself_res = if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = &qself.kind {
752-
path.res
753-
} else {
754-
Res::Err
755-
};
756-
757-
let bound = match (qself_ty.kind(), qself_res) {
758-
(_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => {
759-
// `Self` in an impl of a trait -- we have a concrete self type and a
760-
// trait reference.
761-
let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) else {
762-
// A cycle error occurred, most likely.
763-
self.dcx().span_bug(span, "expected cycle error");
764-
};
765-
766-
self.probe_single_bound_for_assoc_item(
767-
|| {
768-
traits::supertraits(
769-
tcx,
770-
ty::Binder::dummy(trait_ref.instantiate_identity()),
771-
)
772-
},
773-
AssocItemQSelf::SelfTyAlias,
721+
let self_ty = self.lower_ty(hir_self_ty);
722+
let (item_def_id, bound) = match self.resolve_type_relative_path(
723+
self_ty,
724+
hir_self_ty,
774725
ty::AssocTag::Fn,
775-
assoc_ident,
776-
span,
726+
segment,
727+
hir_ty.hir_id,
728+
hir_ty.span,
777729
None,
778-
)?
779-
}
780-
(
781-
&ty::Param(_),
782-
Res::SelfTyParam { trait_: param_did } | Res::Def(DefKind::TyParam, param_did),
783-
) => self.probe_single_ty_param_bound_for_assoc_item(
784-
param_did.expect_local(),
785-
qself.span,
786-
ty::AssocTag::Fn,
787-
assoc_ident,
788-
span,
789-
)?,
790-
_ => {
791-
if let Err(reported) = qself_ty.error_reported() {
792-
return Err(reported);
793-
} else {
794-
// FIXME(return_type_notation): Provide some structured suggestion here.
795-
let err = struct_span_code_err!(
796-
self.dcx(),
797-
span,
798-
E0223,
799-
"ambiguous associated function"
730+
) {
731+
Ok(result) => result,
732+
Err(guar) => return Ty::new_error(tcx, guar),
733+
};
734+
735+
// Don't let `T::method` resolve to some `for<'a> <T as Tr<'a>>::method`,
736+
// which may happen via a higher-ranked where clause or supertrait.
737+
// This is the same restrictions as associated types; even though we could
738+
// support it, it just makes things a lot more difficult to support in
739+
// `resolve_bound_vars`, since we'd need to introduce those as elided
740+
// bound vars on the where clause too.
741+
if bound.has_bound_vars() {
742+
return Ty::new_error(
743+
tcx,
744+
self.dcx().emit_err(errors::AssociatedItemTraitUninferredGenericParams {
745+
span: hir_ty.span,
746+
inferred_sugg: Some(hir_ty.span.with_hi(segment.ident.span.lo())),
747+
bound: format!("{}::", tcx.anonymize_bound_vars(bound).skip_binder()),
748+
mpart_sugg: None,
749+
what: tcx.def_descr(item_def_id),
750+
}),
800751
);
801-
return Err(err.emit());
802752
}
803-
}
804-
};
805753

806-
// Don't let `T::method` resolve to some `for<'a> <T as Tr<'a>>::method`,
807-
// which may happen via a higher-ranked where clause or supertrait.
808-
// This is the same restrictions as associated types; even though we could
809-
// support it, it just makes things a lot more difficult to support in
810-
// `resolve_bound_vars`, since we'd need to introduce those as elided
811-
// bound vars on the where clause too.
812-
if bound.has_bound_vars() {
813-
return Err(self.tcx().dcx().emit_err(
814-
errors::AssociatedItemTraitUninferredGenericParams {
815-
span,
816-
inferred_sugg: Some(span.with_hi(item_segment.ident.span.lo())),
817-
bound: format!("{}::", tcx.anonymize_bound_vars(bound).skip_binder(),),
818-
mpart_sugg: None,
819-
what: "function",
820-
},
821-
));
754+
match self.lower_return_type_notation_ty(bound, item_def_id, hir_ty.span) {
755+
Ok(ty) => Ty::new_alias(tcx, ty::Projection, ty),
756+
Err(guar) => Ty::new_error(tcx, guar),
757+
}
758+
}
759+
_ => self.lower_ty(hir_ty),
822760
}
823-
824-
let trait_def_id = bound.def_id();
825-
let assoc_ty = self
826-
.probe_assoc_item(assoc_ident, ty::AssocTag::Fn, qpath_hir_id, span, trait_def_id)
827-
.expect("failed to find associated type");
828-
829-
Ok((bound, assoc_ty.def_id))
830761
}
831762

832763
/// Do the common parts of lowering an RTN type. This involves extending the

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn validate_cmse_abi<'tcx>(
3535
_ => tcx.hir_span(hir_id),
3636
};
3737
struct_span_code_err!(
38-
tcx.dcx(),
38+
dcx,
3939
span,
4040
E0781,
4141
"the `\"C-cmse-nonsecure-call\"` ABI is only allowed on function pointers"

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
7878

7979
// We don't support empty trait objects.
8080
if regular_traits.is_empty() && auto_traits.is_empty() {
81-
let guar = self.report_trait_object_with_no_traits_error(
82-
span,
83-
user_written_bounds.iter().copied(),
84-
);
81+
let guar =
82+
self.report_trait_object_with_no_traits(span, user_written_bounds.iter().copied());
8583
return Ty::new_error(tcx, guar);
8684
}
8785
// We don't support >1 principal
8886
if regular_traits.len() > 1 {
89-
let guar = self.report_trait_object_addition_traits_error(&regular_traits);
87+
let guar = self.report_trait_object_addition_traits(&regular_traits);
9088
return Ty::new_error(tcx, guar);
9189
}
9290
// Don't create a dyn trait if we have errors in the principal.
@@ -132,7 +130,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
132130
if references_self {
133131
// With trait alias and type alias combined, type resolver
134132
// may not be able to catch all illegal `Self` usages (issue 139082)
135-
let guar = tcx.dcx().emit_err(SelfInTypeAlias { span });
133+
let guar = self.dcx().emit_err(SelfInTypeAlias { span });
136134
b.term = replace_dummy_self_with_error(tcx, b.term, guar);
137135
}
138136
}
@@ -360,7 +358,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
360358
hir_bound.trait_ref.path.res == Res::Def(DefKind::Trait, trait_ref.def_id)
361359
&& hir_bound.span.contains(span)
362360
});
363-
self.complain_about_missing_type_params(
361+
self.report_missing_type_params(
364362
missing_type_params,
365363
trait_ref.def_id,
366364
span,

0 commit comments

Comments
 (0)