Skip to content

Commit c88f0b5

Browse files
authoredNov 21, 2022
fix(derive): elide lifetimes in derived functions (#226)
1 parent c857595 commit c88f0b5

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed
 

‎miette-derive/src/code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Code {
7272
pub(crate) fn gen_struct(&self) -> Option<TokenStream> {
7373
let code = &self.0;
7474
Some(quote! {
75-
fn code<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> {
75+
fn code(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
7676
std::option::Option::Some(std::boxed::Box::new(#code))
7777
}
7878
})

‎miette-derive/src/forward.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ impl WhichFn {
5858
pub fn signature(&self) -> TokenStream {
5959
match self {
6060
Self::Code => quote! {
61-
fn code<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>>
61+
fn code(& self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>>
6262
},
6363
Self::Help => quote! {
64-
fn help<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>>
64+
fn help(& self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>>
6565
},
6666
Self::Url => quote! {
67-
fn url<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>>
67+
fn url(& self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>>
6868
},
6969
Self::Severity => quote! {
7070
fn severity(&self) -> std::option::Option<miette::Severity>

‎miette-derive/src/help.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl Help {
108108
Some(quote! {
109109
Self::#ident #display_pat => {
110110
use miette::macro_helpers::ToOption;
111-
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)) })
111+
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)) })
112112
},
113113
})
114114
}
@@ -123,7 +123,7 @@ impl Help {
123123
Help::Display(display) => {
124124
let (fmt, args) = display.expand_shorthand_cloned(&display_members);
125125
Some(quote! {
126-
fn help<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> {
126+
fn help(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
127127
#[allow(unused_variables, deprecated)]
128128
let Self #display_pat = self;
129129
std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args)))
@@ -133,11 +133,11 @@ impl Help {
133133
Help::Field(member, ty) => {
134134
let var = quote! { __miette_internal_var };
135135
Some(quote! {
136-
fn help<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> {
136+
fn help(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
137137
#[allow(unused_variables, deprecated)]
138138
let Self #display_pat = self;
139139
use miette::macro_helpers::ToOption;
140-
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)) })
140+
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)) })
141141
}
142142
})
143143
}

‎miette-derive/src/url.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Url {
129129
}
130130
};
131131
Some(quote! {
132-
fn url<'a>(&'a self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + 'a>> {
132+
fn url(&self) -> std::option::Option<std::boxed::Box<dyn std::fmt::Display + '_>> {
133133
#[allow(unused_variables, deprecated)]
134134
let Self #pat = self;
135135
std::option::Option::Some(std::boxed::Box::new(format!(#fmt #args)))

‎tests/derive.rs

+25-25
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ fn fmt_help() {
185185
#[derive(Debug, Diagnostic, Error)]
186186
#[error("welp")]
187187
#[diagnostic(code(foo::bar::baz), help("{} x {0} x {:?}", 1, "2"))]
188-
struct FooStruct(String);
188+
struct FooStruct<'a>(&'a str);
189189

190190
assert_eq!(
191191
"1 x hello x \"2\"".to_string(),
@@ -195,8 +195,8 @@ fn fmt_help() {
195195
#[derive(Debug, Diagnostic, Error)]
196196
#[error("welp")]
197197
#[diagnostic(code(foo::bar::baz), help("{} x {my_field} x {:?}", 1, "2"))]
198-
struct BarStruct {
199-
my_field: String,
198+
struct BarStruct<'a> {
199+
my_field: &'a str,
200200
}
201201

202202
assert_eq!(
@@ -211,9 +211,9 @@ fn fmt_help() {
211211

212212
#[derive(Debug, Diagnostic, Error)]
213213
#[error("welp")]
214-
enum FooEnum {
214+
enum FooEnum<'a> {
215215
#[diagnostic(code(foo::x), help("{} x {0} x {:?}", 1, "2"))]
216-
X(String),
216+
X(&'a str),
217217

218218
#[diagnostic(code(foo::x), help("{} x {len} x {:?}", 1, "2"))]
219219
Y { len: usize },
@@ -243,9 +243,9 @@ fn help_field() {
243243
#[derive(Debug, Diagnostic, Error)]
244244
#[error("welp")]
245245
#[diagnostic()]
246-
struct Foo {
246+
struct Foo<'a> {
247247
#[help]
248-
do_this: Option<String>,
248+
do_this: Option<&'a str>,
249249
}
250250

251251
assert_eq!(
@@ -261,11 +261,11 @@ fn help_field() {
261261
#[derive(Debug, Diagnostic, Error)]
262262
#[error("welp")]
263263
#[diagnostic()]
264-
enum Bar {
265-
A(#[help] Option<String>),
264+
enum Bar<'a> {
265+
A(#[help] Option<&'a str>),
266266
B {
267267
#[help]
268-
do_this: Option<String>,
268+
do_this: Option<&'a str>,
269269
},
270270
}
271271

@@ -286,7 +286,7 @@ fn help_field() {
286286
#[derive(Debug, Diagnostic, Error)]
287287
#[error("welp")]
288288
#[diagnostic()]
289-
struct Baz(#[help] Option<String>);
289+
struct Baz<'a>(#[help] Option<&'a str>);
290290

291291
assert_eq!(
292292
"x".to_string(),
@@ -296,7 +296,7 @@ fn help_field() {
296296
#[derive(Debug, Diagnostic, Error)]
297297
#[error("welp")]
298298
#[diagnostic()]
299-
struct Quux(#[help] String);
299+
struct Quux<'a>(#[help] &'a str);
300300

301301
assert_eq!(
302302
"x".to_string(),
@@ -309,9 +309,9 @@ fn test_snippet_named_struct() {
309309
#[derive(Debug, Diagnostic, Error)]
310310
#[error("welp")]
311311
#[diagnostic(code(foo::bar::baz))]
312-
struct Foo {
312+
struct Foo<'a> {
313313
#[source_code]
314-
src: String,
314+
src: &'a str,
315315
#[label("var 1")]
316316
var1: SourceSpan,
317317
#[label = "var 2"]
@@ -331,8 +331,8 @@ fn test_snippet_unnamed_struct() {
331331
#[derive(Debug, Diagnostic, Error)]
332332
#[error("welp")]
333333
#[diagnostic(code(foo::bar::baz))]
334-
struct Foo(
335-
#[source_code] String,
334+
struct Foo<'a>(
335+
#[source_code] &'a str,
336336
#[label("{0}")] SourceSpan,
337337
#[label = "idk"] SourceSpan,
338338
#[label] SourceSpan,
@@ -346,11 +346,11 @@ fn test_snippet_enum() {
346346
#[derive(Debug, Diagnostic, Error)]
347347
#[error("welp")]
348348
#[allow(dead_code)]
349-
enum Foo {
349+
enum Foo<'a> {
350350
#[diagnostic(code(foo::a))]
351351
A {
352352
#[source_code]
353-
src: String,
353+
src: &'a str,
354354
msg: String,
355355
#[label("hi this is where the thing went wrong ({msg})")]
356356
var0: SourceSpan,
@@ -516,9 +516,9 @@ fn test_forward_struct_named() {
516516
help("{help}"),
517517
forward(span)
518518
)]
519-
struct Struct {
519+
struct Struct<'a> {
520520
span: ForwardsTo,
521-
help: &'static str,
521+
help: &'a str,
522522
}
523523
// Also check the From impl here
524524
let diag = Struct {
@@ -535,7 +535,7 @@ fn test_forward_struct_unnamed() {
535535
#[derive(Debug, Diagnostic, Error)]
536536
#[error("display")]
537537
#[diagnostic(code(foo::bar::overridden), url("{1}"), forward(0))]
538-
struct Struct(ForwardsTo, &'static str);
538+
struct Struct<'a>(ForwardsTo, &'a str);
539539

540540
// Also check the From impl here
541541
let diag = Struct(ForwardsTo::new(), "url here");
@@ -546,12 +546,12 @@ fn test_forward_struct_unnamed() {
546546
#[test]
547547
fn test_forward_enum_named() {
548548
#[derive(Debug, Diagnostic, Error)]
549-
enum Enum {
549+
enum Enum<'a> {
550550
#[error("help: {help_text}")]
551551
#[diagnostic(code(foo::bar::overridden), help("{help_text}"), forward(span))]
552552
Variant {
553553
span: ForwardsTo,
554-
help_text: &'static str,
554+
help_text: &'a str,
555555
},
556556
}
557557
// Also check the From impl here
@@ -569,10 +569,10 @@ fn test_forward_enum_named() {
569569
#[test]
570570
fn test_forward_enum_unnamed() {
571571
#[derive(Debug, Diagnostic, Error)]
572-
enum ForwardEnumUnnamed {
572+
enum ForwardEnumUnnamed<'a> {
573573
#[error("help: {1}")]
574574
#[diagnostic(code(foo::bar::overridden), help("{1}"), forward(0))]
575-
Variant(ForwardsTo, &'static str),
575+
Variant(ForwardsTo, &'a str),
576576
}
577577
// Also check the From impl here
578578
let variant = ForwardEnumUnnamed::Variant(ForwardsTo::new(), "overridden help please");

0 commit comments

Comments
 (0)
Please sign in to comment.