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

[superseded] simplify code that handles hints/warnings #10063

Closed

Conversation

timotheecour
Copy link
Member

@timotheecour timotheecour commented Dec 21, 2018

  • WarningsToStr, HintsToStr hardcoded tables wasn't DRY; I removed it in favor of proc msgToHumanStr*(a: TMsgKind): string. This simplifies a bunch of code related to hints/warnings
  • added hintLinkingVerbose to always show link command; happy to make that the new meaning for hintLink if we don't want to create a new hint for that (the existing hintLink isn't very useful as it doesn't show anything except "Link")

note

I could also use an inverted index table, eg:

const humanStrToMsg* = block: ## Inverted index of `proc humanStrToMsg`
  var ret = initTable[string, TMsgKind]()
  for a in TNoteKind:
    let str = a.msgToHumanStr.normalizeStyle
    ret[str] = a
  ret

proc normalizeStyle(a: string): string=
  # normalizes: _Foo_Bar => Foobar so that `cmpIgnoreStyle` doesn't need to be used

however, humanStrToMsg is only used in non performance critical places so I went for simplicity instead (with same time complexity as before PR)

@timotheecour timotheecour force-pushed the pr_simplify_hint_warn branch 2 times, most recently from 28f668b to bf1c539 Compare December 21, 2018 07:55
@krux02
Copy link
Contributor

krux02 commented Dec 21, 2018

Hmm, I am not convinced that this is any better or worse. It is just different.

@timotheecour
Copy link
Member Author

timotheecour commented Dec 21, 2018

I don't think it's good style to have to write stuff like this:

const	
   WarningsToStr* = ["CannotOpenFile", "OctalEscape",	
     "XIsNeverRead", "XmightNotBeenInit",	
     "Deprecated", "ConfigDeprecated",	
     "SmallLshouldNotBeUsed", "UnknownMagic",	
     "RedefinitionOfLabel", "UnknownSubstitutionX",	
     "LanguageXNotSupported", "FieldXNotSupported",	
     "CommentXIgnored",	
     "TypelessParam", "UseBase", "WriteToForeignHeap",	
     "UnsafeCode", "EachIdentIsTuple", "ShadowIdent",	
     "ProveInit", "ProveField", "ProveIndex", "GcUnsafe", "GcUnsafe2", "Uninit",	
     "GcMem", "Destructor", "LockLevel", "ResultShadowed",	
     "Spacing", "User"]	
 
    HintsToStr* = [	
     "Success", "SuccessX", "CC", "LineTooLong",	
     "XDeclaredButNotUsed", "ConvToBaseNotNeeded", "ConvFromXtoItselfNotNeeded",	
     "ExprAlwaysX", "QuitCalled", "Processing", "CodeBegin", "CodeEnd", "Conf",	
     "Path", "CondTrue", "CondFalse", "Name", "Pattern", "Exec", "Link", "Dependency",	
     "Source", "Performance", "StackTrace", "GCStats", "GlobalVar",	
     "User", "UserRaw", "ExtendedContext",	
   ]

which is completely redundant / DRY violation of the corresponding enum members; my PR gets rid of it and computes these automatically (and simplifies code that gets human string from enum)

@Araq
Copy link
Member

Araq commented Dec 21, 2018

which is completely redundant / DRY violation of the corresponding enum members; my PR gets rid of it and computes these automatically (and simplifies code that gets human string from enum)

Regardless of whether your chances are good or bad, this is just not true. These names are exposed to the command line so any internal compiler renaming would influence Nim's command line syntax.

@timotheecour
Copy link
Member Author

timotheecour commented Dec 21, 2018

These names are exposed to the command line so any internal compiler renaming would influence Nim's command line syntax.

but there are only 4 exceptions out of 60 names (hints/warnings), which I cleanly handle in msgToHumanStr :

proc msgToHumanStr*(a: TMsgKind): string =
   case a # handle exceptions here
   of warnInconsistentSpacing: result = "Spacing"
   of hintConditionAlwaysTrue: result = "CondTrue"
   of hintConditionAlwaysFalse: result = "CondFalse"
   of hintExecuting: result = "Exec"
   of hintLinking: result = "Link"
   else: # general case
     case a
     of warnMin..warnMax: result = ($a)["warn".len .. ^1]
     of hintMin..hintMax: result = ($a)["hint".len .. ^1]
     else: doAssert false

IMO it's cleaner to handle the general case (allowing for the occasional, rare exception) rather than duplicating the enum names in WarningsToStr, HintsToStr.
The rest of code is also simplified thanks to that.

@timotheecour
Copy link
Member Author

/cc @Araq tests are green

@timotheecour
Copy link
Member Author

ping @Araq

@Araq
Copy link
Member

Araq commented Dec 27, 2018

I like the old version better and objectively your code adds new imports / dependencies that the old variant didn't have.

@Araq Araq closed this Dec 27, 2018
@timotheecour timotheecour changed the title simplify code that handles hints/warnings [TODO] simplify code that handles hints/warnings Dec 27, 2018
@timotheecour
Copy link
Member Author

timotheecour commented Jun 18, 2021

removing the TODO; this has since been more or less implemented via #15775 and hint:link + --listcmd for the 2 aspects of this PR
pr_simplify_hint_warn

@timotheecour timotheecour deleted the pr_simplify_hint_warn branch June 18, 2021 00:06
@timotheecour timotheecour changed the title [TODO] simplify code that handles hints/warnings [superseded] simplify code that handles hints/warnings Jun 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants