Skip to content

Commit

Permalink
place explicit lifetime bound after generic param
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed May 9, 2024
1 parent 87293c9 commit 58b918f
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
21 changes: 19 additions & 2 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let (type_scope, type_param_sugg_span) = match bound_kind {
GenericKind::Param(param) => {
let generics = self.tcx.generics_of(generic_param_scope);
let def_id = generics.type_param(param, self.tcx).def_id.expect_local();
let type_param = generics.type_param(param, self.tcx);
let def_id = type_param.def_id.expect_local();
let scope = self.tcx.local_def_id_to_hir_id(def_id).owner.def_id;
// Get the `hir::Param` to verify whether it already has any bounds.
// We do this to avoid suggesting code that ends up as `T: 'a'b`,
Expand All @@ -2384,7 +2385,23 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
Some((span, open_paren_sp)) => Some((span, true, open_paren_sp)),
// If `param` corresponds to `Self`, no usable suggestion span.
None if generics.has_self && param.index == 0 => None,
None => Some((self.tcx.def_span(def_id).shrink_to_hi(), false, None)),
None => {
let span = self.tcx.def_span(def_id);
let span = if type_param.default_value(self.tcx).is_some() {
let start = span.shrink_to_lo();
self.tcx
.sess
.source_map()
.span_extend_while(start, |char| {
!char.is_whitespace() && char != '='
})
.unwrap()
.shrink_to_hi()
} else {
span.shrink_to_hi()
};
Some((span, false, None))
}
};
(scope, sugg_span)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ error-pattern: r#T: 'static
//@ error-pattern: r#K: 'static
//@ error-pattern: T: 'static=

// https://github.com/rust-lang/rust/issues/124785

struct Foo<T, K = i32>(&'static T, &'static K);
//~^ ERROR: the parameter type `T` may not live long enough
//~| ERROR: the parameter type `K` may not live long enough

struct Bar<r#T, r#K = i32>(&'static r#T, &'static r#K);
//~^ ERROR: the parameter type `T` may not live long enough
//~| ERROR: the parameter type `K` may not live long enough

struct Boo<T= i32>(&'static T);
//~^ ERROR: the parameter type `T` may not live long enough

struct Far<T
= i32>(&'static T);
//~^ ERROR: the parameter type `T` may not live long enough

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
error[E0310]: the parameter type `T` may not live long enough
--> $DIR/static-lifetime-tip-with-default-type.rs:7:24
|
LL | struct Foo<T, K = i32>(&'static T, &'static K);
| ^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Foo<T: 'static, K = i32>(&'static T, &'static K);
| +++++++++

error[E0310]: the parameter type `K` may not live long enough
--> $DIR/static-lifetime-tip-with-default-type.rs:7:36
|
LL | struct Foo<T, K = i32>(&'static T, &'static K);
| ^^^^^^^^^^
| |
| the parameter type `K` must be valid for the static lifetime...
| ...so that the reference type `&'static K` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Foo<T, K: 'static = i32>(&'static T, &'static K);
| +++++++++

error[E0310]: the parameter type `T` may not live long enough
--> $DIR/static-lifetime-tip-with-default-type.rs:11:28
|
LL | struct Bar<r#T, r#K = i32>(&'static r#T, &'static r#K);
| ^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Bar<r#T: 'static, r#K = i32>(&'static r#T, &'static r#K);
| +++++++++

error[E0310]: the parameter type `K` may not live long enough
--> $DIR/static-lifetime-tip-with-default-type.rs:11:42
|
LL | struct Bar<r#T, r#K = i32>(&'static r#T, &'static r#K);
| ^^^^^^^^^^^^
| |
| the parameter type `K` must be valid for the static lifetime...
| ...so that the reference type `&'static K` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Bar<r#T, r#K: 'static = i32>(&'static r#T, &'static r#K);
| +++++++++

error[E0310]: the parameter type `T` may not live long enough
--> $DIR/static-lifetime-tip-with-default-type.rs:15:20
|
LL | struct Boo<T= i32>(&'static T);
| ^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Boo<T: 'static= i32>(&'static T);
| +++++++++

error[E0310]: the parameter type `T` may not live long enough
--> $DIR/static-lifetime-tip-with-default-type.rs:19:8
|
LL | = i32>(&'static T);
| ^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound
|
LL | struct Far<T: 'static
| +++++++++

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0310`.

0 comments on commit 58b918f

Please sign in to comment.