Skip to content

Commit

Permalink
Merge pull request #622 from Verdagon/solve
Browse files Browse the repository at this point in the history
Simplifying solving layer naming and params
  • Loading branch information
Verdagon authored Jul 22, 2023
2 parents 0883036 + 8520bc8 commit b79258f
Show file tree
Hide file tree
Showing 23 changed files with 526 additions and 596 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,81 +199,6 @@ class AfterRegionsIntegrationTests extends FunSuite with Matchers {
compile.evalForKind(Vector()) match { case VonInt(3) => }
}

test("Abstract func without virtual") {
val compile = RunCompilation.test(
"""
|sealed interface ISpaceship<X Ref, Y Ref, Z Ref> { }
|abstract func launch<X, Y, Z>(self &ISpaceship<X, Y, Z>, bork X) where func drop(X)void;
|
|exported func main() int {
| a = #[](10, {_});
| return a.3;
|}
""".stripMargin)

compile.getCompilerOutputs() match {
case Err(e) => vimpl(e)
case Ok(_) => vfail()
}
}

test("Cant make non-weakable extend a weakable") {
val compile = RunCompilation.test(
"""
|weakable interface IUnit {}
|struct Muta { hp int; }
|impl IUnit for Muta;
|func main(muta Muta) int { return 7; }
|""".stripMargin)

try {
compile.expectCompilerOutputs().lookupFunction("main")
vfail()
} catch {
case WeakableImplingMismatch(false, true) =>
case other => {
other.printStackTrace()
vfail()
}
}
}


test("Cant make weakable extend a non-weakable") {
val compile = RunCompilation.test(
"""
|interface IUnit {}
|weakable struct Muta { hp int; }
|impl IUnit for Muta;
|func main(muta Muta) int { return 7; }
|""".stripMargin)

try {
compile.expectCompilerOutputs().lookupFunction("main")
vfail()
} catch {
case WeakableImplingMismatch(true, false) =>
case _ => vfail()
}
}
test("Cant make weak ref to non-weakable") {
val compile = RunCompilation.test(
"""
|struct Muta { hp int; }
|func getHp(weakMuta &&Muta) { (lock(weakMuta)).get().hp }
|exported func main() int { getHp(&&Muta(7)) }
|""".stripMargin)

try {
compile.expectCompilerOutputs().lookupFunction("main")
vfail()
} catch {
case TookWeakRefOfNonWeakableError() =>
case _ => vfail()
}

}

test("Borrowing toArray") {
val compile = RunCompilation.test(
"""import list.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ object PostParserErrorHumanizer {
case ImplicitCoercionTemplateRuneS(_, inner) => humanizeRune(inner) + ".gen"
case ImplicitRegionRuneS(originalRune) => humanizeRune(originalRune) + ".region"
case CallRegionRuneS(lid) => "_" + lid.path.mkString("") + ".pcall"
case DispatcherRuneFromImplS(innerRune) => "disimpl:" + humanizeRune(innerRune)
case other => vimpl(other)
}
}
Expand Down
42 changes: 16 additions & 26 deletions Frontend/TypingPass/src/dev/vale/typing/ArrayCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class ArrayCompiler(
sizeRuneA: IRuneS,
mutabilityRune: IRuneS,
variabilityRune: IRuneS,
callableTE: ReferenceExpressionTE,
verifyConclusions: Boolean):
callableTE: ReferenceExpressionTE):
StaticArrayFromCallableTE = {
val runeTypingEnv =
new IRuneTypeSolverEnv {
Expand Down Expand Up @@ -93,7 +92,7 @@ class ArrayCompiler(
val rulesA = ruleBuilder.toVector

val CompleteCompilerSolve(_, templatas, _, Vector()) =
inferCompiler.solveExpectComplete(
inferCompiler.solveForResolving(
InferEnv(callingEnv, parentRanges, callLocation, callingEnv, region),
coutputs,
rulesA,
Expand All @@ -102,16 +101,17 @@ class ArrayCompiler(
callLocation,
Vector(),
Vector(),
true,
true,
Vector())
Vector()) match {
case Err(e) => throw CompileErrorExceptionT(TypingPassSolverError(parentRanges, e))
case Ok(c) => c
}

val size = ITemplataT.expectInteger(vassertSome(templatas.get(sizeRuneA)))
val mutability = ITemplataT.expectMutability(vassertSome(templatas.get(mutabilityRune)))
val variability = ITemplataT.expectVariability(vassertSome(templatas.get(variabilityRune)))
val prototype =
overloadResolver.getArrayGeneratorPrototype(
coutputs, callingEnv, parentRanges, callLocation, callableTE, region, true)
coutputs, callingEnv, parentRanges, callLocation, callableTE, region)
val ssaMT = resolveStaticSizedArray(mutability, variability, size, prototype.returnType, region)

maybeElementTypeRuneA.foreach(elementTypeRuneA => {
Expand All @@ -135,8 +135,7 @@ class ArrayCompiler(
maybeElementTypeRune: Option[IRuneS],
mutabilityRune: IRuneS,
sizeTE: ReferenceExpressionTE,
maybeCallableTE: Option[ReferenceExpressionTE],
verifyConclusions: Boolean):
maybeCallableTE: Option[ReferenceExpressionTE]):
ReferenceExpressionTE = {

val runeTypingEnv = TemplataCompiler.createRuneTypeSolverEnv(callingEnv)
Expand Down Expand Up @@ -197,6 +196,7 @@ class ArrayCompiler(
inferCompiler.incrementallySolve(
envs, coutputs, solver,
(solver) => {
// TODO(regions): Sometimes add default region rune
false
}) match {
case Err(f @ FailedCompilerSolve(_, _, err)) => {
Expand All @@ -207,11 +207,7 @@ class ArrayCompiler(
}

val CompleteCompilerSolve(_, templatas, runeToFunctionBound, reachableBounds) =
(inferCompiler.interpretResults(envs, coutputs, invocationRange, callLocation, runeToType, rules, verifyConclusions, true, Vector(), solver) match {
case f @ FailedCompilerSolve(_, _, _) => Err(f)
case i @ IncompleteCompilerSolve(_, _, _, _) => Err(i)
case c @ CompleteCompilerSolve(_, _, _, _) => Ok(c)
}) match {
inferCompiler.checkResolvingConclusionsAndResolve(envs, coutputs, invocationRange, callLocation, runeToType, rules, Vector(), solver) match {
case Err(e) => throw CompileErrorExceptionT(TypingPassSolverError(invocationRange, e))
case Ok(i) => (i)
}
Expand All @@ -238,7 +234,7 @@ class ArrayCompiler(

val prototype =
overloadResolver.getArrayGeneratorPrototype(
coutputs, callingEnv, parentRanges, callLocation, callableTE, region, true)
coutputs, callingEnv, parentRanges, callLocation, callableTE, region)
val rsaMT = resolveRuntimeSizedArray(prototype.returnType, mutability, region)

maybeElementTypeRune.foreach(elementTypeRuneA => {
Expand Down Expand Up @@ -275,7 +271,6 @@ class ArrayCompiler(
Vector(sizeTE.result.coord) ++
maybeCallableTE.map(c => c.result.coord),
Vector(),
true,
true) match {
case Err(e) => throw CompileErrorExceptionT(CouldntFindFunctionToCallT(parentRanges, e))
case Ok(x) => x
Expand Down Expand Up @@ -322,8 +317,7 @@ class ArrayCompiler(
mutabilityRuneA: IRuneS,
variabilityRuneA: IRuneS,
exprs2: Vector[ReferenceExpressionTE],
region: RegionT,
verifyConclusions: Boolean):
region: RegionT):
StaticArrayFromValuesTE = {

val runeTypingEnv = TemplataCompiler.createRuneTypeSolverEnv(callingEnv)
Expand Down Expand Up @@ -387,11 +381,11 @@ class ArrayCompiler(
val solver =
inferCompiler.makeSolver(
envs, coutputs, rules, runeToType, invocationRange, initialKnowns, initialSends)

// Incrementally solve and add default generic parameters (and context region).
inferCompiler.incrementallySolve(
envs, coutputs, solver,
(solver) => {
// TODO(regions): Sometimes add default region
false
}) match {
case Err(f @ FailedCompilerSolve(_, _, err)) => {
Expand All @@ -402,11 +396,7 @@ class ArrayCompiler(
}

val CompleteCompilerSolve(_, templatas, runeToFunctionBound, reachableBounds) =
(inferCompiler.interpretResults(envs, coutputs, invocationRange, callLocation, runeToType, rules, verifyConclusions, true, Vector(), solver) match {
case f @ FailedCompilerSolve(_, _, _) => Err(f)
case i @ IncompleteCompilerSolve(_, _, _, _) => Err(i)
case c @ CompleteCompilerSolve(_, _, _, _) => Ok(c)
}) match {
inferCompiler.checkResolvingConclusionsAndResolve(envs, coutputs, invocationRange, callLocation, runeToType, rules, Vector(), solver) match {
case Err(e) => throw CompileErrorExceptionT(TypingPassSolverError(invocationRange, e))
case Ok(i) => (i)
}
Expand Down Expand Up @@ -458,7 +448,7 @@ class ArrayCompiler(

val prototype =
overloadResolver.getArrayConsumerPrototype(
coutputs, fate, range, callLocation, callableTE, arrayTT.elementType, contextRegion, true)
coutputs, fate, range, callLocation, callableTE, arrayTT.elementType, contextRegion)

ast.DestroyStaticSizedArrayIntoFunctionTE(
arrTE,
Expand Down Expand Up @@ -496,7 +486,7 @@ class ArrayCompiler(

val prototype =
overloadResolver.getArrayConsumerPrototype(
coutputs, fate, range, callLocation, callableTE, arrayTT.elementType, contextRegion, true)
coutputs, fate, range, callLocation, callableTE, arrayTT.elementType, contextRegion)

// val freePrototype =
// destructorCompiler.getFreeFunction(
Expand Down
33 changes: 16 additions & 17 deletions Frontend/TypingPass/src/dev/vale/typing/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ class Compiler(
kind: KindT):
Boolean = {
kind match {
case p @ KindPlaceholderT(_) => implCompiler.isDescendant(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, p, false)
case p @ KindPlaceholderT(_) => implCompiler.isDescendant(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, p)
case contentsRuntimeSizedArrayTT(_, _, _) => false
case OverloadSetT(_, _) => false
case NeverT(fromBreak) => true
case contentsStaticSizedArrayTT(_, _, _, _, _) => false
case s @ StructTT(_) => implCompiler.isDescendant(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, s, false)
case i @ InterfaceTT(_) => implCompiler.isDescendant(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, i, false)
case s @ StructTT(_) => implCompiler.isDescendant(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, s)
case i @ InterfaceTT(_) => implCompiler.isDescendant(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, i)
case IntT(_) | BoolT() | FloatT() | StrT() | VoidT() => false
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@ class Compiler(
Set[KindT]()
}) ++
(descendant match {
case s : ISubKindTT => implCompiler.getParents(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, s, true)
case s : ISubKindTT => implCompiler.getParents(coutputs, envs.parentRanges, envs.callLocation, envs.originalCallingEnv, s)
case _ => Vector[KindT]()
})
}
Expand Down Expand Up @@ -470,8 +470,7 @@ class Compiler(
contextRegion,
coords,
Vector.empty,
true,
verifyConclusions)
true)
}

override def resolveStaticSizedArrayKind(
Expand Down Expand Up @@ -535,11 +534,10 @@ class Compiler(
coutputs: CompilerOutputs,
parentRanges: List[RangeS],
callLocation: LocationInDenizen,
functionTemplata: FunctionTemplataT,
verifyConclusions: Boolean):
functionTemplata: FunctionTemplataT):
FunctionHeaderT = {
functionCompiler.evaluateGenericFunctionFromNonCall(
coutputs, parentRanges, callLocation, functionTemplata, verifyConclusions)
coutputs, parentRanges, callLocation, functionTemplata)
}

override def scoutExpectedFunctionForPrototype(
Expand All @@ -553,8 +551,7 @@ class Compiler(
contextRegion: RegionT,
args: Vector[CoordT],
extraEnvsToLookIn: Vector[IInDenizenEnvironmentT],
exact: Boolean,
verifyConclusions: Boolean):
exact: Boolean):
StampFunctionSuccess = {
overloadResolver.findFunction(
env,
Expand All @@ -567,8 +564,7 @@ class Compiler(
contextRegion,
args,
extraEnvsToLookIn,
exact,
verifyConclusions) match {
exact) match {
case Err(e) => throw CompileErrorExceptionT(CouldntFindFunctionToCallT(callRange, e))
case Ok(x) => x
}
Expand Down Expand Up @@ -673,7 +669,7 @@ class Compiler(
args: Vector[CoordT]):
IEvaluateFunctionResult = {
functionCompiler.evaluateTemplatedFunctionFromCallForPrototype(
coutputs, callRange, callLocation, callingEnv, functionTemplata, explicitTemplateArgs, contextRegion, args, true)
coutputs, callRange, callLocation, callingEnv, functionTemplata, explicitTemplateArgs, contextRegion, args)
}

override def evaluateGenericFunctionFromCallForPrototype(
Expand Down Expand Up @@ -983,7 +979,7 @@ class Compiler(
val templata = FunctionTemplataT(packageEnv, functionA)
val header =
functionCompiler.evaluateGenericFunctionFromNonCall(
coutputs, List(), LocationInDenizen(Vector()), templata, true)
coutputs, List(), LocationInDenizen(Vector()), templata)

functionA.attributes.collectFirst({ case e @ ExternS(_) => e }) match {
case None =>
Expand Down Expand Up @@ -1149,10 +1145,13 @@ class Compiler(
globalEnv, packageEnv, placeholderedExportId, TemplatasStore(placeholderedExportId, Map(), Map()))

val CompleteCompilerSolve(_, templataByRune, _, Vector()) =
inferCompiler.solveExpectComplete(
inferCompiler.solveForDefining(
InferEnv(exportEnv, List(range), LocationInDenizen(Vector()), exportEnv, regionPlaceholder),
coutputs, rules, runeToType, List(range),
LocationInDenizen(Vector()), Vector(), Vector(), true, true, Vector())
LocationInDenizen(Vector()), Vector(), Vector(), Vector()) match {
case Err(f) => throw CompileErrorExceptionT(typing.TypingPassSolverError(List(range), f))
case Ok(c@CompleteCompilerSolve(_, _, _, _)) => c
}
templataByRune.get(typeRuneT.rune) match {
case Some(KindTemplataT(kind)) => {
coutputs.addKindExport(range, kind, placeholderedExportId, exportedName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import PostParserErrorHumanizer._
import dev.vale.postparsing.rules.IRulexSR
import dev.vale.postparsing.PostParserErrorHumanizer
import OverloadResolver._
import dev.vale.highertyping.{FunctionA, HigherTypingErrorHumanizer}
import dev.vale.typing.ast.{AbstractT, FunctionBannerT, FunctionCalleeCandidate, HeaderCalleeCandidate, ICalleeCandidate, PrototypeT, SignatureT}
import dev.vale.typing.infer.{BadIsaSubKind, BadIsaSuperKind, CallResultWasntExpectedType, CantCheckPlaceholder, CantGetComponentsOfPlaceholderPrototype, CantShareMutable, CouldntFindFunction, CouldntResolveKind, ITypingPassSolverError, IsaFailed, KindIsNotConcrete, KindIsNotInterface, LookupFailed, NoAncestorsSatisfyCall, OneOfFailed, OwnershipDidntMatch, ReceivingDifferentOwnerships, ReturnTypeConflict, SendingNonCitizen, SendingNonIdenticalKinds, WrongNumberOfTemplateArgs}
import dev.vale.highertyping._
import dev.vale.typing.ast._
import dev.vale.typing.infer._
import dev.vale.typing.names._
import dev.vale.typing.templata._
import dev.vale.typing.ast._
Expand Down Expand Up @@ -440,13 +440,16 @@ object CompilerErrorHumanizer {
}
case LookupFailed(name) => "Couldn't find anything named: " + humanizeImpreciseName(name)
case KindIsNotConcrete(kind) => {
"Expected kind to be concrete, but was not. Kind: " + kind
"Expected kind to be concrete, but was not. Kind: " + humanizeKind(codeMap, kind)
}
case OneOfFailed(rule) => {
"One-of rule failed."
}
case KindIsNotInterface(kind) => {
"Expected kind to be interface, but was not. Kind: " + kind
"Expected kind to be interface, but was not. Kind: " + humanizeKind(codeMap, kind)
}
case CallResultIsntCallable(result) => {
"Generic call result isn't callable: " + humanizeTemplata(codeMap, result)
}
case CallResultWasntExpectedType(expected, actual) => {
"Expected an instantiation of " + humanizeTemplata(codeMap, expected) + " but got " + humanizeTemplata(codeMap, actual)
Expand Down Expand Up @@ -654,6 +657,7 @@ object CompilerErrorHumanizer {
case IteratorNameT(range) => "it:" + codeMap(range.begin)
case IterableNameT(range) => "ib:" + codeMap(range.begin)
case IterationOptionNameT(range) => "io:" + codeMap(range.begin)
case ImplTemplateNameT(codeLoc) => "implt:" + codeMap(codeLoc)
case ForwarderFunctionNameT(_, inner) => humanizeName(codeMap, inner)
case ForwarderFunctionTemplateNameT(inner, index) => "fwd" + index + ":" + humanizeName(codeMap, inner)
case MagicParamNameT(codeLoc) => "mp:" + codeMap(codeLoc)
Expand Down
Loading

0 comments on commit b79258f

Please sign in to comment.