Skip to content

Commit 0f08261

Browse files
author
Ariel Ben-Yehuda
committed
check built-in types for well-formedness
Fixes rust-lang#21111. Fixes rust-lang#24707. Fixes rust-lang#24957. Fixes rust-lang#25388. Fixes rust-lang#25637. Fixes rust-lang#26301.
1 parent efc3143 commit 0f08261

File tree

6 files changed

+102
-14
lines changed

6 files changed

+102
-14
lines changed

src/librustc_typeck/check/wf.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ impl<'cx,'tcx> BoundsChecker<'cx,'tcx> {
538538
ty.fold_with(self);
539539
self.binding_count -= 1;
540540
}
541+
542+
fn cause(&self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> {
543+
traits::ObligationCause::new(self.span, self.fcx.body_id, code)
544+
}
541545
}
542546

543547
impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
@@ -574,7 +578,22 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
574578
None => { }
575579
}
576580

577-
match t.sty{
581+
match t.sty {
582+
ty::TyArray(ety, _) | ty::TySlice(ety) => {
583+
self.fcx.register_builtin_bound(self.fold_ty(ety),
584+
ty::BoundSized,
585+
self.cause(traits::MiscObligation));
586+
t
587+
}
588+
ty::TyTuple(ref tys) => {
589+
for ty in tys {
590+
self.fcx.register_builtin_bound(self.fold_ty(ty),
591+
ty::BoundSized,
592+
self.cause(traits::MiscObligation)
593+
);
594+
}
595+
t
596+
}
578597
ty::TyStruct(type_id, substs) |
579598
ty::TyEnum(type_id, substs) => {
580599
let type_predicates = self.fcx.tcx().lookup_predicates(type_id);
@@ -583,9 +602,7 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
583602

584603
if self.binding_count == 0 {
585604
self.fcx.add_obligations_for_parameters(
586-
traits::ObligationCause::new(self.span,
587-
self.fcx.body_id,
588-
traits::ItemObligation(type_id)),
605+
self.cause(traits::ItemObligation(type_id)),
589606
&bounds);
590607
} else {
591608
// There are two circumstances in which we ignore
@@ -612,20 +629,17 @@ impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
612629
// that will require an RFC. -nmatsakis)
613630
let bounds = filter_to_trait_obligations(bounds);
614631
self.fcx.add_obligations_for_parameters(
615-
traits::ObligationCause::new(self.span,
616-
self.fcx.body_id,
617-
traits::ItemObligation(type_id)),
632+
self.cause(traits::ItemObligation(type_id)),
618633
&bounds);
619634
}
620635

621636
self.fold_substs(substs);
637+
t
622638
}
623639
_ => {
624-
super_fold_ty(self, t);
640+
super_fold_ty(self, t)
625641
}
626642
}
627-
628-
t // we're not folding to produce a new type, so just return `t` here
629643
}
630644
}
631645

src/test/compile-fail/associated-types-coherence-failure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::marker::PhantomData;
1515
use std::ops::Deref;
1616

17-
pub struct Cow<'a, B: ?Sized>(PhantomData<(&'a (),B)>);
17+
pub struct Cow<'a, B: ?Sized>(PhantomData<(&'a (),*mut B)>);
1818

1919
/// Trait for moving into a `Cow`
2020
pub trait IntoCow<'a, B: ?Sized> {

src/test/compile-fail/issue-24957.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2015 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+
pub enum BsonValue {
12+
A([u8]),
13+
B([BsonValue]), //~ ERROR the trait `core::marker::Sized` is not implemented
14+
//~^ ERROR the trait `core::marker::Sized` is not implemented
15+
}
16+
17+
pub fn set_value(_v:&BsonValue)
18+
{
19+
}
20+
21+
fn main()
22+
{
23+
}

src/test/compile-fail/unsized6.rs

-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ trait T {}
1414

1515
fn f1<X: ?Sized>(x: &X) {
1616
let _: X; // <-- this is OK, no bindings created, no initializer.
17-
let _: (isize, (X, isize)); // same
1817
let y: X; //~ERROR the trait `core::marker::Sized` is not implemented
19-
let y: (isize, (X, isize)); //~ERROR the trait `core::marker::Sized` is not implemented
2018
}
2119
fn f2<X: ?Sized + T>(x: &X) {
2220
let y: X; //~ERROR the trait `core::marker::Sized` is not implemented
23-
let y: (isize, (X, isize)); //~ERROR the trait `core::marker::Sized` is not implemented
2421
}
2522

2623
fn f3<X: ?Sized>(x1: Box<X>, x2: Box<X>, x3: Box<X>) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015 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+
// Check that we catch attempts to create non-well-formed types
12+
13+
struct Bad<T:?Sized+'static, U:?Sized> {
14+
p1: &'static [T], //~ ERROR the trait `core::marker::Sized` is not implemented
15+
p2: U, //~ ERROR the trait `core::marker::Sized` is not implemented
16+
p3: u32
17+
}
18+
19+
trait Tr {}
20+
21+
struct S<T: Tr>(T);
22+
23+
fn b1() -> &'static [(u8,fn(&'static [S<()>]))]
24+
{} //~^ ERROR the trait `Tr` is not implemented
25+
fn b2() -> &'static [[i8]]
26+
{} //~^ ERROR the trait `core::marker::Sized` is not implemented
27+
fn b3() -> &'static [[u16]; 2]
28+
{} //~^ ERROR the trait `core::marker::Sized` is not implemented
29+
fn b4() -> &'static ([i16],)
30+
{} //~^ ERROR the trait `core::marker::Sized` is not implemented
31+
fn b5() -> &'static (u32,[i32],u32)
32+
{} //~^ ERROR the trait `core::marker::Sized` is not implemented
33+
fn b6() -> &'static (u32,u32,[i64])
34+
{} //~^ ERROR the trait `core::marker::Sized` is not implemented
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 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+
struct Wrap<'a, T: ?Sized>(&'a (), T);
12+
13+
fn foo<'a, T>(_a: for<'s> fn() -> [Wrap<'s, T>]) where Wrap<'a, T>: Sized {
14+
//~^ ERROR mismatched types
15+
//~^^ ERROR mismatched types
16+
}
17+
18+
fn main() {}

0 commit comments

Comments
 (0)