Skip to content

Commit

Permalink
rustc_mir: Re-use report_selection_error.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davidtwco committed Jul 7, 2019
1 parent 3cca4ce commit 813c994
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/librustc/traits/error_reporting.rs
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/traits/mod.rs
Expand Up @@ -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 },
Expand Down
1 change: 1 addition & 0 deletions src/librustc/traits/structural_impls.rs
Expand Up @@ -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),
Expand Down
40 changes: 20 additions & 20 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
);
}
}
},
Expand Down
Expand Up @@ -86,7 +86,7 @@ mod non_constants {
fn no_impl_copy_empty_value_multiple_elements() {
let x = None;
let arr: [Option<Bar>; 2] = [x; 2];
//~^ ERROR repeated expression does not implement `std::marker::Copy`
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied [E0277]
}

fn no_impl_copy_value_no_elements() {
Expand All @@ -102,7 +102,7 @@ mod non_constants {
fn no_impl_copy_value_multiple_elements() {
let x = Some(Bar);
let arr: [Option<Bar>; 2] = [x; 2];
//~^ ERROR repeated expression does not implement `std::marker::Copy`
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied [E0277]
}

fn impl_copy_empty_value_no_elements() {
Expand Down
@@ -1,18 +1,23 @@
error: repeated expression does not implement `std::marker::Copy`
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
--> $DIR/migrate-borrowck.rs:88:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
|
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
= help: the following implementations were found:
<std::option::Option<T> 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<Bar>: std::marker::Copy` is not satisfied
--> $DIR/migrate-borrowck.rs:104:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
|
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
= help: the following implementations were found:
<std::option::Option<T> 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`.
Expand Up @@ -85,7 +85,7 @@ mod non_constants {
fn no_impl_copy_empty_value_multiple_elements() {
let x = None;
let arr: [Option<Bar>; 2] = [x; 2];
//~^ ERROR repeated expression does not implement `std::marker::Copy`
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied [E0277]
}

fn no_impl_copy_value_no_elements() {
Expand All @@ -101,7 +101,7 @@ mod non_constants {
fn no_impl_copy_value_multiple_elements() {
let x = Some(Bar);
let arr: [Option<Bar>; 2] = [x; 2];
//~^ ERROR repeated expression does not implement `std::marker::Copy`
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied [E0277]
}

fn impl_copy_empty_value_no_elements() {
Expand Down
@@ -1,18 +1,23 @@
error: repeated expression does not implement `std::marker::Copy`
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
--> $DIR/nll-borrowck.rs:87:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
|
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
= help: the following implementations were found:
<std::option::Option<T> 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<Bar>: std::marker::Copy` is not satisfied
--> $DIR/nll-borrowck.rs:103:37
|
LL | let arr: [Option<Bar>; 2] = [x; 2];
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
|
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
= help: the following implementations were found:
<std::option::Option<T> 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`.
2 changes: 1 addition & 1 deletion src/test/ui/repeat-to-run-dtor-twice.rs
Expand Up @@ -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]
}
5 changes: 3 additions & 2 deletions 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`.

0 comments on commit 813c994

Please sign in to comment.