Skip to content

Commit 2baee88

Browse files
Address comments
1 parent 7690fe3 commit 2baee88

File tree

4 files changed

+52
-117
lines changed

4 files changed

+52
-117
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
535535
// x += 1;
536536
// ```
537537
!i.contains(span)
538-
// We filter these to avoid incorrect main message on `match-cfg-fake-edges.rs`
539-
&& !visitor
540-
.errors
541-
.iter()
542-
.map(|(sp, _)| *sp)
543-
.any(|sp| span < sp && !sp.contains(span))
538+
// We filter these to avoid incorrect main message on `match-cfg-fake-edges.rs`
539+
&& !visitor
540+
.errors
541+
.iter()
542+
.map(|(sp, _)| *sp)
543+
.any(|sp| span < sp && !sp.contains(span))
544544
}) {
545545
show_assign_sugg = true;
546546
"isn't initialized"

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-61
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
55
use rustc_data_structures::fx::FxIndexSet;
66
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
7+
use rustc_hir as hir;
78
use rustc_hir::def_id::DefId;
89
use rustc_hir::intravisit::Visitor;
9-
use rustc_hir::{self as hir, Item, ItemKind, Node};
1010
use rustc_infer::infer::{
1111
error_reporting::nice_region_error::{
1212
self, find_anon_type, find_param_with_region, suggest_adding_lifetime_params,
@@ -291,65 +291,6 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
291291
outlives_suggestion.add_suggestion(self);
292292
}
293293

294-
fn get_impl_ident_and_self_ty_from_trait(
295-
&self,
296-
def_id: DefId,
297-
trait_objects: &FxIndexSet<DefId>,
298-
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
299-
let tcx = self.infcx.tcx;
300-
match tcx.hir().get_if_local(def_id) {
301-
Some(Node::ImplItem(impl_item)) => {
302-
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id()).def_id)
303-
{
304-
Some(Node::Item(Item {
305-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
306-
..
307-
})) => Some((impl_item.ident, self_ty)),
308-
_ => None,
309-
}
310-
}
311-
Some(Node::TraitItem(trait_item)) => {
312-
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
313-
match tcx.hir().find_by_def_id(trait_did.def_id) {
314-
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
315-
// The method being called is defined in the `trait`, but the `'static`
316-
// obligation comes from the `impl`. Find that `impl` so that we can point
317-
// at it in the suggestion.
318-
let trait_did = trait_did.to_def_id();
319-
match tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
320-
match tcx.hir().get_if_local(impl_did.to_def_id()) {
321-
Some(Node::Item(Item {
322-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
323-
..
324-
})) if trait_objects.iter().all(|did| {
325-
// FIXME: we should check `self_ty` against the receiver
326-
// type in the `UnifyReceiver` context, but for now, use
327-
// this imperfect proxy. This will fail if there are
328-
// multiple `impl`s for the same trait like
329-
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
330-
// In that case, only the first one will get suggestions.
331-
let mut traits = vec![];
332-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
333-
hir_v.visit_ty(self_ty);
334-
!traits.is_empty()
335-
}) =>
336-
{
337-
Some(self_ty)
338-
}
339-
_ => None,
340-
}
341-
}) {
342-
Some(self_ty) => Some((trait_item.ident, self_ty)),
343-
_ => None,
344-
}
345-
}
346-
_ => None,
347-
}
348-
}
349-
_ => None,
350-
}
351-
}
352-
353294
/// Report an error because the universal region `fr` was required to outlive
354295
/// `outlived_fr` but it is not known to do so. For example:
355296
///
@@ -844,7 +785,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
844785
visitor.visit_ty(param.param_ty);
845786

846787
let Some((ident, self_ty)) =
847-
self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &visitor.0) else {return};
788+
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, instance.def_id(), &visitor.0) else { return; };
848789

849790
self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
850791
}

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+43-49
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
239239
let mut v = TraitObjectVisitor(FxIndexSet::default());
240240
v.visit_ty(param.param_ty);
241241
if let Some((ident, self_ty)) =
242-
self.get_impl_ident_and_self_ty_from_trait(item_def_id, &v.0)
242+
NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, item_def_id, &v.0)
243243
&& self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
244244
{
245245
override_error_code = Some(ident.name);
@@ -390,60 +390,54 @@ pub fn suggest_new_region_bound(
390390
}
391391

392392
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
393-
fn get_impl_ident_and_self_ty_from_trait(
394-
&self,
393+
pub fn get_impl_ident_and_self_ty_from_trait(
394+
tcx: TyCtxt<'tcx>,
395395
def_id: DefId,
396396
trait_objects: &FxIndexSet<DefId>,
397397
) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
398-
let tcx = self.tcx();
399-
match tcx.hir().get_if_local(def_id) {
400-
Some(Node::ImplItem(impl_item)) => {
401-
match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id()).def_id)
398+
match tcx.hir().get_if_local(def_id)? {
399+
Node::ImplItem(impl_item) => {
400+
let impl_did = tcx.hir().get_parent_item(impl_item.hir_id());
401+
if let hir::OwnerNode::Item(Item {
402+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
403+
..
404+
}) = tcx.hir().owner(impl_did)
402405
{
403-
Some(Node::Item(Item {
404-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
405-
..
406-
})) => Some((impl_item.ident, self_ty)),
407-
_ => None,
406+
Some((impl_item.ident, self_ty))
407+
} else {
408+
None
408409
}
409410
}
410-
Some(Node::TraitItem(trait_item)) => {
411-
let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
412-
match tcx.hir().find_by_def_id(trait_did.def_id) {
413-
Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
414-
// The method being called is defined in the `trait`, but the `'static`
415-
// obligation comes from the `impl`. Find that `impl` so that we can point
416-
// at it in the suggestion.
417-
let trait_did = trait_did.to_def_id();
418-
match tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
419-
match tcx.hir().get_if_local(impl_did.to_def_id()) {
420-
Some(Node::Item(Item {
421-
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
422-
..
423-
})) if trait_objects.iter().all(|did| {
424-
// FIXME: we should check `self_ty` against the receiver
425-
// type in the `UnifyReceiver` context, but for now, use
426-
// this imperfect proxy. This will fail if there are
427-
// multiple `impl`s for the same trait like
428-
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
429-
// In that case, only the first one will get suggestions.
430-
let mut traits = vec![];
431-
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
432-
hir_v.visit_ty(self_ty);
433-
!traits.is_empty()
434-
}) =>
435-
{
436-
Some(self_ty)
437-
}
438-
_ => None,
439-
}
440-
}) {
441-
Some(self_ty) => Some((trait_item.ident, self_ty)),
442-
_ => None,
443-
}
411+
Node::TraitItem(trait_item) => {
412+
let trait_id = tcx.hir().get_parent_item(trait_item.hir_id());
413+
debug_assert_eq!(tcx.def_kind(trait_id.def_id), hir::def::DefKind::Trait);
414+
// The method being called is defined in the `trait`, but the `'static`
415+
// obligation comes from the `impl`. Find that `impl` so that we can point
416+
// at it in the suggestion.
417+
let trait_did = trait_id.to_def_id();
418+
tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
419+
if let Node::Item(Item {
420+
kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
421+
..
422+
}) = tcx.hir().find_by_def_id(impl_did)?
423+
&& trait_objects.iter().all(|did| {
424+
// FIXME: we should check `self_ty` against the receiver
425+
// type in the `UnifyReceiver` context, but for now, use
426+
// this imperfect proxy. This will fail if there are
427+
// multiple `impl`s for the same trait like
428+
// `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
429+
// In that case, only the first one will get suggestions.
430+
let mut traits = vec![];
431+
let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
432+
hir_v.visit_ty(self_ty);
433+
!traits.is_empty()
434+
})
435+
{
436+
Some((trait_item.ident, *self_ty))
437+
} else {
438+
None
444439
}
445-
_ => None,
446-
}
440+
})
447441
}
448442
_ => None,
449443
}
@@ -474,7 +468,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
474468

475469
// Get the `Ident` of the method being called and the corresponding `impl` (to point at
476470
// `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
477-
let Some((ident, self_ty)) = self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &v.0) else {
471+
let Some((ident, self_ty)) = NiceRegionError::get_impl_ident_and_self_ty_from_trait(tcx, instance.def_id(), &v.0) else {
478472
return false;
479473
};
480474

compiler/rustc_lint/src/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
276276
{
277277
let def_id = trait_ref.def_id;
278278
is_def_must_use(cx, def_id, span)
279+
.map(|inner| MustUsePath::TraitObject(Box::new(inner)))
279280
} else {
280281
None
281282
}
282-
.map(|inner| MustUsePath::TraitObject(Box::new(inner)))
283283
}),
284284
ty::Tuple(tys) => {
285285
let elem_exprs = if let hir::ExprKind::Tup(elem_exprs) = expr.kind {

0 commit comments

Comments
 (0)