From 813c994a754b963660b2030699ee1b77c5886ed5 Mon Sep 17 00:00:00 2001 From: David Wood Date: Wed, 12 Jun 2019 08:26:16 +0100 Subject: [PATCH] rustc_mir: Re-use `report_selection_error`. This commit replaces the new error that was being emitted in NLL type check with a call to `report_selection_error` so that the same trait error as before this PR is emitted. --- src/librustc/traits/error_reporting.rs | 4 ++ src/librustc/traits/mod.rs | 2 + src/librustc/traits/structural_impls.rs | 1 + .../borrow_check/nll/type_check/mod.rs | 40 +++++++++---------- .../migrate-borrowck.rs | 4 +- .../migrate-borrowck.stderr | 13 ++++-- .../nll-borrowck.rs | 4 +- .../nll-borrowck.stderr | 13 ++++-- src/test/ui/repeat-to-run-dtor-twice.rs | 2 +- src/test/ui/repeat-to-run-dtor-twice.stderr | 5 ++- 10 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index a7b3a2cab11..d6cc68bcdab 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -1564,6 +1564,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { err.note(&format!("required for the cast to the object type `{}`", self.ty_to_string(object_ty))); } + ObligationCauseCode::RepeatVec => { + err.note("the `Copy` trait is required because the \ + repeated element will be copied"); + } ObligationCauseCode::VariableType(_) => { err.note("all local variables must have a statically known size"); if !self.tcx.features().unsized_locals { diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs index b90f4ef91ce..1ca92d79fa5 100644 --- a/src/librustc/traits/mod.rs +++ b/src/librustc/traits/mod.rs @@ -195,6 +195,8 @@ pub enum ObligationCauseCode<'tcx> { SizedReturnType, /// Yield type must be Sized SizedYieldType, + /// [T,..n] --> T must be Copy + RepeatVec, /// Types of fields (other than the last, except for packed structs) in a struct must be sized. FieldSized { adt_kind: AdtKind, last: bool }, diff --git a/src/librustc/traits/structural_impls.rs b/src/librustc/traits/structural_impls.rs index 7d160af1a56..129a400d28f 100644 --- a/src/librustc/traits/structural_impls.rs +++ b/src/librustc/traits/structural_impls.rs @@ -488,6 +488,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> { super::SizedArgumentType => Some(super::SizedArgumentType), super::SizedReturnType => Some(super::SizedReturnType), super::SizedYieldType => Some(super::SizedYieldType), + super::RepeatVec => Some(super::RepeatVec), super::FieldSized { adt_kind, last } => Some(super::FieldSized { adt_kind, last }), super::ConstSized => Some(super::ConstSized), super::SharedStatic => Some(super::SharedStatic), diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index 90812599f93..dbb5a52e0aa 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -35,7 +35,7 @@ use rustc::mir::*; use rustc::traits::query::type_op; use rustc::traits::query::type_op::custom::CustomTypeOp; use rustc::traits::query::{Fallible, NoSolution}; -use rustc::traits::{ObligationCause, PredicateObligations}; +use rustc::traits::{self, ObligationCause, PredicateObligations}; use rustc::ty::adjustment::{PointerCast}; use rustc::ty::fold::TypeFoldable; use rustc::ty::subst::{Subst, SubstsRef, UnpackedKind, UserSubsts}; @@ -1968,25 +1968,25 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { // a required check to make sure that repeated elements implement `Copy`. let span = body.source_info(location).span; let ty = operand.ty(body, tcx); - let is_copy = self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span); - if !is_copy { - let copy_path = self.tcx().def_path_str( - self.tcx().lang_items().copy_trait().unwrap()); - self.tcx().sess - .struct_span_err( - span, - &format!("repeated expression does not implement `{}`", copy_path), - ) - .span_label(span, &format!( - "the trait `{}` is not implemented for `{}`", - copy_path, ty, - )) - .note(&format!( - "the `{}` trait is required because the repeated element will be \ - copied", - copy_path, - )) - .emit(); + if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) { + self.infcx.report_selection_error( + &traits::Obligation::new( + ObligationCause::new( + span, + self.tcx().hir().def_index_to_hir_id(self.mir_def_id.index), + traits::ObligationCauseCode::RepeatVec, + ), + self.param_env, + ty::Predicate::Trait(ty::Binder::bind(ty::TraitPredicate { + trait_ref: ty::TraitRef::new( + self.tcx().lang_items().copy_trait().unwrap(), + tcx.mk_substs_trait(ty, &[]), + ), + })), + ), + &traits::SelectionError::Unimplemented, + false, + ); } } }, diff --git a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs index fb944df14c8..5e7f11799c8 100644 --- a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs +++ b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs @@ -86,7 +86,7 @@ mod non_constants { fn no_impl_copy_empty_value_multiple_elements() { let x = None; let arr: [Option; 2] = [x; 2]; - //~^ ERROR repeated expression does not implement `std::marker::Copy` + //~^ ERROR the trait bound `std::option::Option: std::marker::Copy` is not satisfied [E0277] } fn no_impl_copy_value_no_elements() { @@ -102,7 +102,7 @@ mod non_constants { fn no_impl_copy_value_multiple_elements() { let x = Some(Bar); let arr: [Option; 2] = [x; 2]; - //~^ ERROR repeated expression does not implement `std::marker::Copy` + //~^ ERROR the trait bound `std::option::Option: std::marker::Copy` is not satisfied [E0277] } fn impl_copy_empty_value_no_elements() { diff --git a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.stderr b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.stderr index 5de0cd505eb..71f5a8ccaa2 100644 --- a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.stderr +++ b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.stderr @@ -1,18 +1,23 @@ -error: repeated expression does not implement `std::marker::Copy` +error[E0277]: the trait bound `std::option::Option: std::marker::Copy` is not satisfied --> $DIR/migrate-borrowck.rs:88:37 | LL | let arr: [Option; 2] = [x; 2]; | ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option` | - = note: the `std::marker::Copy` trait is required because the repeated element will be copied + = help: the following implementations were found: + as std::marker::Copy> + = note: the `Copy` trait is required because the repeated element will be copied -error: repeated expression does not implement `std::marker::Copy` +error[E0277]: the trait bound `std::option::Option: std::marker::Copy` is not satisfied --> $DIR/migrate-borrowck.rs:104:37 | LL | let arr: [Option; 2] = [x; 2]; | ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option` | - = note: the `std::marker::Copy` trait is required because the repeated element will be copied + = help: the following implementations were found: + as std::marker::Copy> + = note: the `Copy` trait is required because the repeated element will be copied error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.rs b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.rs index eed7be5f16e..804aa6b7c6d 100644 --- a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.rs +++ b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.rs @@ -85,7 +85,7 @@ mod non_constants { fn no_impl_copy_empty_value_multiple_elements() { let x = None; let arr: [Option; 2] = [x; 2]; - //~^ ERROR repeated expression does not implement `std::marker::Copy` + //~^ ERROR the trait bound `std::option::Option: std::marker::Copy` is not satisfied [E0277] } fn no_impl_copy_value_no_elements() { @@ -101,7 +101,7 @@ mod non_constants { fn no_impl_copy_value_multiple_elements() { let x = Some(Bar); let arr: [Option; 2] = [x; 2]; - //~^ ERROR repeated expression does not implement `std::marker::Copy` + //~^ ERROR the trait bound `std::option::Option: std::marker::Copy` is not satisfied [E0277] } fn impl_copy_empty_value_no_elements() { diff --git a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.stderr b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.stderr index 5cd6d2a8f06..0b2da5933a1 100644 --- a/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.stderr +++ b/src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.stderr @@ -1,18 +1,23 @@ -error: repeated expression does not implement `std::marker::Copy` +error[E0277]: the trait bound `std::option::Option: std::marker::Copy` is not satisfied --> $DIR/nll-borrowck.rs:87:37 | LL | let arr: [Option; 2] = [x; 2]; | ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option` | - = note: the `std::marker::Copy` trait is required because the repeated element will be copied + = help: the following implementations were found: + as std::marker::Copy> + = note: the `Copy` trait is required because the repeated element will be copied -error: repeated expression does not implement `std::marker::Copy` +error[E0277]: the trait bound `std::option::Option: std::marker::Copy` is not satisfied --> $DIR/nll-borrowck.rs:103:37 | LL | let arr: [Option; 2] = [x; 2]; | ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option` | - = note: the `std::marker::Copy` trait is required because the repeated element will be copied + = help: the following implementations were found: + as std::marker::Copy> + = note: the `Copy` trait is required because the repeated element will be copied error: aborting due to 2 previous errors +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/repeat-to-run-dtor-twice.rs b/src/test/ui/repeat-to-run-dtor-twice.rs index 14336435fe8..d857178166a 100644 --- a/src/test/ui/repeat-to-run-dtor-twice.rs +++ b/src/test/ui/repeat-to-run-dtor-twice.rs @@ -15,5 +15,5 @@ impl Drop for Foo { fn main() { let a = Foo { x: 3 }; let _ = [ a; 5 ]; - //~^ ERROR repeated expression does not implement `std::marker::Copy` + //~^ ERROR the trait bound `Foo: std::marker::Copy` is not satisfied [E0277] } diff --git a/src/test/ui/repeat-to-run-dtor-twice.stderr b/src/test/ui/repeat-to-run-dtor-twice.stderr index dff6332f61b..5434f6cef54 100644 --- a/src/test/ui/repeat-to-run-dtor-twice.stderr +++ b/src/test/ui/repeat-to-run-dtor-twice.stderr @@ -1,10 +1,11 @@ -error: repeated expression does not implement `std::marker::Copy` +error[E0277]: the trait bound `Foo: std::marker::Copy` is not satisfied --> $DIR/repeat-to-run-dtor-twice.rs:17:13 | LL | let _ = [ a; 5 ]; | ^^^^^^^^ the trait `std::marker::Copy` is not implemented for `Foo` | - = note: the `std::marker::Copy` trait is required because the repeated element will be copied + = note: the `Copy` trait is required because the repeated element will be copied error: aborting due to previous error +For more information about this error, try `rustc --explain E0277`.