Skip to content

Commit

Permalink
Tweak to type inference of Seq and Array collection literals
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jan 17, 2025
1 parent 2120996 commit 4b8a832
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2460,8 +2460,13 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
untpd.TypedSplice(witness.select(nme.fromLiteral))
case _ =>
defaultMaker
// When expected type is a Seq or Array, propagate the `elemProto` as expected
// type of the elements.
val elems = elemProto match
case WildcardType(_) => tree.elems
case _ => tree.elems.map(untpd.Typed(_, untpd.TypeTree(elemProto)))
typed(
untpd.Apply(maker, tree.elems).withSpan(tree.span)
untpd.Apply(maker, elems).withSpan(tree.span)
.showing(i"typed collection literal $tree ---> $result", typr)
, pt)

Expand Down
6 changes: 6 additions & 0 deletions docs/_docs/reference/experimental/collection-literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ Using this scheme, the literals we have seen earlier could also be given alterna
implicit search. That is, an implicit search for a given of the type would not be
attempted because the type is not specific enough. Concretely, this is the case for Wildcard types `?`, `Any`, `AnyRef`, unconstrained type variables, or type variables constrained from above by an under-specified type.

- If the expected type is a subtype of `Seq` or an array type, we typecheck the
elements with the elements of the expected type. This means we can get the same
precision in propagated expected types as if the constructor was written explicitly.
Hence, we can't regress by going from `Seq(...)` or `Array(...)` to a
collection literal.

**Syntax**

```
Expand Down

0 comments on commit 4b8a832

Please sign in to comment.