Skip to content

Unsupported-syntax diagnostics are preempted by closure-variable resolution #2758

Description

@egparedes

Constructs that gt4py.next rejects via the _UNSUPPORTED_FEATURE_HINTS catalogue in ffront/dialect_parser.py only produce their friendly diagnostic when they do not reference an external name. If the construct names a symbol (e.g. an exception type), closure-variable resolution runs first and fails with an unrelated, internal-sounding message.

Reproduction

import gt4py.next as gtx
from gt4py.next import float64
from gt4py.next.ffront.func_to_foast import FieldOperatorParser

IDim = gtx.Dimension("IDim")


def try_finally(a: gtx.Field[[IDim], float64]) -> gtx.Field[[IDim], float64]:
    try:
        a = a + 1.0
    finally:
        pass
    return a


def try_except(a: gtx.Field[[IDim], float64]) -> gtx.Field[[IDim], float64]:
    try:
        a = a + 1.0
    except ValueError:
        pass
    return a

Results:

program diagnostic
try_finally UnsupportedPythonFeatureError: Unsupported Python syntax: 'try' statement. (correct)
try_except DSLTypeError: Unexpected object 'ValueError' of type '<class 'type'>' encountered. (wrong)

Why

ValueError is collected as a closure variable and converted to a FOAST symbol before the AST is visited, so DialectParser.generic_visit — which consults the catalogue — is never reached.

Impact

  • try / except X is the common spelling; the catalogued ast.Try message is effectively only reachable via the rare try / finally form.
  • ast.TryStar is entirely unreachable: try* grammatically requires an except* handler, which must name an exception type. The catalogue entry added in fix[cartesian, eve, next]: latent bugs and deprecated typing shims surfaced by the Python 3.12 floor #2755 is therefore pinned at the catalogue level rather than end-to-end, with a comment explaining why.
  • ast.Raise is likely affected in the same way (raise ValueError(...)).

The user-visible effect is that a plain unsupported-syntax error is reported as an internal type error with no actionable hint.

Possible direction

Visit the AST for unsupported constructs before (or independently of) resolving closure variables, so syntax-level rejections win over symbol-level ones. Adding a test for the try / except X spelling in tests/next_tests/unit_tests/ffront_tests/test_diagnostic_messages.py would pin the fix.

Found while removing the Python 3.10 shims in #2755.

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-foundIssues found by agents and need triage. Remove the label once the issue is human confirmed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions