-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 3 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,6 +1292,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
return tcx.ty_error(); | ||
} | ||
|
||
let is_const = match &element.kind { | ||
hir::ExprKind::ConstBlock(..) => true, | ||
hir::ExprKind::Path(qpath) => { | ||
let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id); | ||
matches!( | ||
res, | ||
Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::AnonConst, _) | ||
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. you may want to add 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. +1 |
||
) | ||
} | ||
_ => false, | ||
}; | ||
|
||
if !is_const { | ||
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 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. I don't object to this code, but I'm a bit confused what's going on here -- it seems like, in the MIR type check, we enforce that 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 mir has this check somewhat subtly. It only checks Tho I realize now that I should probably review whether things become Operand::Copy while ignoring lifetimes |
||
} | ||
} | ||
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 |
||
|
||
tcx.mk_ty(ty::Array(t, count)) | ||
} | ||
|
||
|
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/copy_modulo_regions.rs:14:5 | ||
| | ||
LL | fn foo<'a>() -> [Foo<'a>; 100] { | ||
| -- lifetime `'a` defined here | ||
LL | [mk_foo::<'a>(); 100] | ||
| ^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static` | ||
| | ||
= note: requirement occurs because of the type `Foo<'_>`, which makes the generic argument `'_` invariant | ||
= note: the struct `Foo<'a>` is invariant over the parameter `'a` | ||
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance | ||
|
||
error: aborting due to previous error | ||
|
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