Skip to content

Commit

Permalink
Merge pull request #1531 from hylo-lang/better-ambuiguity-diagnostics
Browse files Browse the repository at this point in the history
Better ambiguity diagnostics
  • Loading branch information
kyouko-taiga authored Jul 21, 2024
2 parents f75ca3d + 8211b05 commit 92ef38a
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Sources/FrontEnd/DiagnosticSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public struct DiagnosticSet: Error {
containsError = containsError || other.containsError
}

/// Removes from `self` the elements that are not also contained in `other`.
public mutating func formIntersection(_ other: Self) {
self = .init(elements.filter(other.elements.contains(_:)))
}

/// Throws `self` if any errors were reported.
public func throwOnError() throws {
if containsError { throw self }
Expand Down
10 changes: 8 additions & 2 deletions Sources/FrontEnd/TypeChecking/ConstraintSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,17 @@ struct ConstraintSystem {
penalties: penalties, diagnostics: d, stale: stale.map({ goals[$0] }))
}

/// Creates an ambiguous solution.
/// Creates an ambiguous solution, reporting the ambiguity with `d`.
///
/// The return value is the intersection of the choices and diagnostics that are identical in
/// each result along with an additional diagnostic describing the ambiguity.
///
/// - Requires: `result` contains at least two elements.
private func formAmbiguousSolution<T>(
_ results: Explorations<T>, diagnosedBy d: Diagnostic
) -> Solution {
var s = results.elements.reduce(into: Solution(), { (s, r) in s.formIntersection(r.solution) })
let (first, others) = results.elements.headAndTail!
var s = others.reduce(into: first.solution, { (s, r) in s.formIntersection(r.solution) })
s.incorporate(d)
return s
}
Expand Down
1 change: 1 addition & 0 deletions Sources/FrontEnd/TypeChecking/Solution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct Solution {
mutating func formIntersection(_ other: Self) {
typeAssumptions.formIntersection(other.typeAssumptions)
bindingAssumptions.formIntersection(other.bindingAssumptions)
diagnostics.formIntersection(other.diagnostics)
penalties = max(penalties, other.penalties)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/HyloTests/TestCases/TypeChecking/Overloading.hylo
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public fun main() {
check<Any>(x6)

let a = 1
fn(a, a) //! diagnostic ambiguous use of 'fn'
_ = fn(a, a) //! diagnostic ambiguous use of 'fn'
}

0 comments on commit 92ef38a

Please sign in to comment.