Skip to content

Commit 813c994

Browse files
committed
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.
1 parent 3cca4ce commit 813c994

File tree

10 files changed

+53
-35
lines changed

10 files changed

+53
-35
lines changed

src/librustc/traits/error_reporting.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15641564
err.note(&format!("required for the cast to the object type `{}`",
15651565
self.ty_to_string(object_ty)));
15661566
}
1567+
ObligationCauseCode::RepeatVec => {
1568+
err.note("the `Copy` trait is required because the \
1569+
repeated element will be copied");
1570+
}
15671571
ObligationCauseCode::VariableType(_) => {
15681572
err.note("all local variables must have a statically known size");
15691573
if !self.tcx.features().unsized_locals {

src/librustc/traits/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ pub enum ObligationCauseCode<'tcx> {
195195
SizedReturnType,
196196
/// Yield type must be Sized
197197
SizedYieldType,
198+
/// [T,..n] --> T must be Copy
199+
RepeatVec,
198200

199201
/// Types of fields (other than the last, except for packed structs) in a struct must be sized.
200202
FieldSized { adt_kind: AdtKind, last: bool },

src/librustc/traits/structural_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ impl<'a, 'tcx> Lift<'tcx> for traits::ObligationCauseCode<'a> {
488488
super::SizedArgumentType => Some(super::SizedArgumentType),
489489
super::SizedReturnType => Some(super::SizedReturnType),
490490
super::SizedYieldType => Some(super::SizedYieldType),
491+
super::RepeatVec => Some(super::RepeatVec),
491492
super::FieldSized { adt_kind, last } => Some(super::FieldSized { adt_kind, last }),
492493
super::ConstSized => Some(super::ConstSized),
493494
super::SharedStatic => Some(super::SharedStatic),

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc::mir::*;
3535
use rustc::traits::query::type_op;
3636
use rustc::traits::query::type_op::custom::CustomTypeOp;
3737
use rustc::traits::query::{Fallible, NoSolution};
38-
use rustc::traits::{ObligationCause, PredicateObligations};
38+
use rustc::traits::{self, ObligationCause, PredicateObligations};
3939
use rustc::ty::adjustment::{PointerCast};
4040
use rustc::ty::fold::TypeFoldable;
4141
use rustc::ty::subst::{Subst, SubstsRef, UnpackedKind, UserSubsts};
@@ -1968,25 +1968,25 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19681968
// a required check to make sure that repeated elements implement `Copy`.
19691969
let span = body.source_info(location).span;
19701970
let ty = operand.ty(body, tcx);
1971-
let is_copy = self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span);
1972-
if !is_copy {
1973-
let copy_path = self.tcx().def_path_str(
1974-
self.tcx().lang_items().copy_trait().unwrap());
1975-
self.tcx().sess
1976-
.struct_span_err(
1977-
span,
1978-
&format!("repeated expression does not implement `{}`", copy_path),
1979-
)
1980-
.span_label(span, &format!(
1981-
"the trait `{}` is not implemented for `{}`",
1982-
copy_path, ty,
1983-
))
1984-
.note(&format!(
1985-
"the `{}` trait is required because the repeated element will be \
1986-
copied",
1987-
copy_path,
1988-
))
1989-
.emit();
1971+
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
1972+
self.infcx.report_selection_error(
1973+
&traits::Obligation::new(
1974+
ObligationCause::new(
1975+
span,
1976+
self.tcx().hir().def_index_to_hir_id(self.mir_def_id.index),
1977+
traits::ObligationCauseCode::RepeatVec,
1978+
),
1979+
self.param_env,
1980+
ty::Predicate::Trait(ty::Binder::bind(ty::TraitPredicate {
1981+
trait_ref: ty::TraitRef::new(
1982+
self.tcx().lang_items().copy_trait().unwrap(),
1983+
tcx.mk_substs_trait(ty, &[]),
1984+
),
1985+
})),
1986+
),
1987+
&traits::SelectionError::Unimplemented,
1988+
false,
1989+
);
19901990
}
19911991
}
19921992
},

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/migrate-borrowck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ mod non_constants {
8686
fn no_impl_copy_empty_value_multiple_elements() {
8787
let x = None;
8888
let arr: [Option<Bar>; 2] = [x; 2];
89-
//~^ ERROR repeated expression does not implement `std::marker::Copy`
89+
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied [E0277]
9090
}
9191

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

108108
fn impl_copy_empty_value_no_elements() {
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
error: repeated expression does not implement `std::marker::Copy`
1+
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
22
--> $DIR/migrate-borrowck.rs:88:37
33
|
44
LL | let arr: [Option<Bar>; 2] = [x; 2];
55
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
66
|
7-
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
7+
= help: the following implementations were found:
8+
<std::option::Option<T> as std::marker::Copy>
9+
= note: the `Copy` trait is required because the repeated element will be copied
810

9-
error: repeated expression does not implement `std::marker::Copy`
11+
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
1012
--> $DIR/migrate-borrowck.rs:104:37
1113
|
1214
LL | let arr: [Option<Bar>; 2] = [x; 2];
1315
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
1416
|
15-
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
17+
= help: the following implementations were found:
18+
<std::option::Option<T> as std::marker::Copy>
19+
= note: the `Copy` trait is required because the repeated element will be copied
1620

1721
error: aborting due to 2 previous errors
1822

23+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/consts/rfc-2203-const-array-repeat-exprs/nll-borrowck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mod non_constants {
8585
fn no_impl_copy_empty_value_multiple_elements() {
8686
let x = None;
8787
let arr: [Option<Bar>; 2] = [x; 2];
88-
//~^ ERROR repeated expression does not implement `std::marker::Copy`
88+
//~^ ERROR the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied [E0277]
8989
}
9090

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

107107
fn impl_copy_empty_value_no_elements() {
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
error: repeated expression does not implement `std::marker::Copy`
1+
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
22
--> $DIR/nll-borrowck.rs:87:37
33
|
44
LL | let arr: [Option<Bar>; 2] = [x; 2];
55
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
66
|
7-
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
7+
= help: the following implementations were found:
8+
<std::option::Option<T> as std::marker::Copy>
9+
= note: the `Copy` trait is required because the repeated element will be copied
810

9-
error: repeated expression does not implement `std::marker::Copy`
11+
error[E0277]: the trait bound `std::option::Option<Bar>: std::marker::Copy` is not satisfied
1012
--> $DIR/nll-borrowck.rs:103:37
1113
|
1214
LL | let arr: [Option<Bar>; 2] = [x; 2];
1315
| ^^^^^^ the trait `std::marker::Copy` is not implemented for `std::option::Option<Bar>`
1416
|
15-
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
17+
= help: the following implementations were found:
18+
<std::option::Option<T> as std::marker::Copy>
19+
= note: the `Copy` trait is required because the repeated element will be copied
1620

1721
error: aborting due to 2 previous errors
1822

23+
For more information about this error, try `rustc --explain E0277`.

src/test/ui/repeat-to-run-dtor-twice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ impl Drop for Foo {
1515
fn main() {
1616
let a = Foo { x: 3 };
1717
let _ = [ a; 5 ];
18-
//~^ ERROR repeated expression does not implement `std::marker::Copy`
18+
//~^ ERROR the trait bound `Foo: std::marker::Copy` is not satisfied [E0277]
1919
}
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: repeated expression does not implement `std::marker::Copy`
1+
error[E0277]: the trait bound `Foo: std::marker::Copy` is not satisfied
22
--> $DIR/repeat-to-run-dtor-twice.rs:17:13
33
|
44
LL | let _ = [ a; 5 ];
55
| ^^^^^^^^ the trait `std::marker::Copy` is not implemented for `Foo`
66
|
7-
= note: the `std::marker::Copy` trait is required because the repeated element will be copied
7+
= note: the `Copy` trait is required because the repeated element will be copied
88

99
error: aborting due to previous error
1010

11+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)