Skip to content

hasFeature() by itself doesn't prevent parsing of new syntax  #64627

Open
@gwynne

Description

@gwynne

The following code produces an error in Swift 5.8 and swift-DEVELOPMENT-SNAPSHOT-2023-03-23-a:

#if compiler(>=5.8)
#if hasFeature(VariadicGenerics)
func f<each T>(_: repeat each T) {}
#else
// Code for supporting older compilers
#endif
#else
// Repeat the code for supporting older compilers
#endif
$ swift test.swift
test.swift:3:13: error: expected '>' to complete generic parameter list
func f<each T>(_: repeat each T) {}
            ^

To support compilers older than 5.8, one must write:

#if compiler(>=5.8)
#if compiler(>=5.8) && hasFeature(VariadicGenerics)
func f<each T>(_: repeat each T) {}
#else
// Code for supporting older compilers
#endif
#else
// Repeat the code for supporting older compilers
#endif

This is awkward and requires inline comments to avoid confusion. It's already ugly enough to have to repeat the else branch when supporting 5.7 and earlier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    #ifFeature → compiler control statements: Conditional compilation blocksbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcompiler control statementsFeature: compiler control statementsparserArea → compiler: The legacy C++ parserswift 5.8unexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions