-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce Copy bounds for repeat elements while considering lifetimes #95819
Changes from all commits
4e6e68e
bc14b6b
018f934
be54947
67ce547
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1292,9 +1292,49 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
return tcx.ty_error(); | ||
} | ||
|
||
self.check_repeat_element_needs_copy_bound(element, count, element_ty); | ||
|
||
tcx.mk_ty(ty::Array(t, count)) | ||
} | ||
|
||
fn check_repeat_element_needs_copy_bound( | ||
&self, | ||
element: &hir::Expr<'_>, | ||
count: ty::Const<'tcx>, | ||
element_ty: Ty<'tcx>, | ||
) { | ||
let tcx = self.tcx; | ||
// Actual constants as the repeat element get inserted repeatedly instead of getting copied via Copy. | ||
match &element.kind { | ||
hir::ExprKind::ConstBlock(..) => return, | ||
hir::ExprKind::Path(qpath) => { | ||
let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id); | ||
if let Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::AnonConst, _) = res | ||
{ | ||
return; | ||
} | ||
} | ||
_ => {} | ||
} | ||
// If someone calls a const fn, they can extract that call out into a separate constant (or a const | ||
// block in the future), so we check that to tell them that in the diagnostic. Does not affect typeck. | ||
let is_const_fn = match element.kind { | ||
hir::ExprKind::Call(func, _args) => match *self.node_ty(func.hir_id).kind() { | ||
ty::FnDef(def_id, _) => tcx.is_const_fn(def_id), | ||
_ => false, | ||
}, | ||
_ => false, | ||
}; | ||
|
||
// If the length is 0, we don't create any elements, so we don't copy any. If the length is 1, we | ||
// don't copy that one element, we move it. Only check for Copy if the length is larger. | ||
if count.try_eval_usize(tcx, self.param_env).map_or(true, |len| len > 1) { | ||
let lang_item = self.tcx.require_lang_item(LangItem::Copy, None); | ||
let code = traits::ObligationCauseCode::RepeatElementCopy { is_const_fn }; | ||
self.require_type_meets(element_ty, element.span, code, lang_item); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we move that entire block to its own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Damn I love rust analyzer. Just selected the code and clicked "extract into fn". Add some lifetimes to types and remove the generated |
||
} | ||
|
||
fn check_expr_tuple( | ||
&self, | ||
elts: &'tcx [hir::Expr<'tcx>], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
error[E0277]: the trait bound `Header<'_>: Copy` is not satisfied | ||
--> $DIR/repeat_empty_ok.rs:8:19 | ||
--> $DIR/repeat_empty_ok.rs:8:20 | ||
| | ||
LL | let headers = [Header{value: &[]}; 128]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>` | ||
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>` | ||
| | ||
= note: the `Copy` trait is required because the repeated element will be copied | ||
= note: the `Copy` trait is required because this value will be copied for each element of the array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only nitpick I'd have with the output would be to point at the whole array, But I think that'd be useful only in uncommon code, so it's not worth it to change it in this PR, particularly with the verbosity tradeoff:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We had this before. I could revert the spans to point at the entire array again, but I thought it was actually neat that we were just pointing to the repeat element now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nono, what we have now it's good. |
||
help: consider annotating `Header<'_>` with `#[derive(Copy)]` | ||
| | ||
LL | #[derive(Copy)] | ||
| | ||
|
||
error[E0277]: the trait bound `Header<'_>: Copy` is not satisfied | ||
--> $DIR/repeat_empty_ok.rs:13:19 | ||
--> $DIR/repeat_empty_ok.rs:13:20 | ||
| | ||
LL | let headers = [Header{value: &[0]}; 128]; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>` | ||
| ^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `Header<'_>` | ||
| | ||
= note: the `Copy` trait is required because the repeated element will be copied | ||
= note: the `Copy` trait is required because this value will be copied for each element of the array | ||
help: consider annotating `Header<'_>` with `#[derive(Copy)]` | ||
| | ||
LL | #[derive(Copy)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![feature(nll)] | ||
|
||
#[derive(Clone)] | ||
struct Foo<'a>(fn(&'a ()) -> &'a ()); | ||
|
||
impl Copy for Foo<'static> {} | ||
|
||
fn mk_foo<'a>() -> Foo<'a> { | ||
println!("mk_foo"); | ||
Foo(|x| x) | ||
} | ||
|
||
fn foo<'a>() -> [Foo<'a>; 100] { | ||
[mk_foo::<'a>(); 100] //~ ERROR lifetime may not live long enough | ||
} | ||
|
||
fn main() { | ||
foo(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that function is now unused, so you can remove it