support user annotations in fns, methods

This commit is contained in:
Niko Matsakis 2018-08-09 06:58:28 -04:00
parent 5f1643d2ea
commit 2d1d3fef62
7 changed files with 339 additions and 26 deletions
src
librustc
hir
infer/canonical
librustc_mir/hair/cx
test/ui/nll/user-annotations

View File

@ -68,7 +68,7 @@ pub enum Def {
Const(DefId),
Static(DefId, bool /* is_mutbl */),
StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
VariantCtor(DefId, CtorKind),
VariantCtor(DefId, CtorKind), // DefId refers to the enum variant
Method(DefId),
AssociatedConst(DefId),

View File

@ -188,6 +188,36 @@ impl<'tcx, R> Canonical<'tcx, QueryResult<'tcx, R>> {
}
}
impl<'gcx, V> Canonical<'gcx, V> {
/// Allows you to map the `value` of a canonical while keeping the
/// same set of bound variables.
///
/// **WARNING:** This function is very easy to mis-use, hence the
/// name! In particular, the new value `W` must use all **the
/// same type/region variables** in **precisely the same order**
/// as the original! (The ordering is defined by the
/// `TypeFoldable` implementation of the type in question.)
///
/// An example of a **correct** use of this:
///
/// ```rust,ignore
/// let a: Canonical<'_, T> = ...;
/// let b: Canonical<'_, (T,)> = a.unchecked_map(|v| (v, ));
/// ```
///
/// An example of an **incorrect** use of this:
///
/// ```rust,ignore
/// let a: Canonical<'tcx, T> = ...;
/// let ty: Ty<'tcx> = ...;
/// let b: Canonical<'tcx, (T, Ty<'tcx>)> = a.unchecked_map(|v| (v, ty));
/// ```
pub fn unchecked_map<W>(self, map_op: impl FnOnce(V) -> W) -> Canonical<'gcx, W> {
let Canonical { variables, value } = self;
Canonical { variables, value: map_op(value) }
}
}
pub type QueryRegionConstraint<'tcx> = ty::Binder<ty::OutlivesPredicate<Kind<'tcx>, Region<'tcx>>>;
impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {

View File

@ -265,6 +265,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}
})
.collect();
// FIXME(#47184) -- user given type annot on ADTs
ExprKind::Adt {
adt_def,
substs,
@ -423,6 +424,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
ty::Adt(adt, substs) => {
match adt.adt_kind() {
AdtKind::Struct | AdtKind::Union => {
// FIXME(#47184) -- user given type annot on ADTs
ExprKind::Adt {
adt_def: adt,
variant_index: 0,
@ -448,6 +450,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
assert!(base.is_none());
let index = adt.variant_index_with_id(variant_id);
// FIXME(#47184) -- user given type annot on ADTs
ExprKind::Adt {
adt_def: adt,
variant_index: index,
@ -686,18 +689,70 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}
}
fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
expr: &hir::Expr,
custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>)
-> Expr<'tcx> {
fn user_annotated_ty_for_def(
cx: &mut Cx<'a, 'gcx, 'tcx>,
hir_id: hir::HirId,
def: &Def,
) -> Option<CanonicalTy<'tcx>> {
let user_substs = cx.tables().user_substs(hir_id)?;
Some(match def {
// A reference to something callable -- e.g., a fn, method, or
// a tuple-struct or tuple-variant. This has the type of a
// `Fn` but with the user-given substitutions.
Def::Fn(_) |
Def::Method(_) |
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) =>
user_substs.unchecked_map(|user_substs| {
// Here, we just pair a `DefId` with the
// `user_substs`, so no new types etc are introduced.
cx.tcx().mk_fn_def(def.def_id(), user_substs)
}),
Def::Const(_def_id) |
Def::AssociatedConst(_def_id) =>
bug!("unimplemented"),
// A unit struct/variant which is used as a value (e.g.,
// `None`). This has the type of the enum/struct that defines
// this variant -- but with the substitutions given by the
// user.
Def::StructCtor(_def_id, CtorKind::Const) |
Def::VariantCtor(_def_id, CtorKind::Const) =>
match &cx.tables().node_id_to_type(hir_id).sty {
ty::TyAdt(adt_def, _) =>
user_substs.unchecked_map(|user_substs| {
// Here, we just pair an `AdtDef` with the
// `user_substs`, so no new types etc are introduced.
cx.tcx().mk_adt(adt_def, user_substs)
}),
sty => bug!("unexpected sty: {:?}", sty),
},
_ =>
bug!("user_annotated_ty_for_def: unexpected def {:?} at {:?}", def, hir_id)
})
}
fn method_callee<'a, 'gcx, 'tcx>(
cx: &mut Cx<'a, 'gcx, 'tcx>,
expr: &hir::Expr,
overloaded_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
) -> Expr<'tcx> {
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let (def_id, substs) = custom_callee.unwrap_or_else(|| {
if let Some(def) = cx.tables().type_dependent_defs().get(expr.hir_id) {
(def.def_id(), cx.tables().node_substs(expr.hir_id))
} else {
span_bug!(expr.span, "no type-dependent def for method callee")
let (def_id, substs, user_ty) = match overloaded_callee {
Some((def_id, substs)) => (def_id, substs, None),
None => {
let type_dependent_defs = cx.tables().type_dependent_defs();
let def = type_dependent_defs
.get(expr.hir_id)
.unwrap_or_else(|| {
span_bug!(expr.span, "no type-dependent def for method callee")
});
let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, def);
(def.def_id(), cx.tables().node_substs(expr.hir_id), user_ty)
}
});
};
let ty = cx.tcx().mk_fn_def(def_id, substs);
Expr {
temp_lifetime,
@ -705,7 +760,7 @@ fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
span: expr.span,
kind: ExprKind::Literal {
literal: ty::Const::zero_sized(cx.tcx(), ty),
user_ty: None, // TODO
user_ty,
},
}
}
@ -756,12 +811,15 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
Def::Fn(_) |
Def::Method(_) |
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) => ExprKind::Literal {
literal: ty::Const::zero_sized(
cx.tcx,
cx.tables().node_id_to_type(expr.hir_id),
),
user_ty: None, // TODO
Def::VariantCtor(_, CtorKind::Fn) => {
let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, &def);
ExprKind::Literal {
literal: ty::Const::zero_sized(
cx.tcx,
cx.tables().node_id_to_type(expr.hir_id),
),
user_ty,
}
},
Def::Const(def_id) |
@ -772,11 +830,12 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
substs,
cx.tables().node_id_to_type(expr.hir_id),
),
user_ty: None, // TODO?
user_ty: None, // FIXME(#47184) -- user given type annot on constants
},
Def::StructCtor(def_id, CtorKind::Const) |
Def::VariantCtor(def_id, CtorKind::Const) => {
// FIXME(#47184) -- user given type annot on ADTs
match cx.tables().node_id_to_type(expr.hir_id).sty {
// A unit struct/variant which is used as a value.
// We return a completely different ExprKind here to account for this special case.
@ -963,12 +1022,13 @@ fn overloaded_operator<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
}
}
fn overloaded_place<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
expr: &'tcx hir::Expr,
place_ty: Ty<'tcx>,
custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
args: Vec<ExprRef<'tcx>>)
-> ExprKind<'tcx> {
fn overloaded_place<'a, 'gcx, 'tcx>(
cx: &mut Cx<'a, 'gcx, 'tcx>,
expr: &'tcx hir::Expr,
place_ty: Ty<'tcx>,
overloaded_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
args: Vec<ExprRef<'tcx>>,
) -> ExprKind<'tcx> {
// For an overloaded *x or x[y] expression of type T, the method
// call returns an &T and we must add the deref so that the types
// line up (this is because `*x` and `x[y]` represent places):
@ -993,7 +1053,7 @@ fn overloaded_place<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
// construct the complete expression `foo()` for the overloaded call,
// which will yield the &T type
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
let fun = method_callee(cx, expr, custom_callee);
let fun = method_callee(cx, expr, overloaded_callee);
let ref_expr = Expr {
temp_lifetime,
ty: ref_ty,

View File

@ -0,0 +1,60 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Unit test for the "user substitutions" that are annotated on each
// node.
#![feature(nll)]
fn some_fn<T>(arg: T) { }
fn no_annot() {
let c = 66;
some_fn(&c); // OK
}
fn annot_underscore() {
let c = 66;
some_fn::<_>(&c); // OK
}
fn annot_reference_any_lifetime() {
let c = 66;
some_fn::<&u32>(&c); // OK
}
fn annot_reference_static_lifetime() {
let c = 66;
some_fn::<&'static u32>(&c); //~ ERROR
}
fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
let c = 66;
some_fn::<&'a u32>(&c); //~ ERROR
}
fn annot_reference_named_lifetime_ok<'a>(c: &'a u32) {
some_fn::<&'a u32>(c);
}
fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
let _closure = || {
let c = 66;
some_fn::<&'a u32>(&c); //~ ERROR
};
}
fn annot_reference_named_lifetime_in_closure_ok<'a>(c: &'a u32) {
let _closure = || {
some_fn::<&'a u32>(c);
};
}
fn main() { }

View File

@ -0,0 +1,41 @@
error[E0597]: `c` does not live long enough
--> $DIR/fns.rs:35:29
|
LL | some_fn::<&'static u32>(&c); //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `c` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: `c` does not live long enough
--> $DIR/fns.rs:40:24
|
LL | some_fn::<&'a u32>(&c); //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `c` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 38:35...
--> $DIR/fns.rs:38:35
|
LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
| ^^
error[E0597]: `c` does not live long enough
--> $DIR/fns.rs:50:28
|
LL | some_fn::<&'a u32>(&c); //~ ERROR
| ^^ borrowed value does not live long enough
LL | };
| - `c` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 47:46...
--> $DIR/fns.rs:47:46
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| ^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0597`.

View File

@ -0,0 +1,81 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Unit test for the "user substitutions" that are annotated on each
// node.
#![feature(nll)]
trait Bazoom<T> {
fn method<U>(&self, arg: T, arg2: U) { }
}
impl<T, U> Bazoom<U> for T {
}
fn no_annot() {
let a = 22;
let b = 44;
let c = 66;
a.method(b, &c); // OK
}
fn annot_underscore() {
let a = 22;
let b = 44;
let c = 66;
a.method::<_>(b, &c); // OK
}
fn annot_reference_any_lifetime() {
let a = 22;
let b = 44;
let c = 66;
a.method::<&u32>(b, &c); // OK
}
fn annot_reference_static_lifetime() {
let a = 22;
let b = 44;
let c = 66;
a.method::<&'static u32>(b, &c); //~ ERROR
}
fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
let a = 22;
let b = 44;
let c = 66;
a.method::<&'a u32>(b, &c); //~ ERROR
}
fn annot_reference_named_lifetime_ok<'a>(c: &'a u32) {
let a = 22;
let b = 44;
a.method::<&'a u32>(b, c);
}
fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
let a = 22;
let b = 44;
let _closure = || {
let c = 66;
a.method::<&'a u32>(b, &c); //~ ERROR
};
}
fn annot_reference_named_lifetime_in_closure_ok<'a>(c: &'a u32) {
let a = 22;
let b = 44;
let _closure = || {
a.method::<&'a u32>(b, c);
};
}
fn main() { }

View File

@ -0,0 +1,41 @@
error[E0597]: `c` does not live long enough
--> $DIR/method-call.rs:48:34
|
LL | a.method::<&'static u32>(b, &c); //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `c` dropped here while still borrowed
|
= note: borrowed value must be valid for the static lifetime...
error[E0597]: `c` does not live long enough
--> $DIR/method-call.rs:55:29
|
LL | a.method::<&'a u32>(b, &c); //~ ERROR
| ^^ borrowed value does not live long enough
LL | }
| - `c` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 51:35...
--> $DIR/method-call.rs:51:35
|
LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
| ^^
error[E0597]: `c` does not live long enough
--> $DIR/method-call.rs:69:33
|
LL | a.method::<&'a u32>(b, &c); //~ ERROR
| ^^ borrowed value does not live long enough
LL | };
| - `c` dropped here while still borrowed
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 64:46...
--> $DIR/method-call.rs:64:46
|
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
| ^^
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0597`.