Skip to content

trimming trivia nodes corrupts source ranges #2687

@tayloraswift

Description

@tayloraswift

Description

using the trimmed family of SyntaxProtocol methods corrupts the ByteSourceRanges when obtaining syntax classifications.

i was able to reproduce this on 510.0.2, and on 600.0.0-prerelease-2024-06-12.

Steps to Reproduce

import SwiftIDEUtils
import SwiftParser
import SwiftSyntax

let text:String = """
@_documentation(metadata: blah)
public mutating
func f() {}
"""
let utf8:[UInt8] = .init(text.utf8)

var parser:Parser = utf8.withUnsafeBufferPointer { .init($0) }
let decl:DeclSyntax = .parse(from: &parser)

let function:FunctionDeclSyntax = decl.as(FunctionDeclSyntax.self)!

for span:SyntaxClassifiedRange in function.modifiers.trimmed.classifications
{
    let range:Range<Int> = span.range.offset ..< span.range.offset + span.range.length
    print(String.init(decoding: utf8[range], as: Unicode.UTF8.self))
}

publi
c
 mutatin

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions