From 2d1d3fef6205276e0123dea96cc346d1da7df6d0 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 9 Aug 2018 06:58:28 -0400 Subject: [PATCH] support user annotations in fns, methods --- src/librustc/hir/def.rs | 2 +- src/librustc/infer/canonical/mod.rs | 30 +++++ src/librustc_mir/hair/cx/expr.rs | 110 ++++++++++++++---- src/test/ui/nll/user-annotations/fns.rs | 60 ++++++++++ src/test/ui/nll/user-annotations/fns.stderr | 41 +++++++ .../ui/nll/user-annotations/method-call.rs | 81 +++++++++++++ .../nll/user-annotations/method-call.stderr | 41 +++++++ 7 files changed, 339 insertions(+), 26 deletions(-) create mode 100644 src/test/ui/nll/user-annotations/fns.rs create mode 100644 src/test/ui/nll/user-annotations/fns.stderr create mode 100644 src/test/ui/nll/user-annotations/method-call.rs create mode 100644 src/test/ui/nll/user-annotations/method-call.stderr diff --git a/src/librustc/hir/def.rs b/src/librustc/hir/def.rs index 65146f2de84..b10f4785f16 100644 --- a/src/librustc/hir/def.rs +++ b/src/librustc/hir/def.rs @@ -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), diff --git a/src/librustc/infer/canonical/mod.rs b/src/librustc/infer/canonical/mod.rs index 2e57ef7b17d..8a2332cee2e 100644 --- a/src/librustc/infer/canonical/mod.rs +++ b/src/librustc/infer/canonical/mod.rs @@ -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(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, Region<'tcx>>>; impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> { diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index 00b2bcd486b..9a771e3d451 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -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> { + 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>) - -> 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>, +) -> 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, diff --git a/src/test/ui/nll/user-annotations/fns.rs b/src/test/ui/nll/user-annotations/fns.rs new file mode 100644 index 00000000000..4f26d5422b0 --- /dev/null +++ b/src/test/ui/nll/user-annotations/fns.rs @@ -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 or the MIT license +// , 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(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() { } diff --git a/src/test/ui/nll/user-annotations/fns.stderr b/src/test/ui/nll/user-annotations/fns.stderr new file mode 100644 index 00000000000..b6ef336567c --- /dev/null +++ b/src/test/ui/nll/user-annotations/fns.stderr @@ -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`. diff --git a/src/test/ui/nll/user-annotations/method-call.rs b/src/test/ui/nll/user-annotations/method-call.rs new file mode 100644 index 00000000000..9a03679bef0 --- /dev/null +++ b/src/test/ui/nll/user-annotations/method-call.rs @@ -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 or the MIT license +// , 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 { + fn method(&self, arg: T, arg2: U) { } +} + +impl Bazoom 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() { } diff --git a/src/test/ui/nll/user-annotations/method-call.stderr b/src/test/ui/nll/user-annotations/method-call.stderr new file mode 100644 index 00000000000..f1c7ff1e0fb --- /dev/null +++ b/src/test/ui/nll/user-annotations/method-call.stderr @@ -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`.