Skip to content

Commit bda32a4

Browse files
committed
Auto merge of #108301 - Dylan-DPC:rollup-70zpkt0, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #108000 (lint: don't suggest MaybeUninit::assume_init for uninhabited types) - #108105 (Explain the default panic hook better) - #108141 (Add rpitit queries) - #108272 (docs: wrong naming convention in struct keyword doc) - #108285 (remove unstable `pick_stable_methods_before_any_unstable` flag) - #108289 (Name placeholder in some region errors) - #108290 (Add a test for default trait method with RPITITs) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3fee48c + 60c0972 commit bda32a4

File tree

27 files changed

+429
-323
lines changed

27 files changed

+429
-323
lines changed

compiler/rustc_hir/src/definitions.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ pub enum DefPathData {
280280
AnonConst,
281281
/// An `impl Trait` type node.
282282
ImplTrait,
283+
/// `impl Trait` generated associated type node.
284+
ImplTraitAssocTy,
283285
}
284286

285287
impl Definitions {
@@ -403,7 +405,7 @@ impl DefPathData {
403405
TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => Some(name),
404406

405407
Impl | ForeignMod | CrateRoot | Use | GlobalAsm | ClosureExpr | Ctor | AnonConst
406-
| ImplTrait => None,
408+
| ImplTrait | ImplTraitAssocTy => None,
407409
}
408410
}
409411

@@ -422,7 +424,7 @@ impl DefPathData {
422424
ClosureExpr => DefPathDataName::Anon { namespace: sym::closure },
423425
Ctor => DefPathDataName::Anon { namespace: sym::constructor },
424426
AnonConst => DefPathDataName::Anon { namespace: sym::constant },
425-
ImplTrait => DefPathDataName::Anon { namespace: sym::opaque },
427+
ImplTrait | ImplTraitAssocTy => DefPathDataName::Anon { namespace: sym::opaque },
426428
}
427429
}
428430
}

compiler/rustc_hir_typeck/src/method/probe.rs

+2-54
Original file line numberDiff line numberDiff line change
@@ -1095,17 +1095,8 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
10951095
}
10961096

10971097
fn pick_core(&self) -> Option<PickResult<'tcx>> {
1098-
let pick = self.pick_all_method(Some(&mut vec![]));
1099-
1100-
// In this case unstable picking is done by `pick_method`.
1101-
if !self.tcx.sess.opts.unstable_opts.pick_stable_methods_before_any_unstable {
1102-
return pick;
1103-
}
1104-
1105-
if pick.is_none() {
1106-
return self.pick_all_method(None);
1107-
}
1108-
pick
1098+
// Pick stable methods only first, and consider unstable candidates if not found.
1099+
self.pick_all_method(Some(&mut vec![])).or_else(|| self.pick_all_method(None))
11091100
}
11101101

11111102
fn pick_all_method(
@@ -1244,54 +1235,11 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
12441235
})
12451236
}
12461237

1247-
fn pick_method_with_unstable(&self, self_ty: Ty<'tcx>) -> Option<PickResult<'tcx>> {
1248-
debug!("pick_method_with_unstable(self_ty={})", self.ty_to_string(self_ty));
1249-
1250-
let mut possibly_unsatisfied_predicates = Vec::new();
1251-
1252-
for (kind, candidates) in
1253-
&[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1254-
{
1255-
debug!("searching {} candidates", kind);
1256-
let res = self.consider_candidates(
1257-
self_ty,
1258-
candidates,
1259-
&mut possibly_unsatisfied_predicates,
1260-
Some(&mut vec![]),
1261-
);
1262-
if res.is_some() {
1263-
return res;
1264-
}
1265-
}
1266-
1267-
for (kind, candidates) in
1268-
&[("inherent", &self.inherent_candidates), ("extension", &self.extension_candidates)]
1269-
{
1270-
debug!("searching unstable {kind} candidates");
1271-
let res = self.consider_candidates(
1272-
self_ty,
1273-
candidates,
1274-
&mut possibly_unsatisfied_predicates,
1275-
None,
1276-
);
1277-
if res.is_some() {
1278-
return res;
1279-
}
1280-
}
1281-
1282-
self.unsatisfied_predicates.borrow_mut().extend(possibly_unsatisfied_predicates);
1283-
None
1284-
}
1285-
12861238
fn pick_method(
12871239
&self,
12881240
self_ty: Ty<'tcx>,
12891241
mut unstable_candidates: Option<&mut Vec<(Candidate<'tcx>, Symbol)>>,
12901242
) -> Option<PickResult<'tcx>> {
1291-
if !self.tcx.sess.opts.unstable_opts.pick_stable_methods_before_any_unstable {
1292-
return self.pick_method_with_unstable(self_ty);
1293-
}
1294-
12951243
debug!("pick_method(self_ty={})", self.ty_to_string(self_ty));
12961244

12971245
let mut possibly_unsatisfied_predicates = Vec::new();

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,16 @@ pub(super) fn note_and_explain_region<'tcx>(
129129
alt_span: Option<Span>,
130130
) {
131131
let (description, span) = match *region {
132-
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {
133-
msg_span_from_free_region(tcx, region, alt_span)
132+
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::RePlaceholder(_) | ty::ReStatic => {
133+
msg_span_from_named_region(tcx, region, alt_span)
134134
}
135135

136-
ty::RePlaceholder(_) => return,
137-
138136
ty::ReError(_) => return,
139137

140-
// FIXME(#13998) RePlaceholder should probably print like
141-
// ReFree rather than dumping Debug output on the user.
142-
//
143138
// We shouldn't really be having unification failures with ReVar
144139
// and ReLateBound though.
145140
ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
146-
(format!("lifetime {:?}", region), alt_span)
141+
(format!("lifetime `{region}`"), alt_span)
147142
}
148143
};
149144

@@ -157,12 +152,12 @@ fn explain_free_region<'tcx>(
157152
region: ty::Region<'tcx>,
158153
suffix: &str,
159154
) {
160-
let (description, span) = msg_span_from_free_region(tcx, region, None);
155+
let (description, span) = msg_span_from_named_region(tcx, region, None);
161156

162157
label_msg_span(err, prefix, description, span, suffix);
163158
}
164159

165-
fn msg_span_from_free_region<'tcx>(
160+
fn msg_span_from_named_region<'tcx>(
166161
tcx: TyCtxt<'tcx>,
167162
region: ty::Region<'tcx>,
168163
alt_span: Option<Span>,
@@ -173,6 +168,18 @@ fn msg_span_from_free_region<'tcx>(
173168
(msg, Some(span))
174169
}
175170
ty::ReStatic => ("the static lifetime".to_owned(), alt_span),
171+
ty::RePlaceholder(ty::PlaceholderRegion {
172+
name: ty::BoundRegionKind::BrNamed(def_id, name),
173+
..
174+
}) => (format!("the lifetime `{name}` as defined here"), Some(tcx.def_span(def_id))),
175+
ty::RePlaceholder(ty::PlaceholderRegion {
176+
name: ty::BoundRegionKind::BrAnon(_, Some(span)),
177+
..
178+
}) => (format!("the anonymous lifetime defined here"), Some(span)),
179+
ty::RePlaceholder(ty::PlaceholderRegion {
180+
name: ty::BoundRegionKind::BrAnon(_, None),
181+
..
182+
}) => (format!("an anonymous lifetime"), None),
176183
_ => bug!("{:?}", region),
177184
}
178185
}

compiler/rustc_interface/src/tests.rs

-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,6 @@ fn test_unstable_options_tracking_hash() {
776776
tracked!(packed_bundled_libs, true);
777777
tracked!(panic_abort_tests, true);
778778
tracked!(panic_in_drop, PanicStrategy::Abort);
779-
tracked!(pick_stable_methods_before_any_unstable, false);
780779
tracked!(plt, Some(true));
781780
tracked!(polonius, true);
782781
tracked!(precise_enum_drop_elaboration, false);

compiler/rustc_lint/src/builtin.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,13 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
26352635
cx.emit_spanned_lint(
26362636
INVALID_VALUE,
26372637
expr.span,
2638-
BuiltinUnpermittedTypeInit { msg, ty: conjured_ty, label: expr.span, sub },
2638+
BuiltinUnpermittedTypeInit {
2639+
msg,
2640+
ty: conjured_ty,
2641+
label: expr.span,
2642+
sub,
2643+
tcx: cx.tcx,
2644+
},
26392645
);
26402646
}
26412647
}

compiler/rustc_lint/src/lints.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use rustc_errors::{
88
};
99
use rustc_hir::def_id::DefId;
1010
use rustc_macros::{LintDiagnostic, Subdiagnostic};
11-
use rustc_middle::ty::{PolyExistentialTraitRef, Predicate, Ty, TyCtxt};
11+
use rustc_middle::ty::{
12+
inhabitedness::InhabitedPredicate, PolyExistentialTraitRef, Predicate, Ty, TyCtxt,
13+
};
1214
use rustc_session::parse::ParseSess;
1315
use rustc_span::{edition::Edition, sym, symbol::Ident, Span, Symbol};
1416

@@ -419,6 +421,7 @@ pub struct BuiltinUnpermittedTypeInit<'a> {
419421
pub ty: Ty<'a>,
420422
pub label: Span,
421423
pub sub: BuiltinUnpermittedTypeInitSub,
424+
pub tcx: TyCtxt<'a>,
422425
}
423426

424427
impl<'a> DecorateLint<'a, ()> for BuiltinUnpermittedTypeInit<'_> {
@@ -428,7 +431,13 @@ impl<'a> DecorateLint<'a, ()> for BuiltinUnpermittedTypeInit<'_> {
428431
) -> &'b mut rustc_errors::DiagnosticBuilder<'a, ()> {
429432
diag.set_arg("ty", self.ty);
430433
diag.span_label(self.label, fluent::lint_builtin_unpermitted_type_init_label);
431-
diag.span_label(self.label, fluent::lint_builtin_unpermitted_type_init_label_suggestion);
434+
if let InhabitedPredicate::True = self.ty.inhabited_predicate(self.tcx) {
435+
// Only suggest late `MaybeUninit::assume_init` initialization if the type is inhabited.
436+
diag.span_label(
437+
self.label,
438+
fluent::lint_builtin_unpermitted_type_init_label_suggestion,
439+
);
440+
}
432441
self.sub.add_to_diagnostic(diag);
433442
diag
434443
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ provide! { tcx, def_id, other, cdata,
254254
.process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
255255
}
256256

257+
associated_items_for_impl_trait_in_trait => { table_defaulted_array }
258+
257259
visibility => { cdata.get_visibility(def_id.index) }
258260
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
259261
adt_destructor => {

compiler/rustc_metadata/src/rmeta/encoder.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,11 @@ fn should_encode_trait_impl_trait_tys(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
11291129
})
11301130
}
11311131

1132+
// Return `false` to avoid encoding impl trait in trait, while we don't use the query.
1133+
fn should_encode_fn_impl_trait_in_trait<'tcx>(_tcx: TyCtxt<'tcx>, _def_id: DefId) -> bool {
1134+
false
1135+
}
1136+
11321137
impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11331138
fn encode_attrs(&mut self, def_id: LocalDefId) {
11341139
let tcx = self.tcx;
@@ -1137,8 +1142,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11371142
is_doc_hidden: false,
11381143
};
11391144
let attr_iter = tcx
1140-
.hir()
1141-
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
1145+
.opt_local_def_id_to_hir_id(def_id)
1146+
.map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
11421147
.iter()
11431148
.filter(|attr| analyze_attr(attr, &mut state));
11441149

@@ -1211,6 +1216,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12111216
{
12121217
record!(self.tables.trait_impl_trait_tys[def_id] <- table);
12131218
}
1219+
if should_encode_fn_impl_trait_in_trait(tcx, def_id) {
1220+
let table = tcx.associated_items_for_impl_trait_in_trait(def_id);
1221+
record_defaulted_array!(self.tables.associated_items_for_impl_trait_in_trait[def_id] <- table);
1222+
}
12141223
}
12151224

12161225
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {

compiler/rustc_metadata/src/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ define_tables! {
354354
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
355355
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
356356
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
357+
associated_items_for_impl_trait_in_trait: Table<DefIndex, LazyArray<DefId>>,
357358

358359
- optional:
359360
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,

compiler/rustc_middle/src/hir/map/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,13 @@ impl<'hir> Map<'hir> {
849849
}
850850
}
851851

852+
pub fn get_fn_output(self, def_id: LocalDefId) -> Option<&'hir FnRetTy<'hir>> {
853+
match self.tcx.hir_owner(OwnerId { def_id }) {
854+
Some(Owner { node, .. }) => node.fn_decl().map(|fn_decl| &fn_decl.output),
855+
_ => None,
856+
}
857+
}
858+
852859
pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
853860
match self.find(id) {
854861
Some(Node::Variant(variant)) => variant,

compiler/rustc_middle/src/hir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ pub fn provide(providers: &mut Providers) {
121121
let node = owner.node();
122122
Some(Owner { node, hash_without_bodies: owner.nodes.hash_without_bodies })
123123
};
124-
providers.local_def_id_to_hir_id = |tcx, id| {
124+
providers.opt_local_def_id_to_hir_id = |tcx, id| {
125125
let owner = tcx.hir_crate(()).owners[id].map(|_| ());
126-
match owner {
126+
Some(match owner {
127127
MaybeOwner::Owner(_) => HirId::make_owner(id),
128128
MaybeOwner::Phantom => bug!("No HirId for {:?}", id),
129129
MaybeOwner::NonOwner(hir_id) => hir_id,
130-
}
130+
})
131131
};
132132
providers.hir_owner_nodes = |tcx, id| tcx.hir_crate(()).owners[id.def_id].map(|i| &i.nodes);
133133
providers.hir_owner_parent = |tcx, id| {

compiler/rustc_middle/src/query/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,10 @@ rustc_queries! {
8585
desc { |tcx| "getting HIR owner of `{}`", tcx.def_path_str(key.to_def_id()) }
8686
}
8787

88-
/// Gives access to the HIR ID for the given `LocalDefId` owner `key`.
88+
/// Gives access to the HIR ID for the given `LocalDefId` owner `key` if any.
8989
///
90-
/// This can be conveniently accessed by methods on `tcx.hir()`.
91-
/// Avoid calling this query directly.
92-
query local_def_id_to_hir_id(key: LocalDefId) -> hir::HirId {
90+
/// Definitions that were generated with no HIR, would be feeded to return `None`.
91+
query opt_local_def_id_to_hir_id(key: LocalDefId) -> Option<hir::HirId>{
9392
desc { |tcx| "getting HIR ID of `{}`", tcx.def_path_str(key.to_def_id()) }
9493
}
9594

@@ -767,6 +766,26 @@ rustc_queries! {
767766
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
768767
}
769768

769+
/// Given `fn_def_id` of a trait or of an impl that implements a given trait:
770+
/// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns
771+
/// the associated items that correspond to each impl trait in return position for that trait.
772+
/// if `fn_def_id` is the def id of a function defined inside an impl that implements a trait, then it
773+
/// creates and returns the associated items that correspond to each impl trait in return position
774+
/// of the implemented trait.
775+
query associated_items_for_impl_trait_in_trait(fn_def_id: DefId) -> &'tcx [DefId] {
776+
desc { |tcx| "creating associated items for impl trait in trait returned by `{}`", tcx.def_path_str(fn_def_id) }
777+
cache_on_disk_if { fn_def_id.is_local() }
778+
separate_provide_extern
779+
}
780+
781+
/// Given an impl trait in trait `opaque_ty_def_id`, create and return the corresponding
782+
/// associated item.
783+
query associated_item_for_impl_trait_in_trait(opaque_ty_def_id: LocalDefId) -> LocalDefId {
784+
desc { |tcx| "creates the associated item corresponding to the opaque type `{}`", tcx.def_path_str(opaque_ty_def_id.to_def_id()) }
785+
cache_on_disk_if { true }
786+
separate_provide_extern
787+
}
788+
770789
/// Given an `impl_id`, return the trait it implements.
771790
/// Return `None` if this is an inherent impl.
772791
query impl_trait_ref(impl_id: DefId) -> Option<ty::EarlyBinder<ty::TraitRef<'tcx>>> {

compiler/rustc_middle/src/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,10 @@ impl<'tcx> TyCtxt<'tcx> {
24512451
)
24522452
}
24532453

2454+
pub fn local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> HirId {
2455+
self.opt_local_def_id_to_hir_id(local_def_id).unwrap()
2456+
}
2457+
24542458
pub fn trait_solver_next(self) -> bool {
24552459
self.sess.opts.unstable_opts.trait_solver == rustc_session::config::TraitSolver::Next
24562460
}

compiler/rustc_session/src/options.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1567,8 +1567,6 @@ options! {
15671567
"parse only; do not compile, assemble, or link (default: no)"),
15681568
perf_stats: bool = (false, parse_bool, [UNTRACKED],
15691569
"print some performance-related statistics (default: no)"),
1570-
pick_stable_methods_before_any_unstable: bool = (true, parse_bool, [TRACKED],
1571-
"try to pick stable methods first before picking any unstable methods (default: yes)"),
15721570
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],
15731571
"whether to use the PLT when calling into shared libraries;
15741572
only has effect for PIC code on systems with ELF binaries

compiler/rustc_symbol_mangling/src/typeid/typeid_itanium_cxx_abi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ fn encode_ty_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
396396
hir::definitions::DefPathData::CrateRoot
397397
| hir::definitions::DefPathData::Use
398398
| hir::definitions::DefPathData::GlobalAsm
399+
| hir::definitions::DefPathData::ImplTraitAssocTy
399400
| hir::definitions::DefPathData::MacroNs(..)
400401
| hir::definitions::DefPathData::LifetimeNs(..) => {
401402
bug!("encode_ty_name: unexpected `{:?}`", disambiguated_data.data);

compiler/rustc_symbol_mangling/src/v0.rs

+1
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
791791
| DefPathData::Use
792792
| DefPathData::GlobalAsm
793793
| DefPathData::Impl
794+
| DefPathData::ImplTraitAssocTy
794795
| DefPathData::MacroNs(_)
795796
| DefPathData::LifetimeNs(_) => {
796797
bug!("symbol_names: unexpected DefPathData: {:?}", disambiguated_data.data)

0 commit comments

Comments
 (0)