hylo icon indicating copy to clipboard operation
hylo copied to clipboard

Parser gets confused when first token of a subscript is `sink`

Open kyouko-taiga opened this issue 2 years ago • 2 comments
trafficstars

This should parse but doesn't:

public subscript unsafe_bitcast<T, U>(_ value: T): U {
  sink let p = Pointer.to[value].copy().value
  yield p as* (remote let U)
}

kyouko-taiga avatar Jul 30 '23 00:07 kyouko-taiga

I think I understand what is happening, but I don't know how to fix it.

A subscript implementation is a combination of implIntroducer and maybe functionBody. https://github.com/hylo-lang/hylo/blob/5b773b79f9ea009c7480625c3b8021cb74817dc9/Sources/FrontEnd/Parse/Parser.swift#L1218

The implIntroducer consumes the sink, but actually it should be let because the sink let is a BindingPattern.Introducer in the function body.(?) https://github.com/hylo-lang/hylo/blob/5b773b79f9ea009c7480625c3b8021cb74817dc9/Sources/FrontEnd/Parse/Parser.swift#L1165-L1170 https://github.com/hylo-lang/hylo/blob/5b773b79f9ea009c7480625c3b8021cb74817dc9/Sources/FrontEnd/Parse/Parser.swift#L2638-L2641

This this close? Any hints about how to fix this?

Perhaps use state.peek(2) to read the next two tokens. If it's .sink and .let, then implIntroducer should return let. I might be way off base. 😅

peter-evans avatar Jan 12 '24 17:01 peter-evans

I think the problem also occurs when the first token in the body of a subscript bundle is let or inout. I'm not sure that increasing lookahead will catch all cases (we need more rigorous testing for the parser) but that might be worth a try. A more expensive solution would be to backtrack.

kyouko-taiga avatar Jan 14 '24 09:01 kyouko-taiga