Skip to content

Commit

Permalink
Type annotations in associated constant patterns.
Browse files Browse the repository at this point in the history
This commit adds support for respecting user type annotations with
associated constants in patterns.
  • Loading branch information
davidtwco committed Dec 30, 2018
1 parent b182a21 commit 4be7214
Showing 7 changed files with 86 additions and 13 deletions.
26 changes: 24 additions & 2 deletions src/librustc_mir/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
@@ -848,7 +848,28 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
};
match self.tcx.at(span).const_eval(self.param_env.and(cid)) {
Ok(value) => {
return self.const_to_pat(instance, value, id, span)
let pattern = self.const_to_pat(instance, value, id, span);
if !is_associated_const {
return pattern;
}

let user_provided_types = self.tables().user_provided_types();
return if let Some(u_ty) = user_provided_types.get(id) {
let user_ty = PatternTypeProjection::from_user_type(*u_ty);
Pattern {
span,
kind: Box::new(
PatternKind::AscribeUserType {
subpattern: pattern,
user_ty,
user_ty_span: span,
}
),
ty: value.ty,
}
} else {
pattern
}
},
Err(_) => {
self.tcx.sess.span_err(
@@ -938,7 +959,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
id: hir::HirId,
span: Span,
) -> Pattern<'tcx> {
debug!("const_to_pat: cv={:#?}", cv);
debug!("const_to_pat: cv={:#?} id={:?}", cv, id);
let adt_subpattern = |i, variant_opt| {
let field = Field::new(i);
let val = const_field(
@@ -956,6 +977,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
}
}).collect::<Vec<_>>()
};
debug!("const_to_pat: cv.ty={:?} span={:?}", cv.ty, span);
let kind = match cv.ty.sty {
ty::Float(_) => {
let id = self.tcx.hir().hir_to_node_id(id);
22 changes: 12 additions & 10 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
@@ -357,16 +357,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
})
}

pub fn resolve_ufcs(&self,
span: Span,
method_name: ast::Ident,
self_ty: Ty<'tcx>,
expr_id: ast::NodeId)
-> Result<Def, MethodError<'tcx>> {
debug!("resolve_ufcs: method_name={:?} self_ty={:?} expr_id={:?}",
method_name,
self_ty,
expr_id
pub fn resolve_ufcs(
&self,
span: Span,
method_name: ast::Ident,
self_ty: Ty<'tcx>,
expr_id: ast::NodeId
) -> Result<Def, MethodError<'tcx>> {
debug!(
"resolve_ufcs: method_name={:?} self_ty={:?} expr_id={:?}",
method_name, self_ty, expr_id,
);

let tcx = self.tcx;
@@ -375,6 +375,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
match self.probe_for_name(span, mode, method_name, IsSuggestion(false),
self_ty, expr_id, ProbeScope::TraitsInScope) {
Ok(pick) => {
debug!("resolve_ufcs: pick={:?}", pick);
if let Some(import_id) = pick.import_id {
let import_def_id = tcx.hir().local_def_id(import_id);
debug!("resolve_ufcs: used_trait_import: {:?}", import_def_id);
@@ -383,6 +384,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}

let def = pick.item.def();
debug!("resolve_ufcs: def={:?}", def);
tcx.check_stability(def.def_id(), Some(expr_id), span);

Ok(def)
2 changes: 2 additions & 0 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
@@ -4584,6 +4584,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
span: Span)
-> (Def, Option<Ty<'tcx>>, &'b [hir::PathSegment])
{
debug!("resolve_ty_and_def_ufcs: qpath={:?} node_id={:?} span={:?}", qpath, node_id, span);
let (ty, qself, item_segment) = match *qpath {
QPath::Resolved(ref opt_qself, ref path) => {
return (path.def,
@@ -5104,6 +5105,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
Def::Method(def_id) |
Def::AssociatedConst(def_id) => {
let container = tcx.associated_item(def_id).container;
debug!("instantiate_value_path: def={:?} container={:?}", def, container);
match container {
ty::TraitContainer(trait_did) => {
callee::check_legal_trait_for_method_call(tcx, span, trait_did)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-pass
#![allow(dead_code)]
#![allow(dead_code, unreachable_patterns)]

struct Foo;

15 changes: 15 additions & 0 deletions src/test/ui/issue-55511.nll.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0597]: `a` does not live long enough
--> $DIR/issue-55511.rs:13:28
|
LL | let b = Some(Cell::new(&a));
| ^^ borrowed value does not live long enough
LL | match b {
LL | <() as Foo<'static>>::C => { }
| ----------------------- type annotation requires that `a` is borrowed for `'static`
...
LL | }
| - `a` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
18 changes: 18 additions & 0 deletions src/test/ui/issue-55511.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::cell::Cell;

trait Foo<'a> {
const C: Option<Cell<&'a u32>>;
}

impl<'a, T> Foo<'a> for T {
const C: Option<Cell<&'a u32>> = None;
}

fn main() {
let a = 22;
let b = Some(Cell::new(&a));
match b {
<() as Foo<'static>>::C => { }
_ => { }
}
}
14 changes: 14 additions & 0 deletions src/test/ui/issue-55511.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0597]: `a` does not live long enough
--> $DIR/issue-55511.rs:13:29
|
LL | let b = Some(Cell::new(&a));
| ^ borrowed value does not live long enough
...
LL | }
| - borrowed value only lives until here
|
= note: borrowed value must be valid for the static lifetime...

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.

0 comments on commit 4be7214

Please sign in to comment.