From 77bc26f4f3cb5f053dbe72b363b55a53f626f0cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sun, 28 Jan 2018 20:23:49 +0100 Subject: [PATCH] Require yield types to be sized --- src/librustc/traits/error_reporting.rs | 4 ++++ src/librustc/traits/mod.rs | 2 ++ src/librustc/traits/structural_impls.rs | 3 +++ src/librustc_typeck/check/mod.rs | 4 +++- src/test/ui/generator/sized-yield.rs | 21 +++++++++++++++++++++ src/test/ui/generator/sized-yield.stderr | 22 ++++++++++++++++++++++ 6 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/generator/sized-yield.rs create mode 100644 src/test/ui/generator/sized-yield.stderr diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index 42200a3a4472..3cb9449901d0 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -1261,6 +1261,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { err.note("the return type of a function must have a \ statically known size"); } + ObligationCauseCode::SizedYieldType => { + err.note("the yield type of a generator must have a \ + statically known size"); + } ObligationCauseCode::AssignmentLhsSized => { err.note("the left-hand-side of an assignment must have a statically known size"); } diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs index fd47e09aad7f..5bfa6f936db7 100644 --- a/src/librustc/traits/mod.rs +++ b/src/librustc/traits/mod.rs @@ -138,6 +138,8 @@ pub enum ObligationCauseCode<'tcx> { VariableType(ast::NodeId), /// Return type must be Sized SizedReturnType, + /// Yield type must be Sized + SizedYieldType, /// [T,..n] --> T must be Copy RepeatVec, diff --git a/src/librustc/traits/structural_impls.rs b/src/librustc/traits/structural_impls.rs index e1e2798ecb51..1eb14a222787 100644 --- a/src/librustc/traits/structural_impls.rs +++ b/src/librustc/traits/structural_impls.rs @@ -209,6 +209,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> { super::VariableType(id) => Some(super::VariableType(id)), super::ReturnType(id) => Some(super::ReturnType(id)), super::SizedReturnType => Some(super::SizedReturnType), + super::SizedYieldType => Some(super::SizedYieldType), super::RepeatVec => Some(super::RepeatVec), super::FieldSized(item) => Some(super::FieldSized(item)), super::ConstSized => Some(super::ConstSized), @@ -526,6 +527,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> { super::VariableType(_) | super::ReturnType(_) | super::SizedReturnType | + super::SizedYieldType | super::ReturnNoExpression | super::RepeatVec | super::FieldSized(_) | @@ -574,6 +576,7 @@ impl<'tcx> TypeFoldable<'tcx> for traits::ObligationCauseCode<'tcx> { super::VariableType(_) | super::ReturnType(_) | super::SizedReturnType | + super::SizedYieldType | super::ReturnNoExpression | super::RepeatVec | super::FieldSized(_) | diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 9b24c09036bc..098a98a8d92f 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1001,7 +1001,9 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>, let span = body.value.span; if body.is_generator && can_be_generator.is_some() { - fcx.yield_ty = Some(fcx.next_ty_var(TypeVariableOrigin::TypeInference(span))); + let yield_ty = fcx.next_ty_var(TypeVariableOrigin::TypeInference(span)); + fcx.require_type_is_sized(yield_ty, span, traits::SizedYieldType); + fcx.yield_ty = Some(yield_ty); } GatherLocalsVisitor { fcx: &fcx, }.visit_body(body); diff --git a/src/test/ui/generator/sized-yield.rs b/src/test/ui/generator/sized-yield.rs new file mode 100644 index 000000000000..f38ebf8b9463 --- /dev/null +++ b/src/test/ui/generator/sized-yield.rs @@ -0,0 +1,21 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(generators, generator_trait)] + +use std::ops::Generator; + +fn main() { + let s = String::from("foo"); + let mut gen = move || { //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied + yield s[..]; + }; + gen.resume(); //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied +} diff --git a/src/test/ui/generator/sized-yield.stderr b/src/test/ui/generator/sized-yield.stderr new file mode 100644 index 000000000000..7adb2cc5598d --- /dev/null +++ b/src/test/ui/generator/sized-yield.stderr @@ -0,0 +1,22 @@ +error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied + --> $DIR/sized-yield.rs:17:26 + | +17 | let mut gen = move || { //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied + | __________________________^ +18 | | yield s[..]; +19 | | }; + | |____^ `str` does not have a constant size known at compile-time + | + = help: the trait `std::marker::Sized` is not implemented for `str` + = note: the yield type of a generator must have a statically known size + +error[E0277]: the trait bound `str: std::marker::Sized` is not satisfied + --> $DIR/sized-yield.rs:20:8 + | +20 | gen.resume(); //~ ERROR the trait bound `str: std::marker::Sized` is not satisfied + | ^^^^^^ `str` does not have a constant size known at compile-time + | + = help: the trait `std::marker::Sized` is not implemented for `str` + +error: aborting due to 2 previous errors +