Skip to content

Commit f87bc6a

Browse files
committed
Make destructuring trait reference work
Closes #15031.
1 parent 0ae4b97 commit f87bc6a

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

src/librustc/middle/typeck/check/_match.rs

+26-10
Original file line numberDiff line numberDiff line change
@@ -726,22 +726,38 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
726726
}
727727

728728
// Helper function to check gc, box and & patterns
729-
pub fn check_pointer_pat(pcx: &pat_ctxt,
730-
pointer_kind: PointerKind,
731-
inner: &ast::Pat,
732-
pat_id: ast::NodeId,
733-
span: Span,
734-
expected: ty::t) {
729+
fn check_pointer_pat(pcx: &pat_ctxt,
730+
pointer_kind: PointerKind,
731+
inner: &ast::Pat,
732+
pat_id: ast::NodeId,
733+
span: Span,
734+
expected: ty::t) {
735735
let fcx = pcx.fcx;
736+
let tcx = fcx.ccx.tcx;
736737
let check_inner: |ty::t| = |e_inner| {
737-
check_pat(pcx, inner, e_inner);
738-
fcx.write_ty(pat_id, expected);
738+
match ty::get(e_inner).sty {
739+
ty::ty_trait(_) if pat_is_binding(&tcx.def_map, inner) => {
740+
// This is "x = SomeTrait" being reduced from
741+
// "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
742+
check_pat(pcx, inner, ty::mk_err());
743+
tcx.sess.span_err(
744+
span,
745+
format!("type `{}` cannot be dereferenced",
746+
fcx.infcx().ty_to_str(expected)).as_slice());
747+
fcx.write_error(pat_id);
748+
}
749+
_ => {
750+
check_pat(pcx, inner, e_inner);
751+
fcx.write_ty(pat_id, expected);
752+
}
753+
}
739754
};
755+
740756
match *structure_of(fcx, span, expected) {
741-
ty::ty_uniq(e_inner) if pointer_kind == Send && !ty::type_is_trait(e_inner) => {
757+
ty::ty_uniq(e_inner) if pointer_kind == Send => {
742758
check_inner(e_inner);
743759
}
744-
ty::ty_rptr(_, e_inner) if pointer_kind == Borrowed && !ty::type_is_trait(e_inner.ty) => {
760+
ty::ty_rptr(_, e_inner) if pointer_kind == Borrowed => {
745761
check_inner(e_inner.ty);
746762
}
747763
_ => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// The regression test for #15031 to make sure destructuring trait
12+
// reference work properly.
13+
14+
trait T {}
15+
impl T for int {}
16+
17+
fn main() {
18+
// For an expression of the form:
19+
//
20+
// let &...&x = &..&SomeTrait;
21+
//
22+
// Say we have n `&` at the left hand and m `&` right hand, then:
23+
// if n < m, we are golden;
24+
// if n == m, it's a derefing non-derefable type error;
25+
// if n > m, it's a type mismatch error.
26+
27+
// n < m
28+
let &x = &(&1 as &T);
29+
let &x = &&(&1 as &T);
30+
let &&x = &&(&1 as &T);
31+
32+
// n == m
33+
let &x = &1 as &T; //~ ERROR cannot be dereferenced
34+
let &&x = &(&1 as &T); //~ ERROR cannot be dereferenced
35+
let box x = box 1 as Box<T>; //~ ERROR cannot be dereferenced
36+
37+
// n > m
38+
let &&x = &1 as &T; //~ ERROR found an `&`-pointer pattern
39+
let &&&x = &(&1 as &T); //~ ERROR found an `&`-pointer pattern
40+
let box box x = box 1 as Box<T>; //~ ERROR found a box pattern
41+
}

src/test/compile-fail/issue-4972.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum TraitWrapper {
1717

1818
fn get_tw_map<'lt>(tw: &'lt TraitWrapper) -> &'lt MyTrait {
1919
match *tw {
20-
A(box ref map) => map, //~ ERROR found a box pattern
20+
A(box ref map) => map, //~ ERROR cannot be dereferenced
2121
}
2222
}
2323

0 commit comments

Comments
 (0)