Skip to content

Commit 9a5e7ba

Browse files
committed
Teach variance checker about the lifetime bounds that appear in trait object types.
1 parent 5e83424 commit 9a5e7ba

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/librustc/middle/typeck/variance.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
778778
variance);
779779
}
780780

781-
ty::ty_trait(box ty::TyTrait { def_id, ref substs, .. }) => {
781+
ty::ty_trait(box ty::TyTrait { def_id, ref substs, bounds }) => {
782782
let trait_def = ty::lookup_trait_def(self.tcx(), def_id);
783783
let generics = &trait_def.generics;
784784

@@ -796,6 +796,10 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
796796
assert!(generics.types.is_empty_in(subst::FnSpace));
797797
assert!(generics.regions.is_empty_in(subst::FnSpace));
798798

799+
// The type `Foo<T+'a>` is contravariant w/r/t `'a`:
800+
let contra = self.contravariant(variance);
801+
self.add_constraints_from_region(bounds.region_bound, contra);
802+
799803
self.add_constraints_from_substs(
800804
def_id,
801805
generics.types.get_slice(subst::TypeSpace),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2014 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+
// Checks that regions which appear in a trait object type are
12+
// observed by the variance inference algorithm (and hence
13+
// `TOption` is contavariant w/r/t `'a` and not bivariant).
14+
//
15+
// Issue #18262.
16+
17+
use std::mem;
18+
19+
trait T { fn foo(); }
20+
21+
#[rustc_variance]
22+
struct TOption<'a> { //~ ERROR regions=[[-];[];[]]
23+
v: Option<Box<T + 'a>>,
24+
}
25+
26+
fn main() { }

src/test/run-pass/issue-16668.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct Parser<'a, I, O> {
1717
}
1818

1919
impl<'a, I, O: 'a> Parser<'a, I, O> {
20-
fn compose<K: 'a>(mut self, mut rhs: Parser<O, K>) -> Parser<'a, I, K> {
20+
fn compose<K: 'a>(mut self, mut rhs: Parser<'a, O, K>) -> Parser<'a, I, K> {
2121
Parser {
2222
parse: box move |&mut: x: I| {
2323
match self.parse.call_mut((x,)) {

0 commit comments

Comments
 (0)