SemverQuery(
id: "constructible_struct_adds_field",
human_readable_name: "struct exhaustively constructible through public API adds field",
description: "A struct exhaustively constructible with a literal using only public API added a new pub field.",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/reference/expressions/struct-expr.html"),
query: r#"
{
CrateDiff {
current {
item {
... on Struct {
visibility_limit @filter(op: "=", value: ["$public"])
# If the struct is now marked `#[non_exhaustive]`, that's
# a separate breaking change that has its own rule.
# Don't also report new field additions, since the programmer has
# clearly stated that they don't consider the struct exhaustive anymore.
attrs @filter(op: "not_contains", value: ["$non_exhaustive"])
struct_name: name @output
struct_type @output @tag
importable_path {
path @output @tag
public_api @filter(op: "=", value: ["$true"])
}
field {
field_name: name @output @tag
visibility_limit @filter(op: "=", value: ["$public"])
span_: span @optional {
filename @output
begin_line @output
end_line @output
}
}
}
}
}
baseline {
item {
# The struct needs to previously have been exhaustively constructible
# with a struct literal using only public API.
# All of the following need to be true for that to have been the case:
# 1. It needs to not have been marked `#[non_exhaustive]`.
# 2. It needs to not have had any non-public fields.
# 3. All of its fields need to have been part of public API.
... on Struct {
visibility_limit @filter(op: "=", value: ["$public"])
struct_type @filter(op: "=", value: ["%struct_type"])
# 1. It needs to not have been marked `#[non_exhaustive]`.
attrs @filter(op: "not_contains", value: ["$non_exhaustive"])
importable_path {
path @filter(op: "=", value: ["%path"])
public_api @filter(op: "=", value: ["$true"])
}
# 2. It needs to not have had any non-public fields.
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
visibility_limit @filter(op: "!=", value: ["$public"])
}
# 3. All of its fields need to have been part of public API.
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
public_api_eligible @filter(op: "!=", value: ["$true"])
}
# The new field did not previously exist.
#
# Do not require the new field to be part of public API.
# Adding a pub field makes existing exhaustive literals not work
# even if the field is `#[doc(hidden)]` or unstable.
field @fold @transform(op: "count") @filter(op: "=", value: ["$zero"]) {
name @filter(op: "=", value: ["%field_name"])
visibility_limit @filter(op: "=", value: ["$public"])
}
}
}
}
}
}"#,
arguments: {
"public": "public",
"non_exhaustive": "#[non_exhaustive]",
"true": true,
"zero": 0,
},
error_message: "A pub struct that could be exhaustively constructed with a literal using only public API has a new pub field, breaking existing exhaustive literals.",
per_result_error_template: Some("field {{struct_name}}.{{field_name}} in {{span_filename}}:{{span_begin_line}}"),
)