fix(derive): elide lifetimes in derived functions

This commit is contained in:
Daniel Rivas 2022-11-19 17:00:00 +00:00
parent c857595e1a
commit 408554f3ad
5 changed files with 34 additions and 34 deletions

View File

@ -72,7 +72,7 @@ impl Code {
pub(crate) fn gen_struct(&self) -> Option<TokenStream> { pub(crate) fn gen_struct(&self) -> Option<TokenStream> {
let code = &self.0; let code = &self.0;
Some(quote! { Some(quote! {
fn code<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> { fn code(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
std::option::Option::Some(std::boxed::Box::new(#code)) std::option::Option::Some(std::boxed::Box::new(#code))
} }
}) })

View File

@ -58,13 +58,13 @@ impl WhichFn {
pub fn signature(&self) -> TokenStream { pub fn signature(&self) -> TokenStream {
match self { match self {
Self::Code => quote! { Self::Code => quote! {
fn code<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> fn code(& self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>>
}, },
Self::Help => quote! { Self::Help => quote! {
fn help<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> fn help(& self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>>
}, },
Self::Url => quote! { Self::Url => quote! {
fn url<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> fn url(& self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>>
}, },
Self::Severity => quote! { Self::Severity => quote! {
fn severity(&self) -> std::option::Option<miette::Severity> fn severity(&self) -> std::option::Option<miette::Severity>

View File

@ -108,7 +108,7 @@ impl Help {
Some(quote! { Some(quote! {
Self::#ident #display_pat => { Self::#ident #display_pat => {
use miette::macro_helpers::ToOption; use miette::macro_helpers::ToOption;
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&#help).as_ref().map(|#var| -> std::boxed::Box<dyn std::fmt::Display + 'a> { std::boxed::Box::new(format!("{}", #var)) }) miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&#help).as_ref().map(|#var| -> std::boxed::Box<dyn std::fmt::Display + '_> { std::boxed::Box::new(format!("{}", #var)) })
}, },
}) })
} }
@ -123,7 +123,7 @@ impl Help {
Help::Display(display) => { Help::Display(display) => {
let (fmt, args) = display.expand_shorthand_cloned(&display_members); let (fmt, args) = display.expand_shorthand_cloned(&display_members);
Some(quote! { Some(quote! {
fn help<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> { fn help(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
#[allow(unused_variables, deprecated)] #[allow(unused_variables, deprecated)]
let Self #display_pat = self; let Self #display_pat = self;
std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args))) std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args)))
@ -133,11 +133,11 @@ impl Help {
Help::Field(member, ty) => { Help::Field(member, ty) => {
let var = quote! { __miette_internal_var }; let var = quote! { __miette_internal_var };
Some(quote! { Some(quote! {
fn help<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> { fn help(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
#[allow(unused_variables, deprecated)] #[allow(unused_variables, deprecated)]
let Self #display_pat = self; let Self #display_pat = self;
use miette::macro_helpers::ToOption; use miette::macro_helpers::ToOption;
miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&self.#member).as_ref().map(|#var| -> std::boxed::Box<dyn std::fmt::Display + 'a> { std::boxed::Box::new(format!("{}", #var)) }) miette::macro_helpers::OptionalWrapper::<#ty>::new().to_option(&self.#member).as_ref().map(|#var| -> std::boxed::Box<dyn std::fmt::Display + '_> { std::boxed::Box::new(format!("{}", #var)) })
} }
}) })
} }

View File

@ -129,7 +129,7 @@ impl Url {
} }
}; };
Some(quote! { Some(quote! {
fn url<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> { fn url(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
#[allow(unused_variables, deprecated)] #[allow(unused_variables, deprecated)]
let Self #pat = self; let Self #pat = self;
std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args))) std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args)))

View File

@ -185,7 +185,7 @@ fn fmt_help() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic(code(foo::bar::baz), help("{} x {0} x {:?}", 1, "2"))] #[diagnostic(code(foo::bar::baz), help("{} x {0} x {:?}", 1, "2"))]
struct FooStruct(String); struct FooStruct<'a>(&'a str);
assert_eq!( assert_eq!(
"1 x hello x \"2\"".to_string(), "1 x hello x \"2\"".to_string(),
@ -195,8 +195,8 @@ fn fmt_help() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic(code(foo::bar::baz), help("{} x {my_field} x {:?}", 1, "2"))] #[diagnostic(code(foo::bar::baz), help("{} x {my_field} x {:?}", 1, "2"))]
struct BarStruct { struct BarStruct<'a> {
my_field: String, my_field: &'a str,
} }
assert_eq!( assert_eq!(
@ -211,9 +211,9 @@ fn fmt_help() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
enum FooEnum { enum FooEnum<'a> {
#[diagnostic(code(foo::x), help("{} x {0} x {:?}", 1, "2"))] #[diagnostic(code(foo::x), help("{} x {0} x {:?}", 1, "2"))]
X(String), X(&'a str),
#[diagnostic(code(foo::x), help("{} x {len} x {:?}", 1, "2"))] #[diagnostic(code(foo::x), help("{} x {len} x {:?}", 1, "2"))]
Y { len: usize }, Y { len: usize },
@ -243,9 +243,9 @@ fn help_field() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic()] #[diagnostic()]
struct Foo { struct Foo<'a> {
#[help] #[help]
do_this: Option<String>, do_this: Option<&'a str>,
} }
assert_eq!( assert_eq!(
@ -261,11 +261,11 @@ fn help_field() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic()] #[diagnostic()]
enum Bar { enum Bar<'a> {
A(#[help] Option<String>), A(#[help] Option<&'a str>),
B { B {
#[help] #[help]
do_this: Option<String>, do_this: Option<&'a str>,
}, },
} }
@ -286,7 +286,7 @@ fn help_field() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic()] #[diagnostic()]
struct Baz(#[help] Option<String>); struct Baz<'a>(#[help] Option<&'a str>);
assert_eq!( assert_eq!(
"x".to_string(), "x".to_string(),
@ -296,7 +296,7 @@ fn help_field() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic()] #[diagnostic()]
struct Quux(#[help] String); struct Quux<'a>(#[help] &'a str);
assert_eq!( assert_eq!(
"x".to_string(), "x".to_string(),
@ -309,9 +309,9 @@ fn test_snippet_named_struct() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic(code(foo::bar::baz))] #[diagnostic(code(foo::bar::baz))]
struct Foo { struct Foo<'a> {
#[source_code] #[source_code]
src: String, src: &'a str,
#[label("var 1")] #[label("var 1")]
var1: SourceSpan, var1: SourceSpan,
#[label = "var 2"] #[label = "var 2"]
@ -331,8 +331,8 @@ fn test_snippet_unnamed_struct() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[diagnostic(code(foo::bar::baz))] #[diagnostic(code(foo::bar::baz))]
struct Foo( struct Foo<'a>(
#[source_code] String, #[source_code] &'a str,
#[label("{0}")] SourceSpan, #[label("{0}")] SourceSpan,
#[label = "idk"] SourceSpan, #[label = "idk"] SourceSpan,
#[label] SourceSpan, #[label] SourceSpan,
@ -346,11 +346,11 @@ fn test_snippet_enum() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("welp")] #[error("welp")]
#[allow(dead_code)] #[allow(dead_code)]
enum Foo { enum Foo<'a> {
#[diagnostic(code(foo::a))] #[diagnostic(code(foo::a))]
A { A {
#[source_code] #[source_code]
src: String, src: &'a str,
msg: String, msg: String,
#[label("hi this is where the thing went wrong ({msg})")] #[label("hi this is where the thing went wrong ({msg})")]
var0: SourceSpan, var0: SourceSpan,
@ -516,9 +516,9 @@ fn test_forward_struct_named() {
help("{help}"), help("{help}"),
forward(span) forward(span)
)] )]
struct Struct { struct Struct<'a> {
span: ForwardsTo, span: ForwardsTo,
help: &'static str, help: &'a str,
} }
// Also check the From impl here // Also check the From impl here
let diag = Struct { let diag = Struct {
@ -535,7 +535,7 @@ fn test_forward_struct_unnamed() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
#[error("display")] #[error("display")]
#[diagnostic(code(foo::bar::overridden), url("{1}"), forward(0))] #[diagnostic(code(foo::bar::overridden), url("{1}"), forward(0))]
struct Struct(ForwardsTo, &'static str); struct Struct<'a>(ForwardsTo, &'a str);
// Also check the From impl here // Also check the From impl here
let diag = Struct(ForwardsTo::new(), "url here"); let diag = Struct(ForwardsTo::new(), "url here");
@ -546,12 +546,12 @@ fn test_forward_struct_unnamed() {
#[test] #[test]
fn test_forward_enum_named() { fn test_forward_enum_named() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
enum Enum { enum Enum<'a> {
#[error("help: {help_text}")] #[error("help: {help_text}")]
#[diagnostic(code(foo::bar::overridden), help("{help_text}"), forward(span))] #[diagnostic(code(foo::bar::overridden), help("{help_text}"), forward(span))]
Variant { Variant {
span: ForwardsTo, span: ForwardsTo,
help_text: &'static str, help_text: &'a str,
}, },
} }
// Also check the From impl here // Also check the From impl here
@ -569,10 +569,10 @@ fn test_forward_enum_named() {
#[test] #[test]
fn test_forward_enum_unnamed() { fn test_forward_enum_unnamed() {
#[derive(Debug, Diagnostic, Error)] #[derive(Debug, Diagnostic, Error)]
enum ForwardEnumUnnamed { enum ForwardEnumUnnamed<'a> {
#[error("help: {1}")] #[error("help: {1}")]
#[diagnostic(code(foo::bar::overridden), help("{1}"), forward(0))] #[diagnostic(code(foo::bar::overridden), help("{1}"), forward(0))]
Variant(ForwardsTo, &'static str), Variant(ForwardsTo, &'a str),
} }
// Also check the From impl here // Also check the From impl here
let variant = ForwardEnumUnnamed::Variant(ForwardsTo::new(), "overridden help please"); let variant = ForwardEnumUnnamed::Variant(ForwardsTo::new(), "overridden help please");