lean4-htt/src/Lean/Elab
David Thrane Christiansen 456ed44550
feat: add a linter for local vars that clash with their constructors (#4301)
This came up when watching new Lean users in a class situation. A number
of them were confused when they omitted a namespace on a constructor
name, and Lean treated the variable as a pattern that matches anything.

For example, this program is accepted but may not do what the user
thinks:
```
inductive Tree (α : Type) where
  | leaf
  | branch (left : Tree α) (val : α) (right : Tree α)

def depth : Tree α → Nat
  | leaf => 0
```
Adding a `branch` case to `depth` results in a confusing message.

With this linter, Lean marks `leaf` with:
```
Local variable 'leaf' resembles constructor 'Tree.leaf' - write '.leaf' (with a dot) or 'Tree.leaf' to use the constructor.
note: this linter can be disabled with `set_option linter.constructorNameAsVariable false`
```

Additionally, the error message that occurs when invalid names are
applied in patterns now suggests similar names. This means that:
```
def length (list : List α) : Nat :=
  match list with
  | nil => 0
  | cons x xs => length xs + 1
```
now results in the following warning on `nil`:
```
warning: Local variable 'nil' resembles constructor 'List.nil' - write '.nil' (with a dot) or 'List.nil' to use the constructor.
note: this linter can be disabled with `set_option linter.constructorNameAsVariable false`
```

and error on `cons`:
```
invalid pattern, constructor or constant marked with '[match_pattern]' expected

Suggestion: 'List.cons' is similar
```

The list of suggested constructors is generated before the type of the
pattern is known, so it's less accurate, but it truncates the list to
ten elements to avoid being overwhelming. This mostly comes up with
`mk`.
2024-06-14 13:03:09 +00:00
..
Deriving feat: incremental have (#4308) 2024-06-04 09:12:27 +00:00
InfoTree feat: display diagnostic information at term and tactic set_option diagnostics true (#4048) 2024-05-01 22:47:57 +00:00
PreDefinition fix: GuessLex: delaborate unused parameters as _ (#4329) 2024-06-05 07:54:29 +00:00
Quotation feat: reserved names (#3675) 2024-03-15 00:33:22 +00:00
Tactic chore: fix typo in trace.split.failure error message (#4431) 2024-06-12 05:57:29 +00:00
App.lean feat: improve error messages for numerals (#4368) 2024-06-06 00:28:42 +00:00
Arg.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Attributes.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
AutoBound.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
AuxDef.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
BinderPredicates.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Binders.lean chore: missing registerTraceClass (#4369) 2024-06-06 00:53:16 +00:00
BindersUtil.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
BuiltinCommand.lean fix: variable must execute pending tactics and elaboration problems (#4370) 2024-06-06 13:06:18 +00:00
BuiltinNotation.lean fix: auto/option params should not break sorry (#4132) 2024-05-11 02:10:40 +00:00
BuiltinTerm.lean feat: improve error messages for numerals (#4368) 2024-06-06 00:28:42 +00:00
Calc.lean fix: move cdot and calc parsers to Lean namespace 2024-05-19 07:20:10 +02:00
CheckTactic.lean chore: bool and prop lemmas for Mathlib compatibility and improved confluence (#3508) 2024-03-04 23:56:30 +00:00
Command.lean fix: non-incremental command blocking further incremental reporting in macro (#4407) 2024-06-08 16:50:15 +00:00
ComputedFields.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Config.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Declaration.lean feat: incrementality for careful command macros such as set_option in theorem, theorem foo.bar, lemma (#4364) 2024-06-05 14:10:38 +00:00
DeclarationRange.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
DeclModifiers.lean feat: reserved names (#3675) 2024-03-15 00:33:22 +00:00
DeclNameGen.lean feat: make anonymous instance names not include proofs (#3934) 2024-04-17 19:41:34 +00:00
DeclUtil.lean fix: universe parameter order discrepancy between theorem and def (#4408) 2024-06-10 23:37:52 +00:00
DefView.lean feat: show signature elaboration errors on body parse error (#4267) 2024-05-24 10:07:07 +00:00
Deriving.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Do.lean chore: bootstrap fixes 2024-05-28 23:04:19 +02:00
ElabRules.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Eval.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Exception.lean feat: incremental elaboration of definition headers, bodies, and tactics (#3940) 2024-05-22 13:23:30 +00:00
Extra.lean fix: use maxType when building expression in expression tree elaborator (#4215) 2024-05-18 20:59:54 +00:00
Frontend.lean fix: non-incremental command blocking further incremental reporting in macro (#4407) 2024-06-08 16:50:15 +00:00
GenInjective.lean feat: reserved names (#3675) 2024-03-15 00:33:22 +00:00
GuardMsgs.lean feat: show diffs when #guard_msgs fails (#3912) 2024-04-18 15:09:44 +00:00
Import.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Inductive.lean perf: use withSynthesize when elaborating let/have type (#4096) 2024-05-09 00:58:43 +00:00
InfoTree.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
InheritDoc.lean feat: reserved names (#3675) 2024-03-15 00:33:22 +00:00
LetRec.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Level.lean fix: mutual inductives with instance parameters (#4342) 2024-06-04 17:35:41 +00:00
Macro.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
MacroArgUtil.lean feat: quotations for parser aliases (#4307) 2024-05-30 09:22:22 +00:00
MacroRules.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Match.lean fix: regression on match expressions with builtin literals (#3521) 2024-02-27 18:49:44 +00:00
MatchAltView.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
MatchExpr.lean perf: match_expr join points (#3580) 2024-03-03 18:15:49 +00:00
Mixfix.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
MutualDef.lean fix: incremental reuse leading to goals in front of the text cursor being shown (#4395) 2024-06-08 15:08:14 +00:00
Notation.lean feat: apply app unexpanders for all prefixes of an application (#3375) 2024-02-27 07:04:17 +00:00
Open.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
ParseImportsFast.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
PatternVar.lean feat: add a linter for local vars that clash with their constructors (#4301) 2024-06-14 13:03:09 +00:00
PreDefinition.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
Print.lean feat: order the output of #print axioms (#4416) 2024-06-10 09:17:05 +00:00
Quotation.lean chore: missing registerTraceClass (#4369) 2024-06-06 00:53:16 +00:00
RecAppSyntax.lean perf: add prelude to all Lean modules 2024-02-18 14:55:17 -08:00
SetOption.lean fix: don't use info nodes before cursor for completion (#3778) 2024-04-02 08:49:24 +00:00
StructInst.lean chore: missing registerTraceClass (#4369) 2024-06-06 00:53:16 +00:00
Structure.lean fix: proposition fields must be theorems (#4006) 2024-04-28 01:59:47 +00:00
Syntax.lean chore: missing registerTraceClass (#4369) 2024-06-06 00:53:16 +00:00
SyntheticMVars.lean fix: tactics in terms in tactics may break incremental reporting (#4436) 2024-06-12 14:59:24 +00:00
Tactic.lean feat: upstream rw? tactic (#3719) 2024-03-23 05:01:35 +00:00
Term.lean fix: universe parameter order discrepancy between theorem and def (#4408) 2024-06-10 23:37:52 +00:00
Util.lean feat: simp to still work even if one simp arg does not work (#4177) 2024-06-03 14:21:31 +00:00