Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve anonymous lifetime note to indicate the target span #82370

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use super::region_constraints::GenericKind;
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};

use crate::infer;
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
use crate::traits::error_reporting::report_object_safety_error;
use crate::traits::{
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
Expand Down Expand Up @@ -179,7 +180,14 @@ fn msg_span_from_early_bound_and_free_regions(
}
ty::ReFree(ref fr) => match fr.bound_region {
ty::BrAnon(idx) => {
(format!("the anonymous lifetime #{} defined on", idx + 1), tcx.hir().span(node))
if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) {
("the anonymous lifetime defined on".to_string(), ty.span)
} else {
(
format!("the anonymous lifetime #{} defined on", idx + 1),
tcx.hir().span(node),
)
}
}
_ => (
format!("the lifetime `{}` as defined on", region),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Error Reporting for Anonymous Region Lifetime Errors
//! where both the regions are anonymous.

use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo;
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError;
Expand Down Expand Up @@ -66,9 +67,9 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let scope_def_id_sub = anon_reg_sub.def_id;
let bregion_sub = anon_reg_sub.boundregion;

let ty_sup = self.find_anon_type(sup, &bregion_sup)?;
let ty_sup = find_anon_type(self.tcx(), sup, &bregion_sup)?;

let ty_sub = self.find_anon_type(sub, &bregion_sub)?;
let ty_sub = find_anon_type(self.tcx(), sub, &bregion_sub)?;

debug!(
"try_report_anon_anon_conflict: found_param1={:?} sup={:?} br1={:?}",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,68 @@
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_hir as hir;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::Node;
use rustc_middle::hir::map::Map;
use rustc_middle::middle::resolve_lifetime as rl;
use rustc_middle::ty::{self, Region, TyCtxt};

impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// This function calls the `visit_ty` method for the parameters
/// corresponding to the anonymous regions. The `nested_visitor.found_type`
/// contains the anonymous type.
///
/// # Arguments
/// region - the anonymous region corresponding to the anon_anon conflict
/// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
///
/// # Example
/// ```
/// fn foo(x: &mut Vec<&u8>, y: &u8)
/// { x.push(y); }
/// ```
/// The function returns the nested type corresponding to the anonymous region
/// for e.g., `&u8` and Vec<`&u8`.
pub(super) fn find_anon_type(
&self,
region: Region<'tcx>,
br: &ty::BoundRegionKind,
) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> {
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id);
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref m, ..),
..
})
| Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(ref m, ..), ..
}) => &m.decl,
_ => return None,
};
/// This function calls the `visit_ty` method for the parameters
/// corresponding to the anonymous regions. The `nested_visitor.found_type`
/// contains the anonymous type.
///
/// # Arguments
/// region - the anonymous region corresponding to the anon_anon conflict
/// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
///
/// # Example
/// ```
/// fn foo(x: &mut Vec<&u8>, y: &u8)
/// { x.push(y); }
/// ```
/// The function returns the nested type corresponding to the anonymous region
/// for e.g., `&u8` and Vec<`&u8`.
pub(crate) fn find_anon_type(
tcx: TyCtxt<'tcx>,
region: Region<'tcx>,
br: &ty::BoundRegionKind,
) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnDecl<'tcx>)> {
if let Some(anon_reg) = tcx.is_suitable_region(region) {
let hir_id = tcx.hir().local_def_id_to_hir_id(anon_reg.def_id);
let fndecl = match tcx.hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref m, ..), ..
})
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref m, ..), .. }) => {
&m.decl
}
_ => return None,
};

fndecl
.inputs
.iter()
.find_map(|arg| self.find_component_for_bound_region(arg, br))
.map(|ty| (ty, &**fndecl))
} else {
None
}
fndecl
.inputs
.iter()
.find_map(|arg| find_component_for_bound_region(tcx, arg, br))
.map(|ty| (ty, &**fndecl))
} else {
None
}
}

// This method creates a FindNestedTypeVisitor which returns the type corresponding
// to the anonymous region.
fn find_component_for_bound_region(
&self,
arg: &'tcx hir::Ty<'tcx>,
br: &ty::BoundRegionKind,
) -> Option<&'tcx hir::Ty<'tcx>> {
let mut nested_visitor = FindNestedTypeVisitor {
tcx: self.tcx(),
bound_region: *br,
found_type: None,
current_index: ty::INNERMOST,
};
nested_visitor.visit_ty(arg);
nested_visitor.found_type
}
// This method creates a FindNestedTypeVisitor which returns the type corresponding
// to the anonymous region.
fn find_component_for_bound_region(
tcx: TyCtxt<'tcx>,
arg: &'tcx hir::Ty<'tcx>,
br: &ty::BoundRegionKind,
) -> Option<&'tcx hir::Ty<'tcx>> {
let mut nested_visitor = FindNestedTypeVisitor {
tcx,
bound_region: *br,
found_type: None,
current_index: ty::INNERMOST,
};
nested_visitor.visit_ty(arg);
nested_visitor.found_type
}

// The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_span::source_map::Span;

mod different_lifetimes;
mod find_anon_type;
pub mod find_anon_type;
mod named_anon_conflict;
mod placeholder_error;
mod static_impl_trait;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Error Reporting for Anonymous Region Lifetime Errors
//! where one region is named and the other is anonymous.
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir::intravisit::Visitor;
Expand Down Expand Up @@ -74,7 +75,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
return None;
}

if let Some((_, fndecl)) = self.find_anon_type(anon, &br) {
if let Some((_, fndecl)) = find_anon_type(self.tcx(), anon, &br) {
if self.is_self_anon(is_first, scope_def_id) {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ LL | | t.test();
LL | | });
| |______^
|
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
note: the parameter type `T` must be valid for the anonymous lifetime defined on the function body at 19:24...
--> $DIR/missing-lifetimes-in-signature-2.rs:19:24
|
LL | fn func<T: Test>(foo: &Foo, t: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^

error: aborting due to previous error

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ LL | fn func<T: Test>(foo: &Foo, t: T) {
LL | foo.bar(move |_| {
| ^^^
|
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
note: the parameter type `T` must be valid for the anonymous lifetime defined on the function body at 19:24...
--> $DIR/missing-lifetimes-in-signature-2.rs:19:24
|
LL | fn func<T: Test>(foo: &Foo, t: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature-2.rs:20:13: 23:6]` will meet its required lifetime bounds
--> $DIR/missing-lifetimes-in-signature-2.rs:20:9
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,47 @@ error[E0311]: the parameter type `G` may not live long enough
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 25:1...
--> $DIR/missing-lifetimes-in-signature.rs:25:1
|
LL | / fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
LL | |
LL | | where
LL | | G: Get<T>
| |_____________^
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 25:26...
--> $DIR/missing-lifetimes-in-signature.rs:25:26
|
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^

error[E0311]: the parameter type `G` may not live long enough
--> $DIR/missing-lifetimes-in-signature.rs:47:45
|
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 47:1...
--> $DIR/missing-lifetimes-in-signature.rs:47:1
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 47:34...
--> $DIR/missing-lifetimes-in-signature.rs:47:34
|
LL | / fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
LL | |
LL | | where
LL | | G: Get<T>
| |_____________^
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^

error[E0311]: the parameter type `G` may not live long enough
--> $DIR/missing-lifetimes-in-signature.rs:59:58
|
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
| ^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the method body at 59:5...
--> $DIR/missing-lifetimes-in-signature.rs:59:5
note: the parameter type `G` must be valid for the anonymous lifetime defined on the method body at 59:47...
--> $DIR/missing-lifetimes-in-signature.rs:59:47
|
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^

error[E0311]: the parameter type `G` may not live long enough
--> $DIR/missing-lifetimes-in-signature.rs:68:45
|
LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
| ^^^^^^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 68:1...
--> $DIR/missing-lifetimes-in-signature.rs:68:1
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 68:34...
--> $DIR/missing-lifetimes-in-signature.rs:68:34
|
LL | / fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
LL | |
LL | | where
LL | | G: Get<T>
| |_____________^
LL | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
| ^^^^^^

error[E0621]: explicit lifetime required in the type of `dest`
--> $DIR/missing-lifetimes-in-signature.rs:73:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@ error[E0311]: the parameter type `G` may not live long enough
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 25:1...
--> $DIR/missing-lifetimes-in-signature.rs:25:1
|
LL | / fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
LL | |
LL | | where
LL | | G: Get<T>
| |_____________^
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 25:26...
--> $DIR/missing-lifetimes-in-signature.rs:25:26
|
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:30:5: 32:6]` will meet its required lifetime bounds
--> $DIR/missing-lifetimes-in-signature.rs:25:37
|
Expand All @@ -57,14 +54,11 @@ error[E0311]: the parameter type `G` may not live long enough
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the function body at 47:1...
--> $DIR/missing-lifetimes-in-signature.rs:47:1
note: the parameter type `G` must be valid for the anonymous lifetime defined on the function body at 47:34...
--> $DIR/missing-lifetimes-in-signature.rs:47:34
|
LL | / fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
LL | |
LL | | where
LL | | G: Get<T>
| |_____________^
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
| ^^^^^^
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:52:5: 54:6]` will meet its required lifetime bounds
--> $DIR/missing-lifetimes-in-signature.rs:47:45
|
Expand All @@ -81,11 +75,11 @@ error[E0311]: the parameter type `G` may not live long enough
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
| ^^^^^^^^^^^^^^^^^^
|
note: the parameter type `G` must be valid for the anonymous lifetime #1 defined on the method body at 59:5...
--> $DIR/missing-lifetimes-in-signature.rs:59:5
note: the parameter type `G` must be valid for the anonymous lifetime defined on the method body at 59:47...
--> $DIR/missing-lifetimes-in-signature.rs:59:47
|
LL | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:61:9: 63:10]` will meet its required lifetime bounds
--> $DIR/missing-lifetimes-in-signature.rs:59:58
|
Expand Down