Autoharness: verify all formatting trait implementations - #4802
Open
srivatsansamraj wants to merge 2 commits into
Open
srivatsansamraj wants to merge 2 commits into
srivatsansamraj wants to merge 2 commits into
Conversation
…bug and Display
`fmt_impl_self_ty` recognised `Debug` and `Display` implementations, whose
`&mut Formatter` argument is supplied by a model that formats a nondeterministic
self value into a discarding sink. The other seven formatting traits went through
the general path and were skipped for that argument: 33 `fmt` methods in `core`.
Extend the mechanism to `Binary`, `Octal`, `LowerHex`, `UpperHex`, `LowerExp`,
`UpperExp` and `Pointer`. Only `Debug`, `Display` and `Pointer` carry a diagnostic
item, so the rest are matched by name inside `core::fmt`. `FmtTrait` maps each
variant to its model, and `AutomaticHarnessPass` holds one map of models instead
of a field per trait.
`Pointer` needs care: `format_args!("{value:p}")` with `value: &T` selects
`impl Pointer for &T`, which formats the reference's address and never reaches
`T`'s implementation. Its model formats through a local wrapper whose
`Pointer::fmt` calls `<T as Pointer>::fmt` directly.
The test gains a type implementing all seven, each failing on a distinct assert,
so a model reaching the wrong implementation, or none, is caught.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Raw-pointer formatting remains ineligible, and the regression test cannot detect swapped trait-to-model mappings.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
Extends autoharness verification from Debug and Display to all standard formatting traits.
Changes:
- Adds trait-specific formatting models and dispatch.
- Expands regression coverage for seven additional traits.
- Updates autoharness documentation.
File summaries
| File | Description |
|---|---|
| tests/script-based-pre/cargo_autoharness_fmt_impls/src/lib.rs | Adds formatting trait test implementations. |
| tests/script-based-pre/cargo_autoharness_fmt_impls/fmt-impls.expected | Adds expected failures and harness results. |
| library/kani_core/src/lib.rs | Adds trait-specific formatting models. |
| kani-compiler/src/kani_middle/transform/automatic.rs | Dispatches through a formatting-model map. |
| kani-compiler/src/kani_middle/mod.rs | Recognizes and maps additional formatting traits. |
| kani-compiler/src/kani_middle/kani_functions.rs | Registers the new models. |
| kani-compiler/src/kani_middle/codegen_units.rs | Generalizes formatting harness documentation. |
| docs/src/reference/experimental/autoharness.md | Documents expanded formatting support. |
Review details
Suppressed comments (3)
kani-compiler/src/kani_middle/mod.rs:1050
- The match arms should follow the same alphabetical order as the enum variants and their
KaniModelcounterparts, rather than retaining the old two variants first.
FmtTrait::Debug => KaniModel::CheckDebugFmt,
FmtTrait::Display => KaniModel::CheckDisplayFmt,
FmtTrait::Binary => KaniModel::CheckBinaryFmt,
kani-compiler/src/kani_middle/mod.rs:1035
- Keep the formatting-trait variants alphabetically ordered, consistent with the adjacent
KaniModelformatting variants inkani_functions.rs:108-125; appendingBinaryafterDisplayleaves this enum out of order.
Debug,
Display,
Binary,
docs/src/reference/experimental/autoharness.md:296
- The documented limitation now covers all nine formatting traits, but
{:#?}is only the alternate Debug syntax; the other traits use forms such as{:#b},{:#x}, and{:#p}. Please describe this as the generic alternate flag (or list the trait-specific forms) so the documentation does not imply that only Debug's alternate path is excluded.
with the trait's bare specifier (`{:?}`, `{}`, `{:x}`, ...) would produce. Code paths that a `fmt`
implementation takes only for a non-default width, precision, fill, alignment, sign, or the
alternate (`{:#?}`) flag are therefore not covered.
- Files reviewed: 8/8 changed files
- Comments generated: 2
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…ally
With all seven traits on one type, the expected output could not tell a harness apart
from the message it produced: swapping two arms of `FmtTrait::model` still yields the
same seven messages and the same seven failures. One type per trait means a model
dispatched to the wrong trait does not resolve for that type at all.
`FmtTrait` and its `model` arms now follow the alphabetical order of the `KaniModel`
variants. The docs limitation names the alternate flag generically rather than as
Debug's `{:#?}` only.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fmt_impl_self_tyrecognisesDebugandDisplayimplementations, whose&mut Formatterargument is supplied by a model that formats a nondeterministic self value into a discarding sink (#4701). The other seven formatting traits went through the general path and were skipped for that argument: 41fmtmethods on anautoharness --list --stdrun, 33 of them incore.This extends the same mechanism to
Binary,Octal,LowerHex,UpperHex,LowerExp,UpperExpandPointer. OnlyDebug,DisplayandPointercarry a diagnostic item, so the rest are matched by name insidecore::fmt.As for
DebugandDisplay, the self type must implement or deriveArbitrary. Of the 41, 20 gain a harness (NonZero,Wrapping,Saturating,Box/Rc/Arcof a primitive, and compiler_builtins' numeric types) and 21 on references and raw pointers (&i32,*const i32,NonNull,Unique,Atomic<*mut _>) stay skipped, now reported for the self type rather than theFormatter.Pointerneeds one extra step.format_args!("{value:p}")withvalue: &Tselectsimpl Pointer for &T, which formats the reference's address rather than delegating toT, so the model would never reach the implementation under test. Its model formats through a local wrapper whosePointer::fmtcalls<T as Pointer>::fmtdirectly.FmtTraitgains the seven variants and amodel()mapping;AutomaticHarnessPassholds one map of models instead of a field per trait.check_*_fmtmodels inkani_core, beside the existing two.cargo_autoharness_fmt_implsgains one type per trait, each failing on its own assert. A model dispatched to the wrong trait does not resolve for that type, so a swap fails to generate rather than passing.Part of #3832; follows #4701.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.