Skip to content

Commit fa1d5cb

Browse files
vlad20012yopox
authored andcommitted
TY: introduce Ty.isEquivalentTo that should replace == for Tys
1 parent 9bc105b commit fa1d5cb

File tree

21 files changed

+154
-17
lines changed

21 files changed

+154
-17
lines changed

src/main/kotlin/org/rust/ide/annotator/RsErrorAnnotator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,10 +1072,10 @@ class RsErrorAnnotator : AnnotatorBase(), HighlightRangeExtension {
10721072
RsDiagnostic.InvalidStartAttrError.InvalidParam(params[0].typeReference ?: params[0], 0)
10731073
.addToHolder(holder)
10741074
}
1075-
if (params[1].typeReference?.type != TyPointer(
1075+
if (params[1].typeReference?.type?.isEquivalentTo(TyPointer(
10761076
TyPointer(TyInteger.U8.INSTANCE, Mutability.IMMUTABLE),
10771077
Mutability.IMMUTABLE
1078-
)
1078+
)) == false
10791079
) {
10801080
RsDiagnostic.InvalidStartAttrError.InvalidParam(params[1].typeReference ?: params[1], 1)
10811081
.addToHolder(holder)

src/main/kotlin/org/rust/ide/hints/type/RsChainMethodTypeHintsProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class RsChainMethodTypeHintsProvider : InlayHintsProvider<RsChainMethodTypeHints
8686
for (call in chain.dropLast(1)) {
8787
val type = normalizeType(call.type, lookup, iterator)
8888
if (type != TyUnknown && call.isLastOnLine) {
89-
if (settings.showSameConsecutiveTypes || type != lastType) {
89+
if (settings.showSameConsecutiveTypes || !type.isEquivalentTo(lastType)) {
9090
presentTypeForMethodCall(call, type)
9191
}
9292
lastType = type

src/main/kotlin/org/rust/ide/hints/type/RsTypeHintsPresentationFactory.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ class RsTypeHintsPresentationFactory(
291291
level + elementsCount > FOLDING_THRESHOLD
292292

293293
private fun isDefaultTypeParameter(argument: Ty, parameter: RsTypeParameter): Boolean =
294-
argument == parameter.typeReference?.type
294+
argument.isEquivalentTo(parameter.typeReference?.type)
295295

296296
private fun isDefaultTypeAlias(argument: Ty, alias: RsTypeAlias): Boolean =
297-
argument == alias.typeReference?.type
297+
argument.isEquivalentTo(alias.typeReference?.type)
298298

299299
private fun List<InlayPresentation>.join(separator: String = ""): InlayPresentation {
300300
if (separator.isEmpty()) {

src/main/kotlin/org/rust/ide/presentation/TypeRendering.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ private data class TypeRenderer(
275275
if (skipUnchangedDefaultTypeArguments && !nonDefaultParamFound) {
276276
if (parameter is RsTypeParameter &&
277277
parameter.typeReference != null &&
278-
parameter.typeReference?.type == subst[parameter]) {
278+
parameter.typeReference?.type?.isEquivalentTo(subst[parameter]) == true) {
279279
continue
280280
} else {
281281
nonDefaultParamFound = true

src/main/kotlin/org/rust/ide/utils/import/RsImportHelper.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ package org.rust.ide.utils.import
77

88
import org.rust.ide.settings.RsCodeInsightSettings
99
import org.rust.lang.core.psi.*
10-
import org.rust.lang.core.psi.ext.*
10+
import org.rust.lang.core.psi.ext.RsElement
11+
import org.rust.lang.core.psi.ext.RsMod
12+
import org.rust.lang.core.psi.ext.RsQualifiedNamedElement
13+
import org.rust.lang.core.psi.ext.typeParameters
1114
import org.rust.lang.core.resolve.TYPES
1215
import org.rust.lang.core.resolve.createProcessor
1316
import org.rust.lang.core.resolve.processNestedScopesUpwards
@@ -162,7 +165,7 @@ object RsImportHelper {
162165
if (skipUnchangedDefaultTypeArguments) {
163166
val filteredTypeArguments = ty.typeArguments
164167
.zip(ty.item.typeParameters)
165-
.dropLastWhile { (argumentTy, param) -> argumentTy == param.typeReference?.type }
168+
.dropLastWhile { (argumentTy, param) -> argumentTy.isEquivalentTo(param.typeReference?.type) }
166169
.map { (argumentTy, _) -> argumentTy }
167170
return ty.copy(typeArguments = filteredTypeArguments).superVisitWith(this)
168171
}

src/main/kotlin/org/rust/lang/core/psi/RsDefaultValueBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class RsDefaultValueBuilder(
161161
val binding = bindings[name] ?: return null
162162
val escapedName = fieldDecl.escapedName ?: return null
163163
return when {
164-
type == binding.type -> {
164+
type.isEquivalentTo(binding.type) -> {
165165
val field = psiFactory.createStructLiteralField(escapedName, psiFactory.createExpression(escapedName))
166166
RsFieldInitShorthandInspection.applyShorthandInit(field)
167167
field

src/main/kotlin/org/rust/lang/core/resolve/ImplLookup.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ sealed class TraitImplSource {
201201
*/
202202
data class ParamEnv(val callerBounds: List<TraitRef>) {
203203
fun boundsFor(ty: Ty): Sequence<BoundElement<RsTraitItem>> =
204-
callerBounds.asSequence().filter { it.selfTy == ty }.map { it.trait }
204+
callerBounds.asSequence().filter { it.selfTy.isEquivalentTo(ty) }.map { it.trait }
205205

206206
fun isEmpty(): Boolean = callerBounds.isEmpty()
207207

src/main/kotlin/org/rust/lang/core/types/ty/Ty.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ abstract class Ty(override val flags: TypeFlags = 0) : Kind, TypeFoldable<Ty> {
4949
* User visible string representation of a type
5050
*/
5151
final override fun toString(): String = render(useAliasNames = false, skipUnchangedDefaultTypeArguments = false)
52+
53+
/**
54+
* Use it instead of [equals] if you want to check that the types are the same from the Rust perspective.
55+
*
56+
* ```rust
57+
* type A = i32;
58+
* fn foo(a: A, b: i32) {
59+
* // Types `A` and `B` are *equivalent*, but not equal
60+
* }
61+
* ```
62+
*/
63+
fun isEquivalentTo(other: Ty?): Boolean = other != null && isEquivalentToInner(other)
64+
65+
protected open fun isEquivalentToInner(other: Ty): Boolean = equals(other)
5266
}
5367

5468
enum class Mutability {

src/main/kotlin/org/rust/lang/core/types/ty/TyAdt.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ data class TyAdt private constructor(
6969
override fun withAlias(aliasedBy: BoundElement<RsTypeAlias>): TyAdt =
7070
copy(aliasedBy = aliasedBy)
7171

72+
override fun isEquivalentToInner(other: Ty): Boolean {
73+
if (this === other) return true
74+
if (other !is TyAdt) return false
75+
76+
if (item != other.item) return false
77+
if (constArguments != other.constArguments) return false
78+
if (typeArguments.size != other.typeArguments.size) return false
79+
for (i in typeArguments.indices) {
80+
if (!typeArguments[i].isEquivalentTo(other.typeArguments[i])) return false
81+
}
82+
83+
return true
84+
}
85+
7286
companion object {
7387
fun valueOf(struct: RsStructOrEnumItemElement): TyAdt =
7488
TyAdt(

src/main/kotlin/org/rust/lang/core/types/ty/TyArray.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ data class TyArray(
2626
base.visitWith(visitor) || const.visitWith(visitor)
2727

2828
override fun withAlias(aliasedBy: BoundElement<RsTypeAlias>): Ty = copy(aliasedBy = aliasedBy)
29+
30+
override fun isEquivalentToInner(other: Ty): Boolean {
31+
if (this === other) return true
32+
if (other !is TyArray) return false
33+
34+
if (!base.isEquivalentTo(other.base)) return false
35+
if (const != other.const) return false
36+
37+
return true
38+
}
2939
}

0 commit comments

Comments
 (0)