Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: now linters in general do not run on #guard_msgs itself #5644

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

kmill
Copy link
Collaborator

@kmill kmill commented Oct 8, 2024

The #guard_msgs command runs the command it is attached to as if it were a top-level command. This is because the top-level command elaborator runs linters, and we are interested in capturing linter warnings using #guard_msgs. However, the linters will run on #guard_msgs itself, leading sometimes to duplicate warnings (like for the unused variable linter).

Rather than special-casing #guard_msgs in every affected linter, this PR special-cases it in the top-level command elaborator itself. Now linters are only run if the command doesn't contain #guard_msgs. This way, the linters are only run on the sub-command that #guard_msgs runs itself. This rule also keeps linters from running multiple times in cases such as set_option pp.mvars false in /-- ... -/ #guard_msgs in ....

The `#guard_msgs` command runs the command it is attached to as if it were a top-level command. This is because the top-level command elaborator runs linters, and we are interested in capturing linter warnings using `#guard_msgs`. However, the linters will run on `#guard_msgs` itself, leading sometimes to duplicate warnings (like for the unused variable linter).

Rather than special-casing `#guard_msgs` in every affected linter, this PR special-cases it in the top-level command elaborator itself. Now linters are only run if the command doesn't contain `#guard_msgs`. This way, the linters are only run on the sub-command that `#guard_msgs` runs.

It would probably be best to require that `#guard_msgs` *only* be runnable as a top-level command, since there are ways to suppress linting with the current setup.
@kmill kmill requested a review from digama0 as a code owner October 8, 2024 01:20
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Oct 8, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Oct 8, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Oct 8, 2024
@leanprover-community-bot leanprover-community-bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Oct 8, 2024
@leanprover-community-bot
Copy link
Collaborator

leanprover-community-bot commented Oct 8, 2024

Mathlib CI status (docs):

-- Run the linters, unless `#guard_msgs` is present, which is special and runs `elabCommandTopLevel` itself,
-- so it is a "super-top-level" command. This is the only command that does this, so we just special case it here
-- rather than engineer a general solution.
unless (stx.find? (·.isOfKind ``Lean.guardMsgsCmd)).isSome do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that linting for #guard_msgs or "ins that precede it" will become trickier, right?

E.g. the linters would only see section in

variable (x : Nat) in
open Lean in
#guard_msgs in
section

?

And something like

open Lean in
run_cmd
  let stx ← `(command| #guard_msgs in section)
  logInfo stx

would be completely unlinted?

Just to be explicit, I am happy with the change, just testing how far the consequences stretch! 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, this change is at odds with the #-command linter. One idea that comes to mind is that we could have a core linter against #guard_msgs itself and enable it for mathlib, but I need to think about this more.

Copy link

@adomani adomani Oct 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an issue for Mathlib: the #-command linter checks the kind of the command on the nose (no digging inside the syntax, as far as I remember). Thus #guard_msgs actually appears as an in to that linter and #guard_msgs is not linted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused about what you're saying — could you take a look at the log for the mathlib testing branch? There's an error in test/HashCommandLinter.lean where #guard_msgs is supposed to be linted. This PR makes it so that #guard_msgs never has linters run on it, and this causes that test to fail.

I'm not sure what you mean about #guard_msgs appearing as an in; the in is part of its syntax, it's not Lean.Parser.Command.in.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of my confusion was due to the fact that the error in the tests is not visible in VSCode, but only with lake test.

The other confusion is that I remembered that in shielded the #-command linter, but really it is the doc-string that usually accompanies #guard_msgs that makes the linter ignore #guard_msgs.

Hence, as is, it is hard for #guard_msgs to get caught in the #-command linter:

import Mathlib.Tactic.Linter

--flagged
#guard_msgs in
run_cmd return

-- not flagged
/---/
#guard_msgs in
run_cmd return

-- not flagged
/-- info: 0 -/
#guard_msgs in
#eval 0

This behaviour was partly by design. I would be happy to have #guard_msgs flagged more consistently as a #-command, but also to make it maintain "special status".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback @adomani, it's helpful to know that you are ok with #guard_msgs being special like this. We can revisit being able to lint for #guard_msgs in mathlib if that ever comes up.

Would you mind fixing that first mathlib HashCommandLinter.lean test as a mathlib PR? That will help with the next nightly-testing.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to fix the test in the same lean-pr-testing-5644 branch, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to fix lean-pr-testing-5644. Maybe all we need to do is fix the nightly-testing branch of mathlib directly.

Pinging @jcommelin: My understanding is that in the next nightly, we'll have to fix the first test in HashCommandLinter.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have added the fix to lean-pr-testing-5644 and I also removed the "hacky pairs" of set_option ... false #guard_msgs in set_option ... true in ....

I can cherry-pick these changes to the appropriate nightly PR, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adomani!

@grunweg
Copy link
Contributor

grunweg commented Oct 8, 2024

Note to self/whoever gets to address this first: when this lands, batteries also has such as hack which can be removed now.

@kmill kmill added this pull request to the merge queue Oct 8, 2024
Merged via the queue into leanprover:master with commit b2b450d Oct 8, 2024
18 checks passed
@leanprover-community-bot leanprover-community-bot added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds-mathlib CI has verified that Mathlib builds against this PR toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants