Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named Tuple element selectable even when multiple elements have same name #21301

Open
kavedaa opened this issue Jul 30, 2024 · 5 comments
Open
Assignees
Labels
area:named-tuples Issues tied to the named tuples feature. itype:bug

Comments

@kavedaa
Copy link

kavedaa commented Jul 30, 2024

Compiler version

3.5.0-RC5 and others.

Minimized code

Welcome to Scala 3.6.0-RC1-bin-20240729-4429d73-NIGHTLY-git-4429d73 (21, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import scala.language.experimental.namedTuples

scala> val x = (1, 2)
val x: (Int, Int) = (1,2)

scala> val y: NamedTuple.NamedTuple[("a", "a"), (Int, Int)] = x
val y: (a : Int, a : Int) = (1,2)

scala> y.a
val res0: Int = 1

Expectation

Compilation error on y.a.

@kavedaa kavedaa added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jul 30, 2024
@soronpo
Copy link
Contributor

soronpo commented Jul 30, 2024

The error should be in preventing such named tuple as defined by y. I'm not sure if it's possible with current named tuple implementation.

@bishabosha
Copy link
Member

bishabosha commented Jul 30, 2024

The problem identified is manually constructing the type - it's on you to enforce the uniqueness - if you use the named tuple syntax, or any of the named tuple methods then there is a check

It's a bit like asInstanceOf

@kavedaa
Copy link
Author

kavedaa commented Jul 31, 2024

@bishabosha I think the questions should rather be:

  1. Is it possible for the compiler to detect multiple elements with same name?
  2. If so, would that be useful?

I think the answer to 2) is clearly "yes". I don't have sufficiently technical knowledge to answer 1), but my assumption was that at y.a the compiler has to at least look up which element corresponds to a - if any at all - and that at that point it would be possible to detect that there was not only one but multiple elements having that name. I could be wrong of course.

@bishabosha
Copy link
Member

bishabosha commented Jul 31, 2024

There could probably be put in an ambiguity check on selection, here is the code taken from Typer:

def tryNamedTupleSelection() =
  val namedTupleElems = qual.tpe.widenDealias.namedTupleElementTypes
  val nameIdx = namedTupleElems.indexWhere(_._1 == selName) 
  if nameIdx >= 0 && Feature.enabled(Feature.namedTuples) then // ? include extra `count == 1` check?
    typed(
      untpd.Apply(
        untpd.Select(untpd.TypedSplice(qual), nme.apply),
        untpd.Literal(Constant(nameIdx))),
      pt)
  else EmptyTree

otherwise the cost of peephole checking all types in case they might be some malformed named tuple might be too much

@Gedochao Gedochao added area:named-tuples Issues tied to the named tuples feature. and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 5, 2024
@Gedochao
Copy link
Contributor

Potentially relevant to #22192

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:named-tuples Issues tied to the named tuples feature. itype:bug
Projects
None yet
Development

No branches or pull requests

5 participants