Skip to content

feat: the no-whitespace linter#14379

Closed
adomani wants to merge 37 commits intomasterfrom
adomani/nowhitespacel
Closed

feat: the no-whitespace linter#14379
adomani wants to merge 37 commits intomasterfrom
adomani/nowhitespacel

Conversation

@adomani
Copy link
Copy Markdown
Contributor

@adomani adomani commented Jul 3, 2024


Open in Gitpod

@adomani adomani added the WIP Work in progress label Jul 3, 2024
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 3, 2024

PR summary 931b42b075

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ getBinders
+ getLinterHash
+ getStartPos
+ inappropriateSpacing
+ modNameToFilePath
+ noInitialWhitespaceLinter

You can run this locally as follows
## summary with just the declaration names:
./scripts/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/declarations_diff.sh long <optional_commit>

The doc-module for script/declarations_diff.sh contains some details about this script.

mathlib-bors bot pushed a commit that referenced this pull request Jul 3, 2024
The fallout of an experiment (#14379) with linting whitespace.

To keep the review interesting, I invite you to find the two changes that are not whitespace removal.
@ghost ghost added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 11, 2024
@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented Jul 11, 2024

@grunweg, this is a prototype for the "no whitespace linter"! I won't have time now to fix the spaces around explicit/implicit binders, but if you want to have a look at it, feel free to do so!

If you do and you can let me know the false positives, I'd be very grateful!

E.g., I'm already aware that the linter does not do the right thing with binders spanning more than one line, but I don't know what other issues it might have.

@ghost ghost removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 11, 2024
@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Jul 11, 2024

Thanks for asking! I took a look at the linter errors - the number is quite manageable (perhaps 50 warnings overall).

  • A significant proportion are false positives.
  • A number of them indicate real problems. After fixing these, usually some error persists (which is then off).

Here are some things I noticed, with some code examples (you can search the diff to find the relevant place) and what the linter prints for me.
0. Small issue, but: the suggestion always uses variable, even if the affected binder is in a theorem statement.

  1. Often, the error message is off by two.
    Example: variable {X : Type u} prints j {U : Under ' should be printed as 'variable {U : Under X}; a similar issue occurred with variable {U : Under C}. (My hypothesis: the right hand side is multiple parts and not all of it is captured somehow?)

  2. Another example, perhaps related: I get the linter output 'ble {D : Type u' should be printed as 'variable {D : Type u₂}' for the code variable {D : Type u₂} [Category.{v₂} D].

  3. I wonder: is this one about nested binders?

def toOver (F : S ⥤ T) (X : T) (f : (Y : S) → F.obj Y ⟶ X)
    (h : ∀ {Y Z : S} (g : Y ⟶ Z), F.map g ≫ f Z = f Y) : S ⥤ Over X :=

prints '∀ {Y Z ' should be printed as 'variable {Y Z : S}'.

  1. Is this one about composite notation? The linter prints '{ f : R →+* T' should be printed as 'variable {f : R →+* T}' on the code
irreducible_def preLift {r : R → R → Prop} {f : R →+* T} (h : ∀ ⦃x y⦄, r x y → f x = f y) :
  1. Another interesting example, about the behaviour after correcting the error. Is this about incrementality, or some caching issue in the linter? The original code is variable [T2Space M] {α β γ : Type*}, the linter (rightly) prints '{α β γ : Type*}' should be printed as 'variable {α β γ : Type*}'.
    After correction and restarting the file, there is still an error, now saying '{α β γ : Type*' should be printed as 'variable {α β γ : Type*}'. That error is bogus, but looks like it's still using the old syntax (note the double space in the old message).

  2. Perhaps this is the same as issue 5? In one example, correcting an issue makes the linter flag the next binders.
    Initial code was theorem countable_meas_pos_of_disjoint_iUnion₀ {ι : Type*} { _ : MeasurableSpace α} {μ : Measure α},
    the linter rightly printed '{ _ : MeasurableSpace α}' should be printed as 'variable {_ : MeasurableSpace α}'.
    Correct the error, now the code is theorem countable_meas_pos_of_disjoint_iUnion₀ {ι : Type*} {_ : MeasurableSpace α} {μ : Measure α}. Now the linter prints three errors!

  • '{ _ : MeasurableSpace α' should be printed as 'variable {_ : MeasurableSpace α}'
  • ' {μ : Measure α' should be printed as 'variable {μ : Measure α}'
  • ' {As : ι → Set α' should be printed as 'variable {As : ι → Set α}'

I've pushed the ~10 true fixes to this PR.

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Jul 11, 2024

Created #14662 as a by-product.

@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented Jul 11, 2024

Quick, incomplete reply:

  • using variable is a hack to produce valid syntax -- I'd like to not print it, but have not figured out how;
  • the linter reads the file that is being modified, so caching and recent modifications in an unsaved file produce unexpected behaviour -- I found that saving and restarting the file works more reliably;
  • one of the issues that you flagged seemed to have an extra space around a binder, so looked like a real case (maybe item 4?).

@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented Jul 11, 2024

The shift in print I think boils down to what counts as a "character": some unicode symbols create some issues and I do not really know how to get consistent behaviour.

@grunweg
Copy link
Copy Markdown
Contributor

grunweg commented Jul 11, 2024

Quick, incomplete reply:

* using variable is a hack to produce valid syntax -- I'd like to not print it, but have not figured out how;

* the linter reads the file that is being modified, so caching and recent modifications in an unsaved file produce unexpected behaviour -- I found that saving and restarting the file works more reliably;

* one of the issues that you flagged seemed to have an extra space around a binder, so looked like a real case (maybe item 4?).

Ah, that explains. I wasn't saving the files - usually, that works (until it doesn't).

@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented Jul 12, 2024

Michael, thank you so much for the complete report! Here is a more detailed answer.

  1. I'd like to not print variable, but the binder alone is not valid syntax, so this is a hack to pretty-print something.

1+2. I think that these are mostly issues with unsaved changes in a file and using a cached version of the text.

3+4+5+6. These again seem to be caching-related.

Conclusions.

  1. In its current form, the linter can only be reliably used in CI, since it gives often non-up-to-date information on modified files.
  2. I did not find many false positives: in fact, after allowing a line break after a colon (:), I am hoping that all the warnings are correct (CI is still running).

mathlib-bors bot pushed a commit that referenced this pull request Jul 12, 2024
These were found by the linter at #14379.

Co-authored-by: Michael Rothgang <[rothgami@math.hu-berlin.de](mailto:rothgami@math.hu-berlin.de)>
@ghost ghost added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Jul 19, 2024
@ghost ghost removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Aug 1, 2024
@ghost ghost added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Aug 5, 2024
@grunweg grunweg added the t-linter Linter label Aug 15, 2024
@adomani
Copy link
Copy Markdown
Contributor Author

adomani commented Nov 16, 2024

This evolved into the ppRoundtrip linter.

@adomani adomani closed this Nov 16, 2024
@YaelDillies YaelDillies deleted the adomani/nowhitespacel branch August 15, 2025 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) t-linter Linter WIP Work in progress

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants