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

Detect non-static lifetime behind reference type parameter #116

Merged
merged 2 commits into from Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 22 additions & 15 deletions impl/src/valid.rs
Expand Up @@ -188,7 +188,7 @@ fn check_field_attrs(fields: &[Field]) -> Result<()> {
}
}
if let Some(source_field) = source_field.or(from_field) {
if contains_non_static_lifetime(source_field) {
if contains_non_static_lifetime(&source_field.ty) {
return Err(Error::new_spanned(
&source_field.original.ty,
"non-static lifetimes are not allowed in the source of an error, because std::error::Error requires the source is dyn Error + 'static",
Expand All @@ -206,21 +206,28 @@ fn same_member(one: &Field, two: &Field) -> bool {
}
}

fn contains_non_static_lifetime(field: &Field) -> bool {
let ty = match field.ty {
Type::Path(ty) => ty,
_ => return false, // maybe implement later if there are common other cases
};
let bracketed = match &ty.path.segments.last().unwrap().arguments {
PathArguments::AngleBracketed(bracketed) => bracketed,
_ => return false,
};
for arg in &bracketed.args {
if let GenericArgument::Lifetime(lifetime) = arg {
if lifetime.ident != "static" {
return true;
fn contains_non_static_lifetime(ty: &Type) -> bool {
match ty {
Type::Path(ty) => {
let bracketed = match &ty.path.segments.last().unwrap().arguments {
PathArguments::AngleBracketed(bracketed) => bracketed,
_ => return false,
};
for arg in &bracketed.args {
match arg {
GenericArgument::Type(ty) if contains_non_static_lifetime(ty) => return true,
GenericArgument::Lifetime(lifetime) if lifetime.ident != "static" => {
return true
}
_ => {}
}
}
false
}
Type::Reference(ty) => ty
.lifetime
.as_ref()
.map_or(false, |lifetime| lifetime.ident != "static"),
_ => false, // maybe implement later if there are common other cases
}
false
}
11 changes: 11 additions & 0 deletions tests/ui/lifetime.rs
@@ -1,3 +1,4 @@
use std::fmt::Debug;
use thiserror::Error;

#[derive(Error, Debug)]
Expand All @@ -8,6 +9,16 @@ struct Error<'a>(#[from] Inner<'a>);
#[error("{0}")]
struct Inner<'a>(&'a str);

#[derive(Error, Debug)]
enum Enum<'a> {
#[error("error")]
Foo(#[from] Generic<&'a str>),
}

#[derive(Error, Debug)]
#[error("{0:?}")]
struct Generic<T: Debug>(T);

fn main() -> Result<(), Error<'static>> {
Err(Error(Inner("some text")))
}
10 changes: 8 additions & 2 deletions tests/ui/lifetime.stderr
@@ -1,5 +1,11 @@
error: non-static lifetimes are not allowed in the source of an error, because std::error::Error requires the source is dyn Error + 'static
--> $DIR/lifetime.rs:5:26
--> $DIR/lifetime.rs:6:26
|
5 | struct Error<'a>(#[from] Inner<'a>);
6 | struct Error<'a>(#[from] Inner<'a>);
| ^^^^^^^^^

error: non-static lifetimes are not allowed in the source of an error, because std::error::Error requires the source is dyn Error + 'static
--> $DIR/lifetime.rs:15:17
|
15 | Foo(#[from] Generic<&'a str>),
| ^^^^^^^^^^^^^^^^