Skip to content

Commit

Permalink
propagate user-type annotation for constants in expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Oct 19, 2018
1 parent ebdfda6 commit e94959b
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,13 +771,11 @@ fn user_substs_applied_to_def(
Def::Fn(_) |
Def::Method(_) |
Def::StructCtor(_, CtorKind::Fn) |
Def::VariantCtor(_, CtorKind::Fn) =>
Def::VariantCtor(_, CtorKind::Fn) |
Def::Const(_) |
Def::AssociatedConst(_) =>
Some(UserTypeAnnotation::TypeOf(def.def_id(), cx.tables().user_substs(hir_id)?)),

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
Expand Down Expand Up @@ -889,14 +887,17 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
},

Def::Const(def_id) |
Def::AssociatedConst(def_id) => ExprKind::Literal {
literal: ty::Const::unevaluated(
cx.tcx,
def_id,
substs,
cx.tables().node_id_to_type(expr.hir_id),
),
user_ty: None, // FIXME(#47184) -- user given type annot on constants
Def::AssociatedConst(def_id) => {
let user_ty = user_substs_applied_to_def(cx, expr.hir_id, &def);
ExprKind::Literal {
literal: ty::Const::unevaluated(
cx.tcx,
def_id,
substs,
cx.tables().node_id_to_type(expr.hir_id),
),
user_ty,
}
},

Def::StructCtor(def_id, CtorKind::Const) |
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/nll/user-annotations/constant-in-expr-inherent-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![feature(nll)]

struct Foo<'a> { x: &'a u32 }

impl<'a> Foo<'a> {
const C: &'a u32 = &22;
}

fn foo<'a>(_: &'a u32) -> &'static u32 {
<Foo<'a>>::C //~ ERROR
}

fn main() {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: unsatisfied lifetime constraints
--> $DIR/constant-in-expr-inherent-1.rs:10:5
|
LL | fn foo<'a>(_: &'a u32) -> &'static u32 {
| -- lifetime `'a` defined here
LL | <Foo<'a>>::C //~ ERROR
| ^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`

error: aborting due to previous error

16 changes: 16 additions & 0 deletions src/test/ui/nll/user-annotations/constant-in-expr-trait-item-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(nll)]

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

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

fn foo<'a>(_: &'a u32) -> &'static u32 {
<() as Foo<'a>>::C //~ ERROR
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: unsatisfied lifetime constraints
--> $DIR/constant-in-expr-trait-item-1.rs:12:5
|
LL | fn foo<'a>(_: &'a u32) -> &'static u32 {
| -- lifetime `'a` defined here
LL | <() as Foo<'a>>::C //~ ERROR
| ^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`

error: aborting due to previous error

16 changes: 16 additions & 0 deletions src/test/ui/nll/user-annotations/constant-in-expr-trait-item-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(nll)]

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

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

fn foo<'a, T: Foo<'a>>() -> &'static u32 {
<T as Foo<'a>>::C //~ ERROR
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: unsatisfied lifetime constraints
--> $DIR/constant-in-expr-trait-item-2.rs:12:5
|
LL | fn foo<'a, T: Foo<'a>>() -> &'static u32 {
| -- lifetime `'a` defined here
LL | <T as Foo<'a>>::C //~ ERROR
| ^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`

error: aborting due to previous error

0 comments on commit e94959b

Please sign in to comment.