Skip to content

Commit

Permalink
Reinstate test to confirm behaviour of clashy fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-incubiq committed Nov 27, 2024
1 parent 704f315 commit 99aeadd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion tracing-attributes/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn gen_block<B: ToTokens>(
if let Some(ref fields) = args.fields {
fields.0.iter().all(|Field { ref name, .. }| {
match name {
// TODO: implement this variant when the compiler supports const evaluation
// TODO(#3158): implement this variant when the compiler supports const evaluation
// (https://rustc-dev-guide.rust-lang.org/const-eval)
FieldName::Expr(_) => true,
FieldName::Punctuated(punctuated) => {
Expand Down
50 changes: 25 additions & 25 deletions tracing-attributes/tests/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,31 @@ fn fn_keyword_ident_in_field(_arg: &str) {}

const CONST_FIELD_NAME: &str = "foo.bar";

#[instrument(fields({CONST_FIELD_NAME} = "quux"))]
#[instrument(fields({CONST_FIELD_NAME} = "baz"))]
fn fn_const_field_name() {}

const fn get_const_fn_field_name() -> &'static str {
"foo.bar"
}

#[instrument(fields({get_const_fn_field_name()} = "quux"))]
#[instrument(fields({get_const_fn_field_name()} = "baz"))]
fn fn_const_fn_field_name() {}

struct FieldNames {}
impl FieldNames {
const FOO_BAR: &'static str = "foo.bar";
}

#[instrument(fields({FieldNames::FOO_BAR} = "quux"))]
#[instrument(fields({FieldNames::FOO_BAR} = "baz"))]
fn fn_struct_const_field_name() {}

#[instrument(fields({"foo"} = "bar"))]
fn fn_string_field_name() {}

// TODO: uncomment when the compiler supports const evaluation
// (https://rustc-dev-guide.rust-lang.org/const-eval)
// const CLASHY_FIELD_NAME: &str = "s";
//
// #[instrument(fields({CONST_FIELD_NAME} = "s"))]
// fn fn_clashy_const_field_name(s: &str) { let _ = s; }
const CLASHY_FIELD_NAME: &str = "s";

#[instrument(fields({CLASHY_FIELD_NAME} = "foo"))]
fn fn_clashy_const_field_name(s: &str) { let _ = s; }

#[derive(Debug)]
struct HasField {
Expand Down Expand Up @@ -193,7 +191,7 @@ fn keyword_ident_in_field_name() {
fn expr_const_field_name() {
let span = expect::span().with_fields(
expect::field("foo.bar")
.with_value(&"quux")
.with_value(&"baz")
.only(),
);
run_test(span, || {
Expand All @@ -205,7 +203,7 @@ fn expr_const_field_name() {
fn expr_const_fn_field_name() {
let span = expect::span().with_fields(
expect::field("foo.bar")
.with_value(&"quux")
.with_value(&"baz")
.only(),
);
run_test(span, || {
Expand All @@ -217,7 +215,7 @@ fn expr_const_fn_field_name() {
fn struct_const_field_name() {
let span = expect::span().with_fields(
expect::field("foo.bar")
.with_value(&"quux")
.with_value(&"baz")
.only(),
);
run_test(span, || {
Expand All @@ -237,19 +235,21 @@ fn string_field_name() {
});
}

// TODO: uncomment when the compiler supports const evaluation
// (https://rustc-dev-guide.rust-lang.org/const-eval)
// #[test]
// fn clashy_const_field_name() {
// let span = expect::span().with_fields(
// expect::field("s")
// .with_value(&"s")
// .only(),
// );
// run_test(span, || {
// fn_clashy_const_field_name("hello world");
// });
// }
#[test]
fn clashy_const_field_name() {
let span = expect::span().with_fields(
// TODO(#3158): to be consistent with event! and span! macros, the argument's value should
// be dropped. but this is only possible to implement when the compiler supports const
// evaluation (https://rustc-dev-guide.rust-lang.org/const-eval).
expect::field("s")
.with_value(&"foo")
.and(expect::field("s").with_value(&"hello world"))
.only(),
);
run_test(span, || {
fn_clashy_const_field_name("hello world");
});
}

fn run_test<F: FnOnce() -> T, T>(span: NewSpan, fun: F) {
let (collector, handle) = collector::mock()
Expand Down

0 comments on commit 99aeadd

Please sign in to comment.