carbon-language / carbon-lang

Carbon Language's main repository: documents, design, implementation, and related tools. (NOTE: Carbon Language is experimental; see README)
https://github.com/carbon-language/carbon-lang/blob/trunk/README.md
Other
32.31k stars 1.48k forks source link

Are rewrites done as part of type checking or monomorphization? #4002

Open josh11b opened 1 month ago

josh11b commented 1 month ago

Summary of issue:

Right now, which rewrite is applied to uses of the subscript operator depends on which interfaces are implemented for the types, see #2274 . There is a final blanket implementation to ensure that the two rewrites produce consistent answers with the same type. However, the category of the result, whether it is a value or a reference, depends on which rewrite is applied. In a generic context, it can be known that subscripting is implemented, and the type that will result, without knowing if it produces a reference expression.

Similarly, #3720 will apply a different rewrite for binding depending on the category of the input. The type of the answer is constrained to match, but different implementations would be used in the two cases.

This leaves two possibilities, neither of which are good:

Details:

Details with an example may be found here.

Any other information that you want to share?

This came up in these contexts:

zygoloid commented 1 month ago

See also #3985 in which I thought I had a solution to this that allowed early rewrites. That doesn't work, but I think that approach with late rewrites does work, and still allows us to guarantee that monomorphization will succeed if generic type checking succeeds. However, because it's doing late rewrites, that means non-template-dependent code in a generic can vary more during monomorphization than would be possible with an early rewrite model -- and for example that will have an impact on what operations we would consider to be object-safe.