Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RES: Fix resolve of enum variants in match pattern in some cases #9083

Merged
merged 1 commit into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main/kotlin/org/rust/lang/core/resolve/Namespace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ val RsNamedElement.namespaces: Set<Namespace> get() = when (this) {
is RsConstant -> VALUES
is RsFunction -> if (this.isProcMacroDef) MACROS else VALUES

is RsEnumVariant -> namespaces
is RsEnumVariant -> ENUM_VARIANT_NS

is RsStructItem -> if (blockFields == null) TYPES_N_VALUES else TYPES

Expand All @@ -64,7 +64,7 @@ fun StubElement<*>.getNamespaces(crate: Crate): Set<Namespace> = when (this) {
is RsConstantStub -> VALUES
is RsFunctionStub -> if (IS_PROC_MACRO_DEF_PROP.getByStub(this, crate)) MACROS else VALUES

is RsEnumVariantStub -> namespaces
is RsEnumVariantStub -> ENUM_VARIANT_NS

is RsStructItemStub -> if (blockFields == null) TYPES_N_VALUES else TYPES

Expand All @@ -75,8 +75,8 @@ fun StubElement<*>.getNamespaces(crate: Crate): Set<Namespace> = when (this) {
else -> TYPES_N_VALUES
}

inline val RsEnumVariant.namespaces: Set<Namespace> get() = if (blockFields === null) VALUES else TYPES
inline val RsEnumVariantStub.namespaces: Set<Namespace> get() = if (blockFields === null) VALUES else TYPES
// https://rust-lang.github.io/rfcs/0234-variants-namespace.html
val ENUM_VARIANT_NS: Set<Namespace> = TYPES_N_VALUES

val RsUseSpeck.namespaces: Set<Namespace>
get() = path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ fun VisItem.toPsi(info: RsModInfo, ns: Namespace): List<RsNamedElement> {
val containingEnums = containingModData.toRsEnum(info)
containingEnums.flatMap { containingEnum ->
containingEnum.variants
.filter { it.name == name && ns in it.namespaces && matchesIsEnabledByCfg(it, this) }
.filter { it.name == name && ns in ENUM_VARIANT_NS && matchesIsEnabledByCfg(it, this) }
}
} else {
val containingMods = containingModData.toScope(info)
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/rust/lang/core/resolve2/ModCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.rust.lang.core.psi.RsBlock
import org.rust.lang.core.psi.RsFile
import org.rust.lang.core.psi.RsFileBase
import org.rust.lang.core.psi.ext.*
import org.rust.lang.core.resolve.namespaces
import org.rust.lang.core.resolve.ENUM_VARIANT_NS
import org.rust.lang.core.resolve.processModDeclResolveVariants
import org.rust.lang.core.resolve2.util.DollarCrateHelper
import org.rust.lang.core.resolve2.util.DollarCrateMap
Expand Down Expand Up @@ -288,7 +288,7 @@ private class ModCollector(
val isVariantDeeplyEnabledByCfg = enumData.isDeeplyEnabledByCfg && variantPsi.isEnabledByCfgSelf(crate)
val variantVisibility = if (isVariantDeeplyEnabledByCfg) Visibility.Public else Visibility.CfgDisabled
val variant = VisItem(variantPath, variantVisibility)
val variantPerNs = PerNs.from(variant, variantPsi.namespaces)
val variantPerNs = PerNs.from(variant, ENUM_VARIANT_NS)
enumData.addVisibleItem(variantName, variantPerNs)
}
return enumData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class RsConstArgumentResolveTest : RsResolveTestBase() {
""")

fun `test in the case of ambiguity const argument is resolved to a type 3`() = checkByCode("""
enum E { S, B }
mod inner { pub fn S() {} }
struct S {}
//X

use E::*;
use inner::*;
struct Foo<const C: i32>();
type Bar = Foo<S>;
//^
Expand Down
11 changes: 11 additions & 0 deletions src/test/kotlin/org/rust/lang/core/resolve/RsResolveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,17 @@ class RsResolveTest : RsResolveTestBase() {
}
""")

fun `test enum variant in match pattern`() = checkByCode("""
pub enum E { A }
//X
use E::*;
fn func(x: E) {
match x {
A { .. } => {}
} //^
}
""")

fun `test enum variant with alias`() = checkByCode("""
enum E { A }
//X
Expand Down