Skip to content

Commit

Permalink
Tweak E0478
Browse files Browse the repository at this point in the history
```
error[E0478]: lifetime bound not satisfied
  --> $DIR/point-at-lifetime-obligation-from-trait-in-trait-object.rs:4:27
   |
LL |     broken: Box<dyn Any + 'a>
   |                     ---   ^^ lifetime bound not satisfied
   |                     |
   |                     this requires `'static`
   |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
  --> $DIR/point-at-lifetime-obligation-from-trait-in-trait-object.rs:3:18
   |
LL | struct Something<'a> {
   |                  ^^
   = note: but lifetime parameter must outlive the static lifetime
note: `'static` requirement introduced here
  --> $SRC_DIR/core/src/any.rs:LL:COL
```
  • Loading branch information
estebank committed Mar 1, 2024
1 parent b163ee0 commit 0f249f5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 20 deletions.
41 changes: 29 additions & 12 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use crate::traits::{
PredicateObligation,
};

use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{
codes::*, pluralize, struct_span_code_err, Applicability, Diag, DiagCtxt, DiagStyledString,
ErrorGuaranteed, IntoDiagnosticArg, MultiSpan,
Expand Down Expand Up @@ -2698,7 +2698,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
pub struct HirTraitObjectVisitor<'tcx> {
pub expected_region: ty::Region<'tcx>,
pub found_region: ty::Region<'tcx>,
pub lifetime_spans: FxHashSet<Span>,
pub primary_spans: Vec<Span>,
pub secondary_spans: Vec<Span>,
pub pred_spans: Vec<Span>,
pub tcx: TyCtxt<'tcx>,
}
Expand All @@ -2716,7 +2717,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
_ => false,
} {
// We want to keep a span to the lifetime bound on the trait object.
self.lifetime_spans.insert(lt.ident.span);
self.primary_spans.push(lt.ident.span);
}
}
fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
Expand All @@ -2742,18 +2743,20 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
.filter_map(filter_predicates(self.found_region, |_| true))
.collect();
if !bindings.is_empty() {
self.lifetime_spans.insert(ptr.span);
self.secondary_spans.push(ptr.span);
self.pred_spans.extend(bindings);
}
}
}
}
// Detect when an associated item is given a lifetime restriction that the
// definition of that associated item couldn't meet.
hir::TyKind::Path(hir::QPath::Resolved(Some(_), path)) => {
self.pred_spans = elaborate_predicates_of(self.tcx, path.res.def_id())
.filter_map(filter_predicates(self.found_region, |_| true))
.collect();
hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
self.pred_spans.extend(
elaborate_predicates_of(self.tcx, path.res.def_id())
.filter_map(filter_predicates(self.found_region, |_| true))
.collect::<Vec<_>>(),
);
}
_ => {}
}
Expand All @@ -2763,7 +2766,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let mut visitor = HirTraitObjectVisitor {
expected_region: sup,
found_region: sub,
lifetime_spans: Default::default(),
primary_spans: vec![],
secondary_spans: vec![],
pred_spans: vec![],
tcx: self.tcx,
};
Expand All @@ -2775,9 +2779,22 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}
}

#[allow(rustc::potential_query_instability)]
let primary_spans: Vec<Span> = visitor.lifetime_spans.into_iter().collect();
Some((primary_spans.into(), visitor.pred_spans.into()))
visitor.primary_spans.sort();
let mut primary_span: MultiSpan = visitor.primary_spans.clone().into();
if let Some(last) = visitor.primary_spans.iter().rev().next() {
primary_span.push_span_label(
*last,
format!(
"lifetime bound{s} not satisfied",
s = pluralize!(visitor.primary_spans.len())
),
);
}

for span in visitor.secondary_spans {
primary_span.push_span_label(span, format!("this requires `{sub}`"));
}
Some((primary_span, visitor.pred_spans.into()))
}

/// Determine whether an error associated with the given span and definition
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0478.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0478]: lifetime bound not satisfied
--> $DIR/E0478.rs:4:37
|
LL | child: Box<dyn Wedding<'kiss> + 'SnowWhite>,
| ^^^^^^^^^^
| ^^^^^^^^^^ lifetime bound not satisfied
|
note: lifetime parameter instantiated with the lifetime `'SnowWhite` as defined here
--> $DIR/E0478.rs:3:22
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ error[E0478]: lifetime bound not satisfied
--> $DIR/unsatisfied-item-lifetime-bound.rs:14:20
|
LL | f: <T as X>::Y<'a>,
| ^^
| ^^ lifetime bound not satisfied
|
note: lifetime parameter instantiated with the lifetime `'a` as defined here
--> $DIR/unsatisfied-item-lifetime-bound.rs:13:10
Expand All @@ -53,7 +53,7 @@ error[E0478]: lifetime bound not satisfied
--> $DIR/unsatisfied-item-lifetime-bound.rs:19:20
|
LL | f: <T as X>::Y<'a>,
| ^^
| ^^ lifetime bound not satisfied
|
note: lifetime parameter instantiated with the lifetime `'a` as defined here
--> $DIR/unsatisfied-item-lifetime-bound.rs:18:10
Expand All @@ -71,7 +71,7 @@ error[E0478]: lifetime bound not satisfied
--> $DIR/unsatisfied-item-lifetime-bound.rs:24:21
|
LL | f: <() as X>::Y<'a>,
| ^^
| ^^ lifetime bound not satisfied
|
note: lifetime parameter instantiated with the lifetime `'a` as defined here
--> $DIR/unsatisfied-item-lifetime-bound.rs:23:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error[E0478]: lifetime bound not satisfied
--> $DIR/point-at-lifetime-obligation-from-trait-in-trait-object.rs:4:21
--> $DIR/point-at-lifetime-obligation-from-trait-in-trait-object.rs:4:27
|
LL | broken: Box<dyn Any + 'a>
| ^^^ ^^
| --- ^^ lifetime bound not satisfied
| |
| this requires `'static`
|
note: lifetime parameter instantiated with the lifetime `'a` as defined here
--> $DIR/point-at-lifetime-obligation-from-trait-in-trait-object.rs:3:18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ error[E0478]: lifetime bound not satisfied
--> $DIR/region-bounds-on-objects-and-type-parameters.rs:21:23
|
LL | z: Box<dyn Is<'a>+'b+'c>,
| ^^
| ^^ lifetime bound not satisfied
|
note: lifetime parameter instantiated with the lifetime `'b` as defined here
--> $DIR/region-bounds-on-objects-and-type-parameters.rs:11:15
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/regions/regions-wf-trait-object.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0478]: lifetime bound not satisfied
--> $DIR/regions-wf-trait-object.rs:7:29
|
LL | x: Box<dyn TheTrait<'a>+'b>
| ^^
| ^^ lifetime bound not satisfied
|
note: lifetime parameter instantiated with the lifetime `'b` as defined here
--> $DIR/regions-wf-trait-object.rs:6:15
Expand Down

0 comments on commit 0f249f5

Please sign in to comment.