Repository: hylo-lang/hylo (swift)

Issue:
Confusing type error
Inference got lost in this program for some reason:

```
type Item: SemiRegular {
  public let cost: Int
  public memberwise init
  public fun infix== (_ other: Item) -> Bool { self.cost == other.cost }
}

fun name(of i: Int, in items : Array<Item>) -> Int {
  items[i].cost.copy() // error: type 'Item' has no member 'copy'
}
```

The program compiles if the body of `name(of:in:)` is replaced by the following:

```
let x = items[i]
return x.cost.copy()
```

This problem was observed on e86fe9e.

Pull request description:
Fixes #1219

Patch:
diff --git a/Sources/FrontEnd/TypeChecking/TypeChecker.swift b/Sources/FrontEnd/TypeChecking/TypeChecker.swift
index 78cce4fb4..303687fcc 100644
--- a/Sources/FrontEnd/TypeChecking/TypeChecker.swift
+++ b/Sources/FrontEnd/TypeChecking/TypeChecker.swift
@@ -5302,14 +5302,14 @@ struct TypeChecker {
     }
 
     // Create the necessary constraints to let the solver resolve the remaining components.
-    for (i, component) in unresolved.enumerated() {
+    for i in (0 ..< unresolved.count).reversed() {
       let memberType = ^freshVariable()
       obligations.insert(
         MemberConstraint(
-          lastVisited!, hasMember: memberType, referredToBy: component, in: program.ast,
-          usedAs: (i == unresolved.count - 1) ? purpose : .unapplied,
-          origin: ConstraintOrigin(.member, at: program[component].site)))
-      lastVisited = constrain(component, to: memberType, in: &obligations)
+          lastVisited!, hasMember: memberType, referredToBy: unresolved[i], in: program.ast,
+          usedAs: (i == 0) ? purpose : .unapplied,
+          origin: ConstraintOrigin(.member, at: program[unresolved[i]].site)))
+      lastVisited = constrain(unresolved[i], to: memberType, in: &obligations)
     }
 
     return lastVisited!


Test patch:
diff --git a/Tests/HyloTests/TestCases/TypeChecking/NameLookupWithDottedPath.hylo b/Tests/HyloTests/TestCases/TypeChecking/NameLookupWithDottedPath.hylo
new file mode 100644
index 000000000..8bc1cbcc2
--- /dev/null
+++ b/Tests/HyloTests/TestCases/TypeChecking/NameLookupWithDottedPath.hylo
@@ -0,0 +1,16 @@
+//- typeCheck expecting: success
+
+type Item: SemiRegular {
+
+  public let cost: Int
+  public memberwise init
+
+  public fun infix== (_ other: Item) -> Bool {
+    self.cost == other.cost
+  }
+
+}
+
+fun cost(of p: Bool, in items: CollectionOfOne<Item>) -> Int {
+  items[p].cost.copy()
+}