Skip to content

Commit

Permalink
Use the new importable_path edge to clean up declarative macro lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi committed Dec 15, 2024
1 parent 1891543 commit 96a8da5
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 23 deletions.
30 changes: 25 additions & 5 deletions src/lints/declarative_macro_missing.ron
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,40 @@ SemverQuery(
item {
... on Macro {
visibility_limit @filter(op: "=", value: ["$public"])
public_api_eligible @filter(op: "=", value: ["$true"])
name @output @tag
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
span_: span @optional {
filename @output
begin_line @output
}
}
}
}
current @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
item {
current {
# There is no exported macro at that path anymore (hidden or not).
item @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
... on Macro {
importable_path {
path @filter(op: "=", value: ["%path"])
}
}
}
# There is also no macro under the same name that is *not* exported,
# nor exportable (in a way that fixes the breakage)
# just by adding `#[macro_export]`.
# This is to differentiate from the `macro_no_longer_exported` and
# `macro_now_doc_hidden` lints.
item @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
... on Macro {
name @filter(op: "=", value: ["%name"])
public_api_eligible @filter(op: "!=", value: ["$true"])
doc_hidden @filter(op: "!=", value: ["$true"])
}
}
}
Expand All @@ -36,8 +56,8 @@ SemverQuery(
"zero": 0,
"true": true,
},
error_message: "A `macro_rules` declarative macro cannot be invoked by its prior name. The macro may have been renamed or removed entirely.",
per_result_error_template: Some("macro_rules! {{name}}, previously in file {{span_filename}}:{{span_begin_line}}"),
error_message: "A `macro_rules!` declarative macro cannot be invoked by its prior name. The macro may have been renamed or removed entirely.",
per_result_error_template: Some("macro {{name}}, previously in file {{span_filename}}:{{span_begin_line}}"),
witness: (
hint_template: r#"{{name}}!(...);"#
),
Expand Down
13 changes: 10 additions & 3 deletions src/lints/macro_no_longer_exported.ron
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ SemverQuery(
item {
... on Macro {
name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
span_: span @optional {
filename @output
Expand All @@ -26,7 +30,10 @@ SemverQuery(
item @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
... on Macro {
name @filter(op: "=", value: ["%name"])
public_api_eligible @filter(op: "=", value: ["$true"])
importable_path {
path @filter(op: "=", value: ["%path"])
}
}
}
Expand Down Expand Up @@ -54,7 +61,7 @@ SemverQuery(
"true": true,
"zero": 0,
},
error_message: "A macro that was previously exported with #[macro_export] is no longer exported. This breaks downstream code that used the macro.",
error_message: "A `macro_rules!` declarative macro that was previously exported with `#[macro_export]` is no longer exported. This breaks downstream code that used the macro.",
per_result_error_template: Some("macro {{name}} in {{span_filename}}:{{span_begin_line}}"),
witness: Some((
hint_template: r#"{{name}}!(...);"#,
Expand Down
16 changes: 12 additions & 4 deletions src/lints/macro_now_doc_hidden.ron
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ SemverQuery(
item {
... on Macro {
name @output @tag
public_api_eligible @filter(op: "=", value: ["$true"])
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
}
}
}
current {
item {
... on Macro {
name @filter(op: "=", value: ["%name"])
doc_hidden @filter(op: "=", value: ["$true"])
public_api_eligible @filter(op: "!=", value: ["$true"])
importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "!=", value: ["$true"])
}
span_: span @optional {
filename @output
begin_line @output
Expand All @@ -34,7 +42,7 @@ SemverQuery(
arguments: {
"true": true,
},
error_message: "A macro that was previously part of the public API is now #[doc(hidden)].",
error_message: "A `macro_rules!` declarative macro that was previously part of the public API is now `#[doc(hidden)]` and not public API anymore.",
per_result_error_template: Some("macro {{name}} in {{span_filename}}:{{span_begin_line}}"),
witness: None,
)
4 changes: 2 additions & 2 deletions test_crates/macro_no_longer_exported/new/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ macro_rules! example_macro {

// No longer exported but is hidden - should NOT trigger (caught by different lint)
#[doc(hidden)]
macro_rules! will_be_hidden {
macro_rules! will_be_hidden_and_not_exported {
() => {
println!("Will become hidden");
println!("Will become hidden and not exported");
};
}

Expand Down
4 changes: 2 additions & 2 deletions test_crates/macro_no_longer_exported/old/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ macro_rules! example_macro {
}

#[macro_export]
macro_rules! will_be_hidden {
macro_rules! will_be_hidden_and_not_exported {
() => {
println!("Will become hidden");
println!("Will become hidden and not exported");
};
}

Expand Down
15 changes: 15 additions & 0 deletions test_outputs/query_execution/declarative_macro_missing.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@ snapshot_kind: text
"./test_crates/declarative_macro_missing/": [
{
"name": String("will_be_removed"),
"path": List([
String("declarative_macro_missing"),
String("will_be_removed"),
]),
"span_begin_line": Uint64(2),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_no_longer_exported/": [
{
"name": String("will_be_hidden_and_not_exported"),
"path": List([
String("macro_no_longer_exported"),
String("will_be_hidden_and_not_exported"),
]),
"span_begin_line": Uint64(9),
"span_filename": String("src/lib.rs"),
},
],
}
16 changes: 16 additions & 0 deletions test_outputs/query_execution/macro_no_longer_exported.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ snapshot_kind: text
"non_exported_span_filename": List([
String("src/lib.rs"),
]),
"path": List([
String("declarative_macro_missing"),
String("will_no_longer_be_exported"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/lib.rs"),
},
Expand All @@ -26,6 +30,10 @@ snapshot_kind: text
"non_exported_span_filename": List([
String("src/lib.rs"),
]),
"path": List([
String("macro_no_longer_exported"),
String("example_macro"),
]),
"span_begin_line": Uint64(2),
"span_filename": String("src/lib.rs"),
},
Expand All @@ -39,6 +47,10 @@ snapshot_kind: text
String("src/lib.rs"),
String("src/lib.rs"),
]),
"path": List([
String("macro_no_longer_exported"),
String("some_macro"),
]),
"span_begin_line": Uint64(26),
"span_filename": String("src/lib.rs"),
},
Expand All @@ -52,6 +64,10 @@ snapshot_kind: text
"non_exported_span_filename": List([
String("src/lib.rs"),
]),
"path": List([
String("macro_now_doc_hidden"),
String("becomes_non_exported"),
]),
"span_begin_line": Uint64(36),
"span_filename": String("src/lib.rs"),
},
Expand Down
15 changes: 8 additions & 7 deletions test_outputs/query_execution/macro_now_doc_hidden.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ snapshot_kind: text
"./test_crates/declarative_macro_missing/": [
{
"name": String("became_doc_hidden"),
"path": List([
String("declarative_macro_missing"),
String("became_doc_hidden"),
]),
"span_begin_line": Uint64(7),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_no_longer_exported/": [
{
"name": String("will_be_hidden"),
"span_begin_line": Uint64(10),
"span_filename": String("src/lib.rs"),
},
],
"./test_crates/macro_now_doc_hidden/": [
{
"name": String("will_be_hidden"),
"path": List([
String("macro_now_doc_hidden"),
String("will_be_hidden"),
]),
"span_begin_line": Uint64(4),
"span_filename": String("src/lib.rs"),
},
Expand Down
5 changes: 5 additions & 0 deletions test_outputs/witnesses/declarative_macro_missing.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ snapshot_kind: text
filename = 'src/lib.rs'
begin_line = 2
hint = 'will_be_removed!(...);'

[["./test_crates/macro_no_longer_exported/"]]
filename = 'src/lib.rs'
begin_line = 9
hint = 'will_be_hidden_and_not_exported!(...);'

0 comments on commit 96a8da5

Please sign in to comment.