-
Notifications
You must be signed in to change notification settings - Fork 742
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
attributes: support destructuring in arguments #397
Merged
hawkw
merged 10 commits into
tokio-rs:master
from
couchand:201910/instrument-argument-destructuring
Oct 22, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f88b64
attributes: extract param_names extractor
couchand ab19e89
attributes: support destructured tuple arguments
couchand aad290d
attributes: support destructured ref arguments
couchand 880ad02
attributes: support destructured tuple struct arguments
couchand 60f514b
attributes: support destructured struct arguments
couchand 26afae1
attributes: add crazy destructuring test
couchand 37b10a2
attributes: move destructuring tests to their own file
couchand 49a430d
attributes: apply cargo fmt
couchand 1e0ce4c
attributes: add helpful comment to pattern pattern match
couchand b47e709
attributes: support self argument as span field
couchand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
mod support; | ||
use support::*; | ||
|
||
use tracing::subscriber::with_default; | ||
use tracing_attributes::instrument; | ||
|
||
#[test] | ||
fn destructure_tuples() { | ||
#[instrument] | ||
fn my_fn((arg1, arg2): (usize, usize)) {} | ||
|
||
let span = span::mock().named("my_fn"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
span.clone().with_field( | ||
field::mock("arg1") | ||
.with_value(&format_args!("1")) | ||
.and(field::mock("arg2").with_value(&format_args!("2"))) | ||
.only(), | ||
), | ||
) | ||
.enter(span.clone()) | ||
.exit(span.clone()) | ||
.drop_span(span) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
my_fn((1, 2)); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn destructure_nested_tuples() { | ||
#[instrument] | ||
fn my_fn(((arg1, arg2), (arg3, arg4)): ((usize, usize), (usize, usize))) {} | ||
|
||
let span = span::mock().named("my_fn"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
span.clone().with_field( | ||
field::mock("arg1") | ||
.with_value(&format_args!("1")) | ||
.and(field::mock("arg2").with_value(&format_args!("2"))) | ||
.and(field::mock("arg3").with_value(&format_args!("3"))) | ||
.and(field::mock("arg4").with_value(&format_args!("4"))) | ||
.only(), | ||
), | ||
) | ||
.enter(span.clone()) | ||
.exit(span.clone()) | ||
.drop_span(span) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
my_fn(((1, 2), (3, 4))); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn destructure_refs() { | ||
#[instrument] | ||
fn my_fn(&arg1: &usize) {} | ||
|
||
let span = span::mock().named("my_fn"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
span.clone() | ||
.with_field(field::mock("arg1").with_value(&format_args!("1")).only()), | ||
) | ||
.enter(span.clone()) | ||
.exit(span.clone()) | ||
.drop_span(span) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
my_fn(&1); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn destructure_tuple_structs() { | ||
struct Foo(usize, usize); | ||
|
||
#[instrument] | ||
fn my_fn(Foo(arg1, arg2): Foo) {} | ||
|
||
let span = span::mock().named("my_fn"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
span.clone().with_field( | ||
field::mock("arg1") | ||
.with_value(&format_args!("1")) | ||
.and(field::mock("arg2").with_value(&format_args!("2"))) | ||
.only(), | ||
), | ||
) | ||
.enter(span.clone()) | ||
.exit(span.clone()) | ||
.drop_span(span) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
my_fn(Foo(1, 2)); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn destructure_structs() { | ||
struct Foo { | ||
bar: usize, | ||
baz: usize, | ||
} | ||
|
||
#[instrument] | ||
fn my_fn( | ||
Foo { | ||
bar: arg1, | ||
baz: arg2, | ||
}: Foo, | ||
) { | ||
let _ = (arg1, arg2); | ||
} | ||
|
||
let span = span::mock().named("my_fn"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
span.clone().with_field( | ||
field::mock("arg1") | ||
.with_value(&format_args!("1")) | ||
.and(field::mock("arg2").with_value(&format_args!("2"))) | ||
.only(), | ||
), | ||
) | ||
.enter(span.clone()) | ||
.exit(span.clone()) | ||
.drop_span(span) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
my_fn(Foo { bar: 1, baz: 2 }); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} | ||
|
||
#[test] | ||
fn destructure_everything() { | ||
struct Foo { | ||
bar: Bar, | ||
baz: (usize, usize), | ||
qux: NoDebug, | ||
} | ||
struct Bar((usize, usize)); | ||
struct NoDebug; | ||
|
||
#[instrument] | ||
fn my_fn( | ||
&Foo { | ||
bar: Bar((arg1, arg2)), | ||
baz: (arg3, arg4), | ||
.. | ||
}: &Foo, | ||
) { | ||
let _ = (arg1, arg2, arg3, arg4); | ||
} | ||
|
||
let span = span::mock().named("my_fn"); | ||
|
||
let (subscriber, handle) = subscriber::mock() | ||
.new_span( | ||
span.clone().with_field( | ||
field::mock("arg1") | ||
.with_value(&format_args!("1")) | ||
.and(field::mock("arg2").with_value(&format_args!("2"))) | ||
.and(field::mock("arg3").with_value(&format_args!("3"))) | ||
.and(field::mock("arg4").with_value(&format_args!("4"))) | ||
.only(), | ||
), | ||
) | ||
.enter(span.clone()) | ||
.exit(span.clone()) | ||
.drop_span(span) | ||
.done() | ||
.run_with_handle(); | ||
|
||
with_default(subscriber, || { | ||
let foo = Foo { | ||
bar: Bar((1, 2)), | ||
baz: (3, 4), | ||
qux: NoDebug, | ||
}; | ||
let _ = foo.qux; // to eliminate unused field warning | ||
my_fn(&foo); | ||
}); | ||
|
||
handle.assert_finished(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this case matches, it's probably a syntax error, right? Can any pattern types we don't currently match be used in function params? I think it might be good to have a comment hereif that's true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about changing that to
unreachable!()
but wasn't sure if my assumptions were valid. I can't seem to find authoritative documentation on the matter, but it does seem like this covers all irrefutable patterns.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I think that if the remaining cases are something that will cause
rustc
to error anyway, it's probably best for the proc macro not to panic, so that rustc can print the correct diagnostics for the syntax error (rather than a more surprising "proc macro panicked!" message)? But, it might be good to have a comment noting that. Not a blocker though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason I was operating under the assumption that this would be a compiler error before the proc macro was invoked. There is a class of errors that will bail before trying to invoke the proc macro, but clearly it doesn't include this (I double checked just to be sure). I think the exact details would be found in RFC #550, but I really don't have the head space to digest that RFC right now (and it's also not at all useful for the purposes of this PR 😆). A comment does seem useful, if for no other reason than to keep others from going down this rabbit hole.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the reason this would error after the proc-macro is evaluated is because the macro could rewrite the function definition. Thanks for adding the comment!