← 输入框|github.com|刷新
Skip to content

Improve error message for undefined user type #1993

Closed

Description

@certik

This:

def check_re_range_alternatives(
        alts : list[ReRange],
        char : str) -> bool:
    result : bool = False
    rr : ReRange
    for rr in alts:
        if check_re_range(rr, char):
            result = True
            break
    return result

gives:

$ lpython a.py 
semantic error: Unsupported type annotation: ReRange
 --> a.py:2:21
  |
2 |         alts : list[ReRange],
  |                     ^^^^^^^ 

But rather the error message should be:

semantic error: The type `ReRange` is not declared

or

semantic error: Unknown type `ReRange`

I think I like the second more.

Regarding "Unsupported type annotation: ReRange", that should be emitted for things like from typing import List. If you just do List[i32] without importing it from anywhere, then I think it should just say Unknown type List, and we can provide a "hint: did you mean list?"

Activity

  1. ubaidsk commented on Jun 22, 2023

    @ubaidsk
    Collaborator

    With respect to this issue, shall we update the error message as follows:

    diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp
    index b5b4ed779..401eb8b4c 100644
    --- a/src/lpython/semantics/python_ast_to_asr.cpp
    +++ b/src/lpython/semantics/python_ast_to_asr.cpp
    @@ -1092,7 +1092,7 @@ public:
             }
     
             if( !type && raise_error ) {
    -            throw SemanticError("Unsupported type annotation: " + var_annotation, loc);
    +            throw SemanticError("Unknown type `" + var_annotation + "`", loc);
             }
     
             return type;
    (END)

    Some examples of how it would look:

    (lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py    
    def main0():
        x: ReRange
    
    main0()
    (lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py
    semantic error: Unknown type `ReRange`
     --> examples/expr2.py:2:8
      |
    2 |     x: ReRange
      |        ^^^^^^^ 
    
    
    Note: if any of the above error or warning messages are not clear or are lacking
    context please report it to us (we consider that a bug that must be fixed).
    (lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py    
    def main0():
        x: ReRange[i32]
    
    main0()
    (lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py
    semantic error: Unknown type `ReRange`
     --> examples/expr2.py:2:8
      |
    2 |     x: ReRange[i32]
      |        ^^^^^^^^^^^^ 
    
    
    Note: if any of the above error or warning messages are not clear or are lacking
    context please report it to us (we consider that a bug that must be fixed).
    (lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py    
    def main0():
        x: list[ReRange]
    
    main0()
    (lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py
    semantic error: Unknown type `ReRange`
     --> examples/expr2.py:2:13
      |
    2 |     x: list[ReRange]
      |             ^^^^^^^ 
    
    
    Note: if any of the above error or warning messages are not clear or are lacking
    context please report it to us (we consider that a bug that must be fixed).
    (lp) ubaid@ubaids-MacBook-Pro lpython % cat examples/expr2.py    
    def main0():
        x: List[ReRange]
    
    main0()
    (lp) ubaid@ubaids-MacBook-Pro lpython % lpython examples/expr2.py
    semantic error: Unknown type `List`
     --> examples/expr2.py:2:8
      |
    2 |     x: List[ReRange]
      |        ^^^^^^^^^^^^^ 
    
    
    Note: if any of the above error or warning messages are not clear or are lacking
    context please report it to us (we consider that a bug that must be fixed).
  2. ubaidsk commented on Jun 22, 2023

    @ubaidsk
    Collaborator

    If you just do List[i32] without importing it from anywhere, then I think it should just say Unknown type List, and we can provide a "hint: did you mean list?"

    Is this a part of this issue? It seems there could several similar hints, for example Unknown type I32, hint: Use i32 and similarly for other types.

  3. certik commented on Jun 22, 2023

    @certik
    ContributorAuthor

    Yes, we can do hints in some other issue.

    Now when I see the examples, "semantic error: Unknown type ReRange" seems a bit short --- it doesn't provide hints what should be done. Maybe "semantic error: The type ReRange is undeclared", suggesting that it should be declared first.

    @rebcabin what exact error message text would you recommend here?

  4. added
    good first issue
    on Sep 27, 2023
  5. tanay-man commented on Feb 7, 2024

    @tanay-man
    Contributor

    @certik Is this error message acceptable?

        semantic error: The type ReRange is undeclared
        --> ../../../lpython_test.py:2:21
    
        alts : list[ReRange],
                     ^^^^^^^ 
    
  6. rebcabin commented on Feb 7, 2024

    @rebcabin
    Contributor

    I like "semantic error: The type 'ReRange' is undeclared."

    Note the punctuation: ReRange must appear in quotes and the entire message must end with a period because the message is a sentence with a subject, object, and verb. An error message like "Unknown type: ReRange" is also acceptable. In such an error message, ReRange is NOT in quotes and the message is not terminated with a period because the message is NOT a sentence, it's just a noun phrase. See any edition of "The Gregg Reference Manual" for the rules of English punctuation. It's important for our messages to be grammatical and correctly punctuated. Otherwise, fluent readers of English will be distracted by the errors in grammar or punctuation and readers will also get an impression that our outputs are less than professional.

  7. tanay-man commented on Feb 7, 2024

    @tanay-man
    Contributor

    I agree that grammatically incorrect error messages can be distracting. After changing the thrown error to 'semantic error: The type 'ReRange' is undeclared.' should the tests be updated?

  8. rebcabin commented on Feb 7, 2024

    @rebcabin
    Contributor

    Yes, all the tests must pass before submitting a pull request :) We have continuous integration, too, which probably checks this.

    ctest
    ./run_tests.py
    pushd integration_tests
    ./run_tests.py
    popd
    
  9. added a commit that references this issue on Feb 8, 2024
  10. added a commit that references this issue on Feb 8, 2024
  11. added a commit that references this issue on Mar 5, 2024
  12. aryangupta701 commented on Mar 24, 2024

    @aryangupta701
    Contributor

    is this solved?

  13. ubaidsk commented on Mar 24, 2024

    @ubaidsk
    Collaborator

    With the latest main, the example in the issue description now gives:

    % cat examples/expr2.py 
    def check_re_range_alternatives(
            alts : list[ReRange],
            char : str) -> bool:
        result : bool = False
        rr : ReRange
        for rr in alts:
            if check_re_range(rr, char):
                result = True
                break
        return result
    % lpython examples/expr2.py 
    semantic error: The type 'ReRange' is undeclared.
     --> examples/expr2.py:2:21
      |
    2 |         alts : list[ReRange],
      |                     ^^^^^^^ 
    
    
    Note: Please report unclear or confusing messages as bugs at
    https://github.com/lcompilers/lpython/issues.
  14. ubaidsk commented on Mar 24, 2024

    @ubaidsk
    Collaborator

    I think this issue is fixed with the latest main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions