review comments

This commit is contained in:
Esteban Küber 2020-01-14 13:18:06 -08:00
parent 4a75ef91f3
commit 5b36c187dc
22 changed files with 206 additions and 218 deletions

View file

@ -1412,6 +1412,8 @@ pub fn suggest_constraining_type_param(
false
}
/// Collect all the returned expressions within the input expression.
/// Used to point at the return spans when we want to suggest some change to them.
struct ReturnsVisitor<'v>(Vec<&'v hir::Expr<'v>>);
impl<'v> Visitor<'v> for ReturnsVisitor<'v> {

View file

@ -4,6 +4,7 @@ use super::{
};
use crate::infer::InferCtxt;
use crate::traits::object_safety::object_safety_violations;
use crate::ty::TypeckTables;
use crate::ty::{self, AdtKind, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable};
@ -543,6 +544,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
}
}
/// If all conditions are met to identify a returned `dyn Trait`, suggest using `impl Trait` if
/// applicable and signal that the error has been expanded appropriately and needs to be
/// emitted.
crate fn suggest_impl_trait(
&self,
err: &mut DiagnosticBuilder<'tcx>,
@ -550,9 +554,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
obligation: &PredicateObligation<'tcx>,
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
) -> bool {
if let ObligationCauseCode::SizedReturnType = obligation.cause.code.peel_derives() {
} else {
return false;
match obligation.cause.code.peel_derives() {
// Only suggest `impl Trait` if the return type is unsized because it is `dyn Trait`.
ObligationCauseCode::SizedReturnType => {}
_ => return false,
}
let hir = self.tcx.hir();
@ -565,12 +570,25 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let body = hir.body(*body_id);
let trait_ref = self.resolve_vars_if_possible(trait_ref);
let ty = trait_ref.skip_binder().self_ty();
if let ty::Dynamic(..) = ty.kind {
} else {
let is_object_safe;
match ty.kind {
ty::Dynamic(predicates, _) => {
// The `dyn Trait` is not object safe, do not suggest `Box<dyn Trait>`.
is_object_safe = predicates.principal_def_id().map_or(true, |def_id| {
!object_safety_violations(self.tcx, def_id).is_empty()
})
}
// We only want to suggest `impl Trait` to `dyn Trait`s.
// For example, `fn foo() -> str` needs to be filtered out.
return false;
_ => return false,
}
let ret_ty = if let hir::FunctionRetTy::Return(ret_ty) = sig.decl.output {
ret_ty
} else {
return false;
};
// Use `TypeVisitor` instead of the output type directly to find the span of `ty` for
// cases like `fn foo() -> (dyn Trait, i32) {}`.
// Recursively look for `TraitObject` types and if there's only one, use that span to
@ -583,122 +601,120 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
if let hir::FunctionRetTy::Return(ret_ty) = sig.decl.output {
let mut all_returns_conform_to_trait = true;
let mut all_returns_have_same_type = true;
let mut last_ty = None;
if let Some(ty_ret_ty) = tables.node_type_opt(ret_ty.hir_id) {
let cause = ObligationCause::misc(ret_ty.span, ret_ty.hir_id);
if let ty::Dynamic(predicates, _) = &ty_ret_ty.kind {
for predicate in predicates.iter() {
for expr in &visitor.0 {
if let Some(returned_ty) = tables.node_type_opt(expr.hir_id) {
if let Some(ty) = last_ty {
all_returns_have_same_type &= ty == returned_ty;
}
last_ty = Some(returned_ty);
let param_env = ty::ParamEnv::empty();
let pred = predicate.with_self_ty(self.tcx, returned_ty);
let obligation =
Obligation::new(cause.clone(), param_env, pred);
all_returns_conform_to_trait &=
self.predicate_may_hold(&obligation);
}
}
}
}
} else {
// We still want to verify whether all the return types conform to each other.
let mut all_returns_conform_to_trait = true;
let mut all_returns_have_same_type = true;
let mut last_ty = None;
if let Some(ty_ret_ty) = tables.node_type_opt(ret_ty.hir_id) {
let cause = ObligationCause::misc(ret_ty.span, ret_ty.hir_id);
let param_env = ty::ParamEnv::empty();
if let ty::Dynamic(predicates, _) = &ty_ret_ty.kind {
for expr in &visitor.0 {
if let Some(returned_ty) = tables.node_type_opt(expr.hir_id) {
if let Some(ty) = last_ty {
all_returns_have_same_type &= ty == returned_ty;
}
all_returns_have_same_type &=
Some(returned_ty) == last_ty || last_ty.is_none();
last_ty = Some(returned_ty);
for predicate in predicates.iter() {
let pred = predicate.with_self_ty(self.tcx, returned_ty);
let obl = Obligation::new(cause.clone(), param_env, pred);
all_returns_conform_to_trait &= self.predicate_may_hold(&obl);
}
}
}
}
} else {
// We still want to verify whether all the return types conform to each other.
for expr in &visitor.0 {
if let Some(returned_ty) = tables.node_type_opt(expr.hir_id) {
if let Some(ty) = last_ty {
all_returns_have_same_type &= ty == returned_ty;
}
last_ty = Some(returned_ty);
}
}
}
let (snippet, last_ty) =
if let (true, hir::TyKind::TraitObject(..), Ok(snippet), true, Some(last_ty)) = (
// Verify that we're dealing with a return `dyn Trait`
ret_ty.span.overlaps(span),
&ret_ty.kind,
self.tcx.sess.source_map().span_to_snippet(ret_ty.span),
// If any of the return types does not conform to the trait, then we can't
// suggest `impl Trait` nor trait objects, it is a type mismatch error.
all_returns_conform_to_trait,
last_ty,
) {
err.code = Some(error_code!(E0746));
err.set_primary_message(
"return type cannot have a bare trait because it must be `Sized`",
(snippet, last_ty)
} else {
return false;
};
err.code(error_code!(E0746));
err.set_primary_message("return type cannot have an unboxed trait object");
err.children.clear();
let impl_trait_msg = "for information on `impl Trait`, see \
<https://doc.rust-lang.org/book/ch10-02-traits.html\
#returning-types-that-implement-traits>";
let trait_obj_msg = "for information on trait objects, see \
<https://doc.rust-lang.org/book/ch17-02-trait-objects.html\
#using-trait-objects-that-allow-for-values-of-different-types>";
let has_dyn = snippet.split_whitespace().next().map_or(false, |s| s == "dyn");
let trait_obj = if has_dyn { &snippet[4..] } else { &snippet[..] };
if all_returns_have_same_type {
// Suggest `-> impl Trait`.
err.span_suggestion(
ret_ty.span,
&format!(
"return `impl {1}` instead, as all return paths are of type `{}`, \
which implements `{1}`",
last_ty, trait_obj,
),
format!("impl {}", trait_obj),
Applicability::MachineApplicable,
);
err.note(impl_trait_msg);
} else {
if is_object_safe {
// Suggest `-> Box<dyn Trait>` and `Box::new(returned_value)`.
// Get all the return values and collect their span and suggestion.
let mut suggestions = visitor
.0
.iter()
.map(|expr| {
(
expr.span,
format!(
"Box::new({})",
self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap()
),
)
})
.collect::<Vec<_>>();
// Add the suggestion for the return type.
suggestions.push((
ret_ty.span,
format!("Box<{}{}>", if has_dyn { "" } else { "dyn " }, snippet),
));
err.multipart_suggestion(
"return a trait object instead",
suggestions,
Applicability::MaybeIncorrect,
);
err.children.clear();
let impl_trait_msg = "for information on `impl Trait`, see \
<https://doc.rust-lang.org/book/ch10-02-traits.html\
#returning-types-that-implement-traits>";
let trait_obj_msg = "for information on trait objects, see \
<https://doc.rust-lang.org/book/ch17-02-trait-objects.html\
#using-trait-objects-that-allow-for-values-of-different-types>";
let has_dyn = snippet.split_whitespace().next().map_or(false, |s| s == "dyn");
let trait_obj = if has_dyn { &snippet[4..] } else { &snippet[..] };
if all_returns_have_same_type {
err.span_suggestion(
ret_ty.span,
&format!(
"you can use the `impl Trait` feature \
in the return type because all the return paths are of type \
`{}`, which implements `dyn {}`",
last_ty, trait_obj,
),
format!("impl {}", trait_obj),
Applicability::MachineApplicable,
);
err.note(impl_trait_msg);
} else {
let mut suggestions = visitor
.0
.iter()
.map(|expr| {
(
expr.span,
format!(
"Box::new({})",
self.tcx
.sess
.source_map()
.span_to_snippet(expr.span)
.unwrap()
),
)
})
.collect::<Vec<_>>();
suggestions.push((
ret_ty.span,
format!("Box<{}{}>", if has_dyn { "" } else { "dyn " }, snippet),
));
err.multipart_suggestion(
"if the performance implications are acceptable, you can return a \
trait object",
suggestions,
Applicability::MaybeIncorrect,
);
err.span_help(
visitor.0.iter().map(|expr| expr.span).collect::<Vec<_>>(),
&format!(
"if all the returned values were of the same type you could use \
`impl {}` as the return type",
trait_obj,
),
);
err.help(
"alternatively, you can always create a new `enum` with a variant \
for each returned type",
);
err.note(impl_trait_msg);
err.note(trait_obj_msg);
}
return true;
} else {
err.note(&format!(
"if trait `{}` was object safe, you could return a trait object",
trait_obj,
));
}
err.note(&format!(
"if all the returned values were of the same type you could use \
`impl {}` as the return type",
trait_obj,
));
err.note(impl_trait_msg);
err.note(trait_obj_msg);
err.note("you can create a new `enum` with a variant for each returned type");
}
return true;
}
false
}
@ -708,9 +724,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
err: &mut DiagnosticBuilder<'tcx>,
obligation: &PredicateObligation<'tcx>,
) {
if let ObligationCauseCode::SizedReturnType = obligation.cause.code.peel_derives() {
} else {
return;
match obligation.cause.code.peel_derives() {
ObligationCauseCode::SizedReturnType => {}
_ => return,
}
let hir = self.tcx.hir();
@ -726,10 +742,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let tables = self.in_progress_tables.map(|t| t.borrow()).unwrap();
for expr in &visitor.0 {
if let Some(returned_ty) = tables.node_type_opt(expr.hir_id) {
err.span_label(
expr.span,
&format!("this returned value is of type `{}`", returned_ty),
);
let ty = self.resolve_vars_if_possible(&returned_ty);
err.span_label(expr.span, &format!("this returned value is of type `{}`", ty));
}
}
}
@ -1685,9 +1699,8 @@ impl<'v> Visitor<'v> for ReturnsVisitor<'v> {
}
fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
match ex.kind {
hir::ExprKind::Ret(Some(ex)) => self.0.push(ex),
_ => {}
if let hir::ExprKind::Ret(Some(ex)) = ex.kind {
self.0.push(ex);
}
hir::intravisit::walk_expr(self, ex);
}

View file

@ -155,8 +155,8 @@ pub struct ObligationCause<'tcx> {
pub code: ObligationCauseCode<'tcx>,
}
impl<'tcx> ObligationCause<'tcx> {
pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span {
impl ObligationCause<'_> {
pub fn span(&self, tcx: TyCtxt<'_>) -> Span {
match self.code {
ObligationCauseCode::CompareImplMethodObligation { .. }
| ObligationCauseCode::MainFunctionType
@ -1172,13 +1172,13 @@ impl<'tcx> ObligationCause<'tcx> {
}
impl<'tcx> ObligationCauseCode<'tcx> {
// Return the base obligation, ignoring derived obligations.
pub fn peel_derives(&self) -> &Self {
match self {
BuiltinDerivedObligation(cause) | ImplDerivedObligation(cause) => {
cause.parent_code.peel_derives()
}
_ => self,
let mut base_cause = self;
while let BuiltinDerivedObligation(cause) | ImplDerivedObligation(cause) = base_cause {
base_cause = &cause.parent_code;
}
base_cause
}
}

View file

@ -244,9 +244,9 @@ impl<'tcx> ty::TyS<'tcx> {
ty::FnPtr(_) => "fn pointer".into(),
ty::Dynamic(ref inner, ..) => {
if let Some(principal) = inner.principal() {
format!("trait `{}`", tcx.def_path_str(principal.def_id())).into()
format!("trait object `dyn {}`", tcx.def_path_str(principal.def_id())).into()
} else {
"trait".into()
"trait object".into()
}
}
ty::Closure(..) => "closure".into(),

View file

@ -50,6 +50,7 @@
//! sort of a minor point so I've opted to leave it for later -- after all,
//! we may want to adjust precisely when coercions occur.
use crate::astconv::AstConv;
use crate::check::{FnCtxt, Needs};
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc::infer::{Coercion, InferOk, InferResult};
@ -1245,7 +1246,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
expression.map(|expr| (expr, blk_id)),
);
if !fcx.tcx.features().unsized_locals {
unsized_return = fcx.is_unsized_return(blk_id);
unsized_return = self.is_return_ty_unsized(fcx, blk_id);
}
}
ObligationCauseCode::ReturnValue(id) => {
@ -1260,7 +1261,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
);
if !fcx.tcx.features().unsized_locals {
let id = fcx.tcx.hir().get_parent_node(id);
unsized_return = fcx.is_unsized_return(id);
unsized_return = self.is_return_ty_unsized(fcx, id);
}
}
_ => {
@ -1290,15 +1291,6 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
.filter(|e| fcx.is_assign_to_bool(e, self.expected_ty()))
.is_some();
if unsized_return {
fcx.tcx.sess.delay_span_bug(
cause.span,
&format!(
"elided E0308 in favor of more detailed E0277 or E0746: {:?}",
cause.code
),
);
}
err.emit_unless(assign_to_bool || unsized_return);
self.final_ty = Some(fcx.tcx.types.err);
@ -1365,10 +1357,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
"...is found to be `{}` here",
fcx.resolve_vars_with_obligations(expected),
));
err.note(
"`impl Trait` as a return type requires that all the returned values must have \
the same type",
);
err.note("to return `impl Trait`, all returned values must be of the same type");
let snippet = fcx
.tcx
.sess
@ -1397,6 +1386,18 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
err
}
fn is_return_ty_unsized(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool {
if let Some((fn_decl, _)) = fcx.get_fn_decl(blk_id) {
if let hir::FunctionRetTy::Return(ty) = fn_decl.output {
let ty = AstConv::ast_ty_to_ty(fcx, ty);
if let ty::Dynamic(..) = ty.kind {
return true;
}
}
}
false
}
pub fn complete<'a>(self, fcx: &FnCtxt<'a, 'tcx>) -> Ty<'tcx> {
if let Some(final_ty) = self.final_ty {
final_ty

View file

@ -4964,18 +4964,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
fn is_unsized_return(&self, blk_id: hir::HirId) -> bool {
if let Some((fn_decl, _)) = self.get_fn_decl(blk_id) {
if let hir::FunctionRetTy::Return(ty) = fn_decl.output {
let ty = AstConv::ast_ty_to_ty(self, ty);
if let ty::Dynamic(..) = ty.kind {
return true;
}
}
}
false
}
/// A possible error is to forget to add a return type that is needed:
///
/// ```

View file

@ -29,7 +29,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:13:13
|
LL | let _ = box { |x| (x as u8) }: Box<dyn Fn(i32) -> _>;
| ^^^^^^^^^^^^^^^^^^^^^ expected trait `std::ops::Fn`, found closure
| ^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::ops::Fn`, found closure
|
= note: expected struct `std::boxed::Box<dyn std::ops::Fn(i32) -> u8>`
found struct `std::boxed::Box<[closure@$DIR/coerce-expect-unsized-ascribed.rs:13:19: 13:32]>`
@ -38,7 +38,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:14:13
|
LL | let _ = box if true { false } else { true }: Box<dyn Debug>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::fmt::Debug`, found `bool`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::fmt::Debug`, found `bool`
|
= note: expected struct `std::boxed::Box<dyn std::fmt::Debug>`
found struct `std::boxed::Box<bool>`
@ -47,7 +47,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:15:13
|
LL | let _ = box match true { true => 'a', false => 'b' }: Box<dyn Debug>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::fmt::Debug`, found `char`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::fmt::Debug`, found `char`
|
= note: expected struct `std::boxed::Box<dyn std::fmt::Debug>`
found struct `std::boxed::Box<char>`
@ -83,7 +83,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:21:13
|
LL | let _ = &{ |x| (x as u8) }: &dyn Fn(i32) -> _;
| ^^^^^^^^^^^^^^^^^^ expected trait `std::ops::Fn`, found closure
| ^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::ops::Fn`, found closure
|
= note: expected reference `&dyn std::ops::Fn(i32) -> u8`
found reference `&[closure@$DIR/coerce-expect-unsized-ascribed.rs:21:16: 21:29]`
@ -92,7 +92,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:22:13
|
LL | let _ = &if true { false } else { true }: &dyn Debug;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::fmt::Debug`, found `bool`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::fmt::Debug`, found `bool`
|
= note: expected reference `&dyn std::fmt::Debug`
found reference `&bool`
@ -101,7 +101,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:23:13
|
LL | let _ = &match true { true => 'a', false => 'b' }: &dyn Debug;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::fmt::Debug`, found `char`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::fmt::Debug`, found `char`
|
= note: expected reference `&dyn std::fmt::Debug`
found reference `&char`
@ -119,7 +119,7 @@ error[E0308]: mismatched types
--> $DIR/coerce-expect-unsized-ascribed.rs:26:13
|
LL | let _ = Box::new(|x| (x as u8)): Box<dyn Fn(i32) -> _>;
| ^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::ops::Fn`, found closure
| ^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn std::ops::Fn`, found closure
|
= note: expected struct `std::boxed::Box<dyn std::ops::Fn(i32) -> _>`
found struct `std::boxed::Box<[closure@$DIR/coerce-expect-unsized-ascribed.rs:26:22: 26:35]>`

View file

@ -13,7 +13,7 @@ LL | pub fn no_iterator() -> impl Iterator<Item = i32> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::iter::Iterator` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size
@ -33,7 +33,7 @@ LL | pub fn no_double_ended_iterator() -> impl DoubleEndedIterator {
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::iter::DoubleEndedIterator` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size
@ -53,7 +53,7 @@ LL | pub fn no_exact_size_iterator() -> impl ExactSizeIterator {
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::iter::ExactSizeIterator` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size
@ -73,7 +73,7 @@ LL | pub fn no_fused_iterator() -> impl FusedIterator {
| ^^^^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::iter::FusedIterator` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size
@ -93,7 +93,7 @@ LL | pub fn no_trusted_len() -> impl TrustedLen {
| ^^^^^^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::iter::TrustedLen` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size
@ -113,7 +113,7 @@ LL | pub fn no_clone() -> impl Clone {
| ^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::clone::Clone` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size
@ -133,7 +133,7 @@ LL | pub fn no_debug() -> impl Debug {
| ^^^^^^^^^^ the trait `std::array::LengthAtMost32` is not implemented for `[i32; 33]`
LL |
LL | IntoIter::new([0i32; 33])
| ------------------------- this returned value is of type `std::array::IntoIter<_, _: usize>`
| ------------------------- this returned value is of type `std::array::IntoIter<i32, 33usize>`
|
= note: required because of the requirements on the impl of `std::fmt::Debug` for `std::array::IntoIter<i32, 33usize>`
= note: the return type of a function must have a statically known size

View file

@ -33,12 +33,10 @@ fn main() {
//~^ ERROR mismatched types
//~| expected trait object `dyn T`
//~| found reference `&_`
//~| expected trait `T`, found reference
let &&&x = &(&1isize as &dyn T);
//~^ ERROR mismatched types
//~| expected trait object `dyn T`
//~| found reference `&_`
//~| expected trait `T`, found reference
let box box x = box 1isize as Box<dyn T>;
//~^ ERROR mismatched types
//~| expected trait object `dyn T`

View file

@ -22,31 +22,31 @@ error[E0308]: mismatched types
LL | let &&x = &1isize as &dyn T;
| ^^
| |
| expected trait `T`, found reference
| expected trait object `dyn T`, found reference
| help: you can probably remove the explicit borrow: `x`
|
= note: expected trait object `dyn T`
found reference `&_`
error[E0308]: mismatched types
--> $DIR/destructure-trait-ref.rs:37:11
--> $DIR/destructure-trait-ref.rs:36:11
|
LL | let &&&x = &(&1isize as &dyn T);
| ^^
| |
| expected trait `T`, found reference
| expected trait object `dyn T`, found reference
| help: you can probably remove the explicit borrow: `x`
|
= note: expected trait object `dyn T`
found reference `&_`
error[E0308]: mismatched types
--> $DIR/destructure-trait-ref.rs:42:13
--> $DIR/destructure-trait-ref.rs:40:13
|
LL | let box box x = box 1isize as Box<dyn T>;
| ^^^^^ ------------------------ this expression has type `std::boxed::Box<dyn T>`
| |
| expected trait `T`, found struct `std::boxed::Box`
| expected trait object `dyn T`, found struct `std::boxed::Box`
|
= note: expected trait object `dyn T`
found struct `std::boxed::Box<_>`

View file

@ -32,7 +32,7 @@ pub fn main() {
let z: Box<dyn ToBar> = Box::new(Bar1 {f: 36});
f5.2 = Bar1 {f: 36};
//~^ ERROR mismatched types
//~| expected trait `ToBar`, found struct `Bar1`
//~| expected trait object `dyn ToBar`, found struct `Bar1`
//~| expected trait object `dyn ToBar`
//~| found struct `Bar1`
//~| ERROR the size for values of type

View file

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/dst-bad-assign-3.rs:33:12
|
LL | f5.2 = Bar1 {f: 36};
| ^^^^^^^^^^^^ expected trait `ToBar`, found struct `Bar1`
| ^^^^^^^^^^^^ expected trait object `dyn ToBar`, found struct `Bar1`
|
= note: expected trait object `dyn ToBar`
found struct `Bar1`

View file

@ -34,7 +34,7 @@ pub fn main() {
let z: Box<dyn ToBar> = Box::new(Bar1 {f: 36});
f5.ptr = Bar1 {f: 36};
//~^ ERROR mismatched types
//~| expected trait `ToBar`, found struct `Bar1`
//~| expected trait object `dyn ToBar`, found struct `Bar1`
//~| expected trait object `dyn ToBar`
//~| found struct `Bar1`
//~| ERROR the size for values of type

View file

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/dst-bad-assign.rs:35:14
|
LL | f5.ptr = Bar1 {f: 36};
| ^^^^^^^^^^^^ expected trait `ToBar`, found struct `Bar1`
| ^^^^^^^^^^^^ expected trait object `dyn ToBar`, found struct `Bar1`
|
= note: expected trait object `dyn ToBar`
found struct `Bar1`

View file

@ -1,23 +1,23 @@
error[E0746]: return type cannot have a bare trait because it must be `Sized`
error[E0746]: return type cannot have an unboxed trait object
--> $DIR/E0746.rs:8:13
|
LL | fn foo() -> dyn Trait { Struct }
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: you can use the `impl Trait` feature in the return type because all the return paths are of type `Struct`, which implements `dyn Trait`
help: return `impl Trait` instead, as all return paths are of type `Struct`, which implements `Trait`
|
LL | fn foo() -> impl Trait { Struct }
| ^^^^^^^^^^
error[E0746]: return type cannot have a bare trait because it must be `Sized`
error[E0746]: return type cannot have an unboxed trait object
--> $DIR/E0746.rs:11:13
|
LL | fn bar() -> dyn Trait {
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: you can use the `impl Trait` feature in the return type because all the return paths are of type `{integer}`, which implements `dyn Trait`
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
|
LL | fn bar() -> impl Trait {
| ^^^^^^^^^^

View file

@ -2,7 +2,7 @@ error[E0308]: mismatched types
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:7:35
|
LL | fn fuz() -> (usize, Trait) { (42, Struct) }
| ^^^^^^ expected trait `Trait`, found struct `Struct`
| ^^^^^^ expected trait object `dyn Trait`, found struct `Struct`
|
= note: expected trait object `(dyn Trait + 'static)`
found struct `Struct`
@ -24,7 +24,7 @@ error[E0308]: mismatched types
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:10:39
|
LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
| ^^^^^^ expected trait `Trait`, found struct `Struct`
| ^^^^^^ expected trait object `dyn Trait`, found struct `Struct`
|
= note: expected trait object `(dyn Trait + 'static)`
found struct `Struct`
@ -42,26 +42,26 @@ LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
= note: required because it appears within the type `(usize, (dyn Trait + 'static))`
= note: the return type of a function must have a statically known size
error[E0746]: return type cannot have a bare trait because it must be `Sized`
error[E0746]: return type cannot have an unboxed trait object
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:13:13
|
LL | fn bap() -> Trait { Struct }
| ^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: you can use the `impl Trait` feature in the return type because all the return paths are of type `Struct`, which implements `dyn Trait`
help: return `impl Trait` instead, as all return paths are of type `Struct`, which implements `Trait`
|
LL | fn bap() -> impl Trait { Struct }
| ^^^^^^^^^^
error[E0746]: return type cannot have a bare trait because it must be `Sized`
error[E0746]: return type cannot have an unboxed trait object
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:15:13
|
LL | fn ban() -> dyn Trait { Struct }
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: you can use the `impl Trait` feature in the return type because all the return paths are of type `Struct`, which implements `dyn Trait`
help: return `impl Trait` instead, as all return paths are of type `Struct`, which implements `Trait`
|
LL | fn ban() -> impl Trait { Struct }
| ^^^^^^^^^^
@ -76,40 +76,26 @@ LL | fn bak() -> dyn Trait { unimplemented!() }
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: the return type of a function must have a statically known size
error[E0746]: return type cannot have a bare trait because it must be `Sized`
error[E0746]: return type cannot have an unboxed trait object
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13
|
LL | fn bal() -> dyn Trait {
| ^^^^^^^^^ doesn't have a size known at compile-time
|
help: if all the returned values were of the same type you could use `impl Trait` as the return type
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:23:5
|
LL | return Struct;
| ^^^^^^
LL | }
LL | 42
| ^^
= help: alternatively, you can always create a new `enum` with a variant for each returned type
= note: if trait `Trait` was object safe, you could return a trait object
= note: if all the returned values were of the same type you could use `impl Trait` as the return type
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
help: if the performance implications are acceptable, you can return a trait object
|
LL | fn bal() -> Box<dyn Trait> {
LL | if true {
LL | return Box::new(Struct);
LL | }
LL | Box::new(42)
|
= note: you can create a new `enum` with a variant for each returned type
error[E0746]: return type cannot have a bare trait because it must be `Sized`
error[E0746]: return type cannot have an unboxed trait object
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:27:13
|
LL | fn bat() -> dyn Trait {
| ^^^^^^^^^ doesn't have a size known at compile-time
|
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
help: you can use the `impl Trait` feature in the return type because all the return paths are of type `{integer}`, which implements `dyn Trait`
help: return `impl Trait` instead, as all return paths are of type `{integer}`, which implements `Trait`
|
LL | fn bat() -> impl Trait {
| ^^^^^^^^^^

View file

@ -10,7 +10,7 @@ LL | }
LL | 0_u32
| ^^^^^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn Foo>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>

View file

@ -5,7 +5,7 @@ LL | ) -> Either<impl Trait<<u32 as Add<u32>>::Output>, impl Trait<<u32 as Add<u
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<u32>` is not implemented for `impl Trait<<u32 as std::ops::Add>::Output>`
...
LL | add_generic(value, 1u32)
| ------------------------ this returned value is of type `Either<impl Trait<<_ as std::ops::Add<_>>::Output>, impl Trait<<_ as std::ops::Add<_>>::Output>>`
| ------------------------ this returned value is of type `Either<impl Trait<<u32 as std::ops::Add>::Output>, impl Trait<<u32 as std::ops::Add>::Output>>`
|
= note: the return type of a function must have a statically known size
@ -16,7 +16,7 @@ LL | ) -> Either<impl Trait<<u32 as Add<u32>>::Output>, impl Trait<<u32 as Add<u
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait<u32>` is not implemented for `impl Trait<<u32 as std::ops::Add>::Output>`
...
LL | add_generic(value, 1u32)
| ------------------------ this returned value is of type `Either<impl Trait<<_ as std::ops::Add<_>>::Output>, impl Trait<<_ as std::ops::Add<_>>::Output>>`
| ------------------------ this returned value is of type `Either<impl Trait<<u32 as std::ops::Add>::Output>, impl Trait<<u32 as std::ops::Add>::Output>>`
|
= note: the return type of a function must have a statically known size

View file

@ -5,7 +5,7 @@ LL | fn foo() -> impl Future<Item=(), Error=Box<dyn Error>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Future` is not implemented for `std::result::Result<(), _>`
LL |
LL | Ok(())
| ------ this returned value is of type `std::result::Result<_, _>`
| ------ this returned value is of type `std::result::Result<(), _>`
|
= note: the return type of a function must have a statically known size

View file

@ -5,7 +5,7 @@ LL | fn should_ret_unit() -> impl T {
| ^^^^^^ the trait `T` is not implemented for `()`
LL |
LL | panic!()
| -------- this returned value is of type `_`
| -------- this returned value is of type `()`
|
= note: the return type of a function must have a statically known size
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

View file

@ -10,7 +10,7 @@ LL | }
LL | 1u32
| ^^^^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn std::fmt::Display>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
@ -27,7 +27,7 @@ LL | } else {
LL | return 1u32;
| ^^^^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn std::fmt::Display>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
@ -44,7 +44,7 @@ LL | } else {
LL | 1u32
| ^^^^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn std::fmt::Display>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
@ -73,7 +73,7 @@ LL | 0 => return 0i32,
LL | _ => 1u32,
| ^^^^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn std::fmt::Display>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
@ -92,7 +92,7 @@ LL | | _ => 2u32,
LL | | }
| |_____^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn std::fmt::Display>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
@ -109,7 +109,7 @@ LL | return 0i32;
LL | 1u32
| ^^^^ expected `i32`, found `u32`
|
= note: `impl Trait` as a return type requires that all the returned values must have the same type
= note: to return `impl Trait`, all returned values must be of the same type
= help: you can instead return a trait object using `Box<dyn std::fmt::Display>`
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>

View file

@ -4,7 +4,7 @@ error[E0308]: mismatched types
LL | fn ice(x: Box<dyn Iterator<Item=()>>) {
| - possibly return type missing here?
LL | *x
| ^^ expected `()`, found trait `std::iter::Iterator`
| ^^ expected `()`, found trait object `dyn std::iter::Iterator`
|
= note: expected unit type `()`
found trait object `(dyn std::iter::Iterator<Item = ()> + 'static)`