Skip to content

Commit 2d1d3fe

Browse files
committed
support user annotations in fns, methods
1 parent 5f1643d commit 2d1d3fe

File tree

7 files changed

+339
-26
lines changed

7 files changed

+339
-26
lines changed

src/librustc/hir/def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub enum Def {
6868
Const(DefId),
6969
Static(DefId, bool /* is_mutbl */),
7070
StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
71-
VariantCtor(DefId, CtorKind),
71+
VariantCtor(DefId, CtorKind), // DefId refers to the enum variant
7272
Method(DefId),
7373
AssociatedConst(DefId),
7474

src/librustc/infer/canonical/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,36 @@ impl<'tcx, R> Canonical<'tcx, QueryResult<'tcx, R>> {
188188
}
189189
}
190190

191+
impl<'gcx, V> Canonical<'gcx, V> {
192+
/// Allows you to map the `value` of a canonical while keeping the
193+
/// same set of bound variables.
194+
///
195+
/// **WARNING:** This function is very easy to mis-use, hence the
196+
/// name! In particular, the new value `W` must use all **the
197+
/// same type/region variables** in **precisely the same order**
198+
/// as the original! (The ordering is defined by the
199+
/// `TypeFoldable` implementation of the type in question.)
200+
///
201+
/// An example of a **correct** use of this:
202+
///
203+
/// ```rust,ignore
204+
/// let a: Canonical<'_, T> = ...;
205+
/// let b: Canonical<'_, (T,)> = a.unchecked_map(|v| (v, ));
206+
/// ```
207+
///
208+
/// An example of an **incorrect** use of this:
209+
///
210+
/// ```rust,ignore
211+
/// let a: Canonical<'tcx, T> = ...;
212+
/// let ty: Ty<'tcx> = ...;
213+
/// let b: Canonical<'tcx, (T, Ty<'tcx>)> = a.unchecked_map(|v| (v, ty));
214+
/// ```
215+
pub fn unchecked_map<W>(self, map_op: impl FnOnce(V) -> W) -> Canonical<'gcx, W> {
216+
let Canonical { variables, value } = self;
217+
Canonical { variables, value: map_op(value) }
218+
}
219+
}
220+
191221
pub type QueryRegionConstraint<'tcx> = ty::Binder<ty::OutlivesPredicate<Kind<'tcx>, Region<'tcx>>>;
192222

193223
impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {

src/librustc_mir/hair/cx/expr.rs

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
265265
}
266266
})
267267
.collect();
268+
// FIXME(#47184) -- user given type annot on ADTs
268269
ExprKind::Adt {
269270
adt_def,
270271
substs,
@@ -423,6 +424,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
423424
ty::Adt(adt, substs) => {
424425
match adt.adt_kind() {
425426
AdtKind::Struct | AdtKind::Union => {
427+
// FIXME(#47184) -- user given type annot on ADTs
426428
ExprKind::Adt {
427429
adt_def: adt,
428430
variant_index: 0,
@@ -448,6 +450,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
448450
assert!(base.is_none());
449451

450452
let index = adt.variant_index_with_id(variant_id);
453+
// FIXME(#47184) -- user given type annot on ADTs
451454
ExprKind::Adt {
452455
adt_def: adt,
453456
variant_index: index,
@@ -686,26 +689,78 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
686689
}
687690
}
688691

689-
fn method_callee<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
690-
expr: &hir::Expr,
691-
custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>)
692-
-> Expr<'tcx> {
692+
fn user_annotated_ty_for_def(
693+
cx: &mut Cx<'a, 'gcx, 'tcx>,
694+
hir_id: hir::HirId,
695+
def: &Def,
696+
) -> Option<CanonicalTy<'tcx>> {
697+
let user_substs = cx.tables().user_substs(hir_id)?;
698+
Some(match def {
699+
// A reference to something callable -- e.g., a fn, method, or
700+
// a tuple-struct or tuple-variant. This has the type of a
701+
// `Fn` but with the user-given substitutions.
702+
Def::Fn(_) |
703+
Def::Method(_) |
704+
Def::StructCtor(_, CtorKind::Fn) |
705+
Def::VariantCtor(_, CtorKind::Fn) =>
706+
user_substs.unchecked_map(|user_substs| {
707+
// Here, we just pair a `DefId` with the
708+
// `user_substs`, so no new types etc are introduced.
709+
cx.tcx().mk_fn_def(def.def_id(), user_substs)
710+
}),
711+
712+
Def::Const(_def_id) |
713+
Def::AssociatedConst(_def_id) =>
714+
bug!("unimplemented"),
715+
716+
// A unit struct/variant which is used as a value (e.g.,
717+
// `None`). This has the type of the enum/struct that defines
718+
// this variant -- but with the substitutions given by the
719+
// user.
720+
Def::StructCtor(_def_id, CtorKind::Const) |
721+
Def::VariantCtor(_def_id, CtorKind::Const) =>
722+
match &cx.tables().node_id_to_type(hir_id).sty {
723+
ty::TyAdt(adt_def, _) =>
724+
user_substs.unchecked_map(|user_substs| {
725+
// Here, we just pair an `AdtDef` with the
726+
// `user_substs`, so no new types etc are introduced.
727+
cx.tcx().mk_adt(adt_def, user_substs)
728+
}),
729+
sty => bug!("unexpected sty: {:?}", sty),
730+
},
731+
732+
_ =>
733+
bug!("user_annotated_ty_for_def: unexpected def {:?} at {:?}", def, hir_id)
734+
})
735+
}
736+
737+
fn method_callee<'a, 'gcx, 'tcx>(
738+
cx: &mut Cx<'a, 'gcx, 'tcx>,
739+
expr: &hir::Expr,
740+
overloaded_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
741+
) -> Expr<'tcx> {
693742
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
694-
let (def_id, substs) = custom_callee.unwrap_or_else(|| {
695-
if let Some(def) = cx.tables().type_dependent_defs().get(expr.hir_id) {
696-
(def.def_id(), cx.tables().node_substs(expr.hir_id))
697-
} else {
698-
span_bug!(expr.span, "no type-dependent def for method callee")
743+
let (def_id, substs, user_ty) = match overloaded_callee {
744+
Some((def_id, substs)) => (def_id, substs, None),
745+
None => {
746+
let type_dependent_defs = cx.tables().type_dependent_defs();
747+
let def = type_dependent_defs
748+
.get(expr.hir_id)
749+
.unwrap_or_else(|| {
750+
span_bug!(expr.span, "no type-dependent def for method callee")
751+
});
752+
let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, def);
753+
(def.def_id(), cx.tables().node_substs(expr.hir_id), user_ty)
699754
}
700-
});
755+
};
701756
let ty = cx.tcx().mk_fn_def(def_id, substs);
702757
Expr {
703758
temp_lifetime,
704759
ty,
705760
span: expr.span,
706761
kind: ExprKind::Literal {
707762
literal: ty::Const::zero_sized(cx.tcx(), ty),
708-
user_ty: None, // TODO
763+
user_ty,
709764
},
710765
}
711766
}
@@ -756,12 +811,15 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
756811
Def::Fn(_) |
757812
Def::Method(_) |
758813
Def::StructCtor(_, CtorKind::Fn) |
759-
Def::VariantCtor(_, CtorKind::Fn) => ExprKind::Literal {
760-
literal: ty::Const::zero_sized(
761-
cx.tcx,
762-
cx.tables().node_id_to_type(expr.hir_id),
763-
),
764-
user_ty: None, // TODO
814+
Def::VariantCtor(_, CtorKind::Fn) => {
815+
let user_ty = user_annotated_ty_for_def(cx, expr.hir_id, &def);
816+
ExprKind::Literal {
817+
literal: ty::Const::zero_sized(
818+
cx.tcx,
819+
cx.tables().node_id_to_type(expr.hir_id),
820+
),
821+
user_ty,
822+
}
765823
},
766824

767825
Def::Const(def_id) |
@@ -772,11 +830,12 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
772830
substs,
773831
cx.tables().node_id_to_type(expr.hir_id),
774832
),
775-
user_ty: None, // TODO?
833+
user_ty: None, // FIXME(#47184) -- user given type annot on constants
776834
},
777835

778836
Def::StructCtor(def_id, CtorKind::Const) |
779837
Def::VariantCtor(def_id, CtorKind::Const) => {
838+
// FIXME(#47184) -- user given type annot on ADTs
780839
match cx.tables().node_id_to_type(expr.hir_id).sty {
781840
// A unit struct/variant which is used as a value.
782841
// 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>,
9631022
}
9641023
}
9651024

966-
fn overloaded_place<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
967-
expr: &'tcx hir::Expr,
968-
place_ty: Ty<'tcx>,
969-
custom_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
970-
args: Vec<ExprRef<'tcx>>)
971-
-> ExprKind<'tcx> {
1025+
fn overloaded_place<'a, 'gcx, 'tcx>(
1026+
cx: &mut Cx<'a, 'gcx, 'tcx>,
1027+
expr: &'tcx hir::Expr,
1028+
place_ty: Ty<'tcx>,
1029+
overloaded_callee: Option<(DefId, &'tcx Substs<'tcx>)>,
1030+
args: Vec<ExprRef<'tcx>>,
1031+
) -> ExprKind<'tcx> {
9721032
// For an overloaded *x or x[y] expression of type T, the method
9731033
// call returns an &T and we must add the deref so that the types
9741034
// 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>,
9931053
// construct the complete expression `foo()` for the overloaded call,
9941054
// which will yield the &T type
9951055
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
996-
let fun = method_callee(cx, expr, custom_callee);
1056+
let fun = method_callee(cx, expr, overloaded_callee);
9971057
let ref_expr = Expr {
9981058
temp_lifetime,
9991059
ty: ref_ty,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2017 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+
// Unit test for the "user substitutions" that are annotated on each
12+
// node.
13+
14+
#![feature(nll)]
15+
16+
fn some_fn<T>(arg: T) { }
17+
18+
fn no_annot() {
19+
let c = 66;
20+
some_fn(&c); // OK
21+
}
22+
23+
fn annot_underscore() {
24+
let c = 66;
25+
some_fn::<_>(&c); // OK
26+
}
27+
28+
fn annot_reference_any_lifetime() {
29+
let c = 66;
30+
some_fn::<&u32>(&c); // OK
31+
}
32+
33+
fn annot_reference_static_lifetime() {
34+
let c = 66;
35+
some_fn::<&'static u32>(&c); //~ ERROR
36+
}
37+
38+
fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
39+
let c = 66;
40+
some_fn::<&'a u32>(&c); //~ ERROR
41+
}
42+
43+
fn annot_reference_named_lifetime_ok<'a>(c: &'a u32) {
44+
some_fn::<&'a u32>(c);
45+
}
46+
47+
fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
48+
let _closure = || {
49+
let c = 66;
50+
some_fn::<&'a u32>(&c); //~ ERROR
51+
};
52+
}
53+
54+
fn annot_reference_named_lifetime_in_closure_ok<'a>(c: &'a u32) {
55+
let _closure = || {
56+
some_fn::<&'a u32>(c);
57+
};
58+
}
59+
60+
fn main() { }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0597]: `c` does not live long enough
2+
--> $DIR/fns.rs:35:29
3+
|
4+
LL | some_fn::<&'static u32>(&c); //~ ERROR
5+
| ^^ borrowed value does not live long enough
6+
LL | }
7+
| - `c` dropped here while still borrowed
8+
|
9+
= note: borrowed value must be valid for the static lifetime...
10+
11+
error[E0597]: `c` does not live long enough
12+
--> $DIR/fns.rs:40:24
13+
|
14+
LL | some_fn::<&'a u32>(&c); //~ ERROR
15+
| ^^ borrowed value does not live long enough
16+
LL | }
17+
| - `c` dropped here while still borrowed
18+
|
19+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 38:35...
20+
--> $DIR/fns.rs:38:35
21+
|
22+
LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
23+
| ^^
24+
25+
error[E0597]: `c` does not live long enough
26+
--> $DIR/fns.rs:50:28
27+
|
28+
LL | some_fn::<&'a u32>(&c); //~ ERROR
29+
| ^^ borrowed value does not live long enough
30+
LL | };
31+
| - `c` dropped here while still borrowed
32+
|
33+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 47:46...
34+
--> $DIR/fns.rs:47:46
35+
|
36+
LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
37+
| ^^
38+
39+
error: aborting due to 3 previous errors
40+
41+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)