From 17fca8b4604bc4cf89ac06292f6475614984edc9 Mon Sep 17 00:00:00 2001 From: Masaki Hara Date: Thu, 8 Jun 2017 14:46:46 +0900 Subject: [PATCH] Check explicitly that tuple initializer is Sized. --- src/librustc/traits/error_reporting.rs | 3 +++ src/librustc/traits/mod.rs | 2 ++ src/librustc/traits/structural_impls.rs | 3 +++ src/librustc_typeck/check/mod.rs | 1 + src/test/compile-fail/unsized3.rs | 1 + 5 files changed, 10 insertions(+) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index e2e3d520d47..35de536223a 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -1097,6 +1097,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { ObligationCauseCode::AssignmentLhsSized => { err.note("the left-hand-side of an assignment must have a statically known size"); } + ObligationCauseCode::TupleInitializerSized => { + err.note("tuples must have a statically known size to be initialized"); + } ObligationCauseCode::StructInitializerSized => { err.note("structs must have a statically known size to be initialized"); } diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs index c128438aea0..16c41c816b4 100644 --- a/src/librustc/traits/mod.rs +++ b/src/librustc/traits/mod.rs @@ -121,6 +121,8 @@ pub enum ObligationCauseCode<'tcx> { // Various cases where expressions must be sized/copy/etc: /// L = X implies that L is Sized AssignmentLhsSized, + /// (x1, .., xn) must be Sized + TupleInitializerSized, /// S { ... } must be Sized StructInitializerSized, /// Type of each variable must be Sized diff --git a/src/librustc/traits/structural_impls.rs b/src/librustc/traits/structural_impls.rs index 0d6df78c2ac..c4479e69032 100644 --- a/src/librustc/traits/structural_impls.rs +++ b/src/librustc/traits/structural_impls.rs @@ -189,6 +189,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> { tcx.lift(&ty).map(super::ObjectCastObligation) } super::AssignmentLhsSized => Some(super::AssignmentLhsSized), + super::TupleInitializerSized => Some(super::TupleInitializerSized), super::StructInitializerSized => Some(super::StructInitializerSized), super::VariableType(id) => Some(super::VariableType(id)), super::ReturnType(id) => Some(super::ReturnType(id)), @@ -476,6 +477,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> { super::TupleElem | super::ItemObligation(_) | super::AssignmentLhsSized | + super::TupleInitializerSized | super::StructInitializerSized | super::VariableType(_) | super::ReturnType(_) | @@ -523,6 +525,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> { super::TupleElem | super::ItemObligation(_) | super::AssignmentLhsSized | + super::TupleInitializerSized | super::StructInitializerSized | super::VariableType(_) | super::ReturnType(_) | diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 34cf1d7f96b..631158df474 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3854,6 +3854,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if tuple.references_error() { tcx.types.err } else { + self.require_type_is_sized(tuple, expr.span, traits::TupleInitializerSized); tuple } } diff --git a/src/test/compile-fail/unsized3.rs b/src/test/compile-fail/unsized3.rs index 9b6ccf22c8d..e96e0ea3aec 100644 --- a/src/test/compile-fail/unsized3.rs +++ b/src/test/compile-fail/unsized3.rs @@ -54,6 +54,7 @@ fn f9(x1: Box>) { fn f10(x1: Box>) { f5(&(32, *x1)); //~^ ERROR `X: std::marker::Sized` is not satisfied + //~| ERROR `X: std::marker::Sized` is not satisfied } pub fn main() {