summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-01-20 01:49:59 -0800
committerGitHub <noreply@github.com>2021-01-20 10:49:59 +0100
commit4fc7fcb775140dc774a713063917fbf7d1392bdf (patch)
treeffb29f080816cc85e7a47a96cde8e79945d023a8
parent2bedb0fe2c38424a53ec7376c05db09090af9f93 (diff)
downloadNim-4fc7fcb775140dc774a713063917fbf7d1392bdf.tar.gz
`--hintAsError` (#16763)
* --hintAsError

* add test, changelog

* condsyms
-rw-r--r--changelog.md1
-rw-r--r--compiler/commands.nim10
-rw-r--r--compiler/condsyms.nim1
-rw-r--r--compiler/msgs.nim8
-rw-r--r--compiler/pragmas.nim1
-rw-r--r--compiler/wordrecg.nim5
-rw-r--r--doc/advopt.txt4
-rw-r--r--tests/misc/twarningaserror.nim35
8 files changed, 56 insertions, 9 deletions
diff --git a/changelog.md b/changelog.md
index 5f3db3dfb..af979fc93 100644
--- a/changelog.md
+++ b/changelog.md
@@ -135,6 +135,7 @@ with other backends. see #9125. Use `-d:nimLegacyJsRound` for previous behavior.
 - Type mismatch errors now show more context, use `-d:nimLegacyTypeMismatch` for previous
   behavior.
 
+- Added `--hintAsError` with similar semantics as `--warningAsError`.
 
 ## Tool changes
 
diff --git a/compiler/commands.nim b/compiler/commands.nim
index bc1a63cc4..0c835a2f7 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -191,7 +191,8 @@ proc processSpecificNote*(arg: string, state: TSpecialWord, pass: TCmdLinePass,
   if i == arg.len: discard
   elif i < arg.len and (arg[i] in {':', '='}): inc(i)
   else: invalidCmdLineOption(conf, pass, orig, info)
-  if state == wHint:
+  # unfortunately, hintUser and warningUser clash
+  if state in {wHint, wHintAsError}:
     let x = findStr(hintMin, hintMax, id, errUnknown)
     if x != errUnknown: n = TNoteKind(x)
     else: localError(conf, info, "unknown hint: " & id)
@@ -209,13 +210,13 @@ proc processSpecificNote*(arg: string, state: TSpecialWord, pass: TCmdLinePass,
     incl(conf.modifiedyNotes, n)
     case val
     of "on":
-      if state == wWarningAsError:
-        incl(conf.warningAsErrors, n)
+      if state in {wWarningAsError, wHintAsError}:
+        incl(conf.warningAsErrors, n) # xxx rename warningAsErrors to noteAsErrors
       else:
         incl(conf.notes, n)
         incl(conf.mainPackageNotes, n)
     of "off":
-      if state == wWarningAsError:
+      if state in {wWarningAsError, wHintAsError}:
         excl(conf.warningAsErrors, n)
       else:
         excl(conf.notes, n)
@@ -607,6 +608,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
   of "warning": processSpecificNote(arg, wWarning, pass, info, switch, conf)
   of "hint": processSpecificNote(arg, wHint, pass, info, switch, conf)
   of "warningaserror": processSpecificNote(arg, wWarningAsError, pass, info, switch, conf)
+  of "hintaserror": processSpecificNote(arg, wHintAsError, pass, info, switch, conf)
   of "hints":
     if processOnOffSwitchOrList(conf, {optHints}, arg, pass, info): listHints(conf)
   of "threadanalysis": processOnOffSwitchG(conf, {optThreadAnalysis}, arg, pass, info)
diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim
index 55f2b4a0f..a6ad45436 100644
--- a/compiler/condsyms.nim
+++ b/compiler/condsyms.nim
@@ -124,3 +124,4 @@ proc initDefines*(symbols: StringTableRef) =
   defineSymbol("nimHasCastPragmaBlocks")
   defineSymbol("nimHasDeclaredLocs")
   defineSymbol("nimHasJsBigIntBackend")
+  defineSymbol("nimHasHintAsError")
diff --git a/compiler/msgs.nim b/compiler/msgs.nim
index afb51da09..442695ea1 100644
--- a/compiler/msgs.nim
+++ b/compiler/msgs.nim
@@ -413,7 +413,7 @@ proc handleError(conf: ConfigRef; msg: TMsgKind, eh: TErrorHandling, s: string)
     if conf.cmd == cmdIdeTools: log(s)
     quit(conf, msg)
   if msg >= errMin and msg <= errMax or
-      (msg in warnMin..warnMax and msg in conf.warningAsErrors):
+      (msg in warnMin..hintMax and msg in conf.warningAsErrors):
     inc(conf.errorCounter)
     conf.exitcode = 1'i8
     if conf.errorCounter >= conf.errorMax:
@@ -522,7 +522,11 @@ proc liMessage*(conf: ConfigRef; info: TLineInfo, msg: TMsgKind, arg: string,
   of hintMin..hintMax:
     sev = Severity.Hint
     ignoreMsg = not conf.hasHint(msg)
-    title = HintTitle
+    if msg in conf.warningAsErrors:
+      ignoreMsg = false
+      title = ErrorTitle
+    else:
+      title = HintTitle
     color = HintColor
     inc(conf.hintCounter)
 
diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim
index 1404959c6..c8aee3327 100644
--- a/compiler/pragmas.nim
+++ b/compiler/pragmas.nim
@@ -352,6 +352,7 @@ proc processNote(c: PContext, n: PNode) =
     of wHint: handleNote(hintMin .. hintMax, c.config.notes)
     of wWarning: handleNote(warnMin .. warnMax, c.config.notes)
     of wWarningAsError: handleNote(warnMin .. warnMax, c.config.warningAsErrors)
+    of wHintAsError: handleNote(hintMin .. hintMax, c.config.warningAsErrors)
     else: invalidPragma(c, n)
   else: invalidPragma(c, n)
 
diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim
index 170d04df1..ad529f437 100644
--- a/compiler/wordrecg.nim
+++ b/compiler/wordrecg.nim
@@ -49,7 +49,10 @@ type
     wNosinks = "nosinks", wMerge = "merge", wLib = "lib", wDynlib = "dynlib",
     wCompilerProc = "compilerproc", wCore = "core", wProcVar = "procvar", 
     wBase = "base", wUsed = "used", wFatal = "fatal", wError = "error", wWarning = "warning", 
-    wHint = "hint", wWarningAsError = "warningAsError", wLine = "line", wPush = "push",
+    wHint = "hint",
+    wWarningAsError = "warningAsError",
+    wHintAsError = "hintAsError",
+    wLine = "line", wPush = "push",
     wPop = "pop", wDefine = "define", wUndef = "undef", wLineDir = "lineDir", 
     wStackTrace = "stackTrace", wLineTrace = "lineTrace", wLink = "link", wCompile = "compile",
     wLinksys = "linksys", wDeprecated = "deprecated", wVarargs = "varargs", wCallconv = "callconv", 
diff --git a/doc/advopt.txt b/doc/advopt.txt
index a3040f378..93a985a37 100644
--- a/doc/advopt.txt
+++ b/doc/advopt.txt
@@ -41,8 +41,8 @@ Advanced options:
   --warning[X]:on|off       turn specific warning X on|off
   --hints:on|off|list       turn all hints on|off or list all available
   --hint[X]:on|off          turn specific hint X on|off
-  --warningAsError[X]:on|off
-                            turn specific warning X into an error on|off
+  --warningAsError[X]:on|off turn specific warning X into an error on|off
+  --hintAsError[X]:on|off    turn specific hint X into an error on|off
   --styleCheck:off|hint|error
                             produce hints or errors for Nim identifiers that
                             do not adhere to Nim's official style guide
diff --git a/tests/misc/twarningaserror.nim b/tests/misc/twarningaserror.nim
new file mode 100644
index 000000000..6f7b76095
--- /dev/null
+++ b/tests/misc/twarningaserror.nim
@@ -0,0 +1,35 @@
+discard """
+  joinable: false
+"""
+
+#[
+tests: hintAsError, warningAsError
+]#
+
+template fn1 =
+  {.hintAsError[ConvFromXtoItselfNotNeeded]:on.}
+  proc fn(a: string) = discard a.string
+  {.hintAsError[ConvFromXtoItselfNotNeeded]:off.}
+
+template fn2 =
+  {.hintAsError[ConvFromXtoItselfNotNeeded]:on.}
+  proc fn(a: string) = discard a
+  {.hintAsError[ConvFromXtoItselfNotNeeded]:off.}
+
+template gn1 =
+  {.warningAsError[ProveInit]:on.}
+  proc fn(): var int = discard
+  discard fn()
+  {.warningAsError[ProveInit]:off.}
+
+template gn2 =
+  {.warningAsError[ProveInit]:on.}
+  proc fn(): int = discard
+  discard fn()
+  {.warningAsError[ProveInit]:off.}
+
+doAssert not compiles(fn1())
+doAssert compiles(fn2())
+
+doAssert not compiles(gn1())
+doAssert compiles(gn2())
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094