Skip to content

Commit

Permalink
Merge pull request #25 from ty1824/improvements
Browse files Browse the repository at this point in the history
Dialector
* Fixed a nullable cast bug in the resolve extension function
* Now allows properties with default values. The hasDefault property must be set on the @Property annotation and a default getter must be provided by the property itself.
* Attempted to add formatting capabilities to the Dialector code generator. This is disabled until a reflective access bug is fixed by the ktlint team/their upstream libraries.

Inkt
* Formalized the error messaging when a cycle occurs so it is more useful and complete.
  • Loading branch information
ty1824 authored Jul 27, 2023
2 parents a76528d + e43ce0b commit 9ff9251
Show file tree
Hide file tree
Showing 31 changed files with 270 additions and 149 deletions.
11 changes: 11 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ subprojects {
repositories {
mavenCentral()
}

// configure Kotlin to allow these opt-in features throughout the project
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
compilerOptions {
freeCompilerArgs.addAll(
"-opt-in=kotlin.time.ExperimentalTime",
"-opt-in=kotlin.contracts.ExperimentalContracts",
"-Xcontext-receivers"
)
}
}
}
6 changes: 5 additions & 1 deletion dialector-kt-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {
}

val kspVersion: String by project
val ktlintVersion: String by project

repositories {
google()
Expand All @@ -22,6 +23,9 @@ dependencies {
implementation("com.squareup:kotlinpoet-ksp:1.12.0")
implementation("com.google.devtools.ksp:symbol-processing-api:$kspVersion")
implementation(kotlin("reflect"))
implementation("com.pinterest.ktlint:ktlint-core:$ktlintVersion")
implementation("com.pinterest.ktlint:ktlint-rule-engine:$ktlintVersion")
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:$ktlintVersion")

testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation(project(":dialector-kt"))
Expand Down Expand Up @@ -114,4 +118,4 @@ signing {
)
sign(publishing.publications)
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class DialectorSymbolProcessorTest {
// Verify raw values
assertEquals("hello", node.property)
assertEquals("provided", node.optionalProperty)
assertEquals("default", node.defaultProperty)
assertEquals(singleChildValue, node.singleChild)
assertEquals(optionalChildValue, node.optionalChild)
assertEquals(listOf(pluralFirstChildValue, pluralSecondChildValue, pluralThirdChildValue), node.pluralChildren)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface SimpleNode : Node {
@Property
val optionalProperty: String?

@Property(hasDefault = true)
val defaultProperty: String
get() = "default"

@Child
val singleChild: ChildNode

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public interface ScopeVariable : SemanticVariable {
public data class InheritScopeConstraint(
val scope: ScopeVariable,
val inheritFrom: ScopeVariable,
val label: String
val label: String,
) : SemanticConstraint

/**
Expand All @@ -33,7 +33,7 @@ public data class DeclareElementConstraint(
val scope: ScopeVariable,
val namespace: Namespace,
val element: Node,
val name: String
val name: String,
) : SemanticConstraint

/**
Expand All @@ -44,7 +44,7 @@ public data class AliasElementConstraint(
val namespace: Namespace,
val original: String,
val aliasNamespace: Namespace,
val alias: String
val alias: String,
) : SemanticConstraint

/**
Expand All @@ -56,7 +56,7 @@ public data class ReferenceIdentifierConstraint(
/** The [Namespace] the search should use */
val namespace: Namespace,
/** The [NodeReference] to resolve */
val reference: NodeReference<out Node>
val reference: NodeReference<out Node>,
) : SemanticConstraint

public object Scopes : ConstraintCreator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public data class ReferencedTypeConstraint(
val scope: ScopeVariable,
val namespace: Namespace,
val reference: NodeReference<out Node>,
val variable: TypeVariable
val variable: TypeVariable,
) : SemanticConstraint

/**
Expand All @@ -88,7 +88,7 @@ public data class TypeScopeConstraint(
/** The type from which to derive the scope. */
val type: Type,
/** Describes this inheritance relation for debugging purposes. */
val label: String
val label: String,
) : SemanticConstraint

/**
Expand All @@ -100,7 +100,7 @@ public data class DeclareElementTypeConstraint(
val scope: ScopeVariable,
val namespace: Namespace,
val name: String,
val type: Type
val type: Type,
) : SemanticConstraint

public object TypeScopes : ConstraintCreator {
Expand Down Expand Up @@ -221,7 +221,7 @@ public typealias ReductionRoutine = ReductionContext.(constraint: TypeRelationCo
private class SimpleReductionRule<T : SemanticConstraint>(
override val name: String,
override val isValidFor: ConstraintClause<T>,
override val reduce: ReductionContext.(T) -> Unit
override val reduce: ReductionContext.(T) -> Unit,
) : ReductionRule<T>

public fun <T : SemanticConstraint> ConstraintClause<T>.reducesTo(name: String, routine: ReductionContext.(T) -> Unit): ReductionRule<T> =
Expand Down Expand Up @@ -277,7 +277,7 @@ public interface Bound {
public data class BaseBound(
override val relation: TypeRelation,
override val variable: TypeVariable,
override val boundingType: Type
override val boundingType: Type,
) : Bound

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public interface TypeVariable : Type, SemanticVariable {
public enum class TypeRelation(public val symbol: String) {
SUBTYPE("<"),
SUPERTYPE(">"),
EQUIVALENT("=");
EQUIVALENT("="),
;

public fun opposite(): TypeRelation = when (this) {
SUBTYPE -> SUPERTYPE
Expand All @@ -23,7 +24,7 @@ public enum class TypeRelation(public val symbol: String) {

public enum class VariableConstraintKind {
PULL_UP,
PUSH_DOWN
PUSH_DOWN,
}

/**
Expand All @@ -34,7 +35,7 @@ public enum class VariableConstraintKind {
*/
public data class TypeVariableConstraint(
val variable: TypeVariable,
val kind: VariableConstraintKind
val kind: VariableConstraintKind,
) : SemanticConstraint

/**
Expand All @@ -47,7 +48,7 @@ public data class TypeRelationConstraint(
val relation: TypeRelation,
val left: Type,
val right: Type,
val mutual: Boolean = false
val mutual: Boolean = false,
) : SemanticConstraint

public object Types : ConstraintCreator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface ScopeGraph : ReferenceResolver {

class SingleRootScopeGraph private constructor(
private val targets: Map<NodeReference<*>, Node>,
private val visibleElements: Map<NodeReference<*>, Sequence<Pair<Node, String>>>
private val visibleElements: Map<NodeReference<*>, Sequence<Pair<Node, String>>>,
) : ScopeGraph {
companion object {
suspend operator fun invoke(root: Node, rules: List<ScopeTraversalRule<out Node>>): SingleRootScopeGraph {
Expand Down Expand Up @@ -166,21 +166,21 @@ class LinearScopeDescriptor(private val onReference: ReferenceHandler) : ScopeDe
private data class ExplicitDeclaration(
override val namespace: Namespace,
override val node: Node,
override val identifier: String
override val identifier: String,
) : Declaration

private data class AliasDeclaration(
val forDeclaration: Declaration,
override val namespace: Namespace,
override val identifier: String
override val identifier: String,
) : Declaration {
override val node: Node
get() = forDeclaration.node
}

private data class IncomingReference(
val namespace: Namespace,
val index: Int
val index: Int,
)

private val declarations: MutableList<Declaration> = mutableListOf()
Expand Down Expand Up @@ -263,7 +263,7 @@ class LinearScopeDescriptor(private val onReference: ReferenceHandler) : ScopeDe

class SimpleScopeTraversalContext(
val createScope: () -> ScopeDescriptor,
val onTraversal: suspend ScopeTraversalContext.(node: Node, scope: ScopeDescriptor) -> Unit
val onTraversal: suspend ScopeTraversalContext.(node: Node, scope: ScopeDescriptor) -> Unit,
) : ScopeTraversalContext {

override val semantics: SemanticAnalysisContext =
Expand All @@ -275,7 +275,7 @@ class SimpleScopeTraversalContext(
}

class LinearScopeGraph private constructor(
private val referenceScopes: Map<NodeReference<*>, LinearScopeDescriptor>
private val referenceScopes: Map<NodeReference<*>, LinearScopeDescriptor>,
) : ScopeGraph {
companion object {
suspend operator fun invoke(root: Node, rules: List<ScopeTraversalRule<out Node>>): LinearScopeGraph {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class MutableInferenceGroup(
variableTerms: Set<VariableTerm>,
typeTerms: Set<TypeTerm> = setOf(),
upperBounds: Set<MutableInferenceGroup> = setOf(),
lowerBounds: Set<MutableInferenceGroup> = setOf()
lowerBounds: Set<MutableInferenceGroup> = setOf(),
) : InferenceGroup {
override val variableTerms: MutableSet<VariableTerm> = variableTerms.toMutableSet()
override val typeTerms: MutableSet<TypeTerm> = typeTerms.toMutableSet()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class InferenceSys {
* An [InferenceVariable] is a special [Type] that represents a "hole" in the type system.
*/
class InferenceVariable internal constructor(
val id: Int
val id: Int,
/*val context: VariableContext*/
) : Type {
override fun equals(other: Any?): Boolean = this === other
Expand Down Expand Up @@ -88,12 +88,12 @@ enum class Directionality {
/**
* The effect is mutually applied, from "left" to "right" and "right" to "left".
*/
Bidirectional
Bidirectional,
}

data class InferenceConstraint(
val left: Term,
val right: Term
val right: Term,
/*val directionality: Directionality = Directionality.Left,*/
/*val context: ConstraintContext*/
)
Expand Down Expand Up @@ -154,7 +154,7 @@ interface IncorporationRule
class InferenceState(
val lattice: TypeLattice,
val reductionRules: List<ReductionRule>,
val incorporationRules: List<IncorporationRule>
val incorporationRules: List<IncorporationRule>,
) {
val constraints: MutableSet<InferenceConstraint> = mutableSetOf()
val currentConstraints: MutableList<InferenceConstraint> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ typealias ReductionRoutine = ReductionContext.(constraint: RelationalConstraint)

private class SimpleReductionRule(
override val isValidFor: RelationalConstraintClause,
override val reduce: ReductionRoutine
override val reduce: ReductionRoutine,
) : ReductionRule

infix fun RelationalConstraintClause.reducesTo(routine: ReductionRoutine): ReductionRule = SimpleReductionRule(this, routine)
Expand Down Expand Up @@ -89,7 +89,7 @@ private class BaseInferenceVariable(override val id: String) : InferenceVariable
data class BaseBound(
override val relation: TypeRelation,
override val variable: InferenceVariable,
override val boundingType: Type
override val boundingType: Type,
) : Bound

sealed class BoundGraphNode {
Expand Down Expand Up @@ -218,7 +218,7 @@ class BoundSystemGraph {

class BaseInferenceContext(
val createVariable: () -> InferenceVariable,
val addConstraint: (InferenceConstraint) -> Unit
val addConstraint: (InferenceConstraint) -> Unit,
) : InferenceContext, ConstraintCreator by SimpleConstraintCreator {
override fun typeVar(): InferenceVariable = createVariable()

Expand All @@ -233,7 +233,7 @@ class BaseReductionContext(
constraint: RelationalConstraint,
rule: ReductionRule,
private val addConstraint: (RelationalConstraint, InferenceOrigin) -> Unit,
private val addBound: (Bound, InferenceOrigin) -> Unit
private val addBound: (Bound, InferenceOrigin) -> Unit,
) : ReductionContext {
private val origin = ReducedFromConstraint(constraint, rule)
override fun constraint(routine: ConstraintCreator.() -> RelationalConstraint) {
Expand Down Expand Up @@ -324,7 +324,7 @@ class BaseInferenceConstraintSystem : InferenceConstraintSystem {
private fun reduce(
reductionRules: List<ReductionRule>,
constraints: ConstraintSystem,
bounds: BoundSystemGraph
bounds: BoundSystemGraph,
) {
constraints.reduce { constraint ->
when (val currentConstraint = constraint) {
Expand All @@ -341,7 +341,7 @@ class BaseInferenceConstraintSystem : InferenceConstraintSystem {
},
{ bound, origin ->
bounds.addBound(bound, origin)
}
},
)
reductionContext.reduce(currentConstraint)
}
Expand All @@ -353,7 +353,7 @@ class BaseInferenceConstraintSystem : InferenceConstraintSystem {

private fun incorporate(
constraints: ConstraintSystem,
boundSystem: BoundSystemGraph
boundSystem: BoundSystemGraph,
) {
SimpleConstraintCreator.apply {
// TODO: Handle constraint origin once supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ interface InferenceVariable : Type {
enum class TypeRelation(val symbol: String) {
SUBTYPE("<"),
SUPERTYPE(">"),
EQUIVALENT("=");
EQUIVALENT("="),
;

fun opposite(): TypeRelation = when (this) {
SUBTYPE -> SUPERTYPE
Expand All @@ -43,7 +44,7 @@ enum class TypeRelation(val symbol: String) {

enum class VariableConstraintKind {
PULL_UP,
PUSH_DOWN
PUSH_DOWN,
}

interface InferenceOrigin
Expand All @@ -52,14 +53,14 @@ sealed class InferenceConstraint

data class VariableConstraint(
val variable: InferenceVariable,
val kind: VariableConstraintKind
val kind: VariableConstraintKind,
) : InferenceConstraint()

data class RelationalConstraint(
val relation: TypeRelation,
val left: Type,
val right: Type,
val mutual: Boolean = false
val mutual: Boolean = false,
) : InferenceConstraint()

interface Bound {
Expand Down
Loading

0 comments on commit 9ff9251

Please sign in to comment.