Skip to content

Commit 4a91d8c

Browse files
committed
Allow T::C syntax in match patterns to refer to trait-assosociated constants.
1 parent cadc67e commit 4a91d8c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/librustc_resolve/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,15 +2519,18 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
25192519
// If anything ends up here entirely resolved,
25202520
// it's an error. If anything ends up here
25212521
// partially resolved, that's OK, because it may
2522-
// be a `T::CONST` that typeck will resolve to
2523-
// an inherent impl.
2522+
// be a `T::CONST` that typeck will resolve.
25242523
if path_res.depth == 0 {
25252524
self.resolve_error(
25262525
path.span,
25272526
&format!("`{}` is not an enum variant, struct or const",
25282527
token::get_ident(
25292528
path.segments.last().unwrap().identifier)));
25302529
} else {
2530+
let const_name = path.segments.last().unwrap()
2531+
.identifier.name;
2532+
let traits = self.get_traits_containing_item(const_name);
2533+
self.trait_map.insert(pattern.id, traits);
25312534
self.record_def(pattern.id, path_res);
25322535
}
25332536
}

src/test/run-pass/associated-const-match-patterns.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ fn main() {
4141
_ => false,
4242
});
4343
// Trait impl
44+
assert!(match Bar::Var1 {
45+
Foo::THEBAR => true,
46+
_ => false,
47+
});
4448
assert!(match Bar::Var1 {
4549
<Foo>::THEBAR => true,
4650
_ => false,

0 commit comments

Comments
 (0)