Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
c2e849c
added a suggestion to create a `const` item if the `fn` in the array …
henryboisdequin Jan 29, 2021
bfd1ccf
Seal the CommandExt, OsStrExt and OsStringExt traits
Amanieu Feb 10, 2021
5fe8490
Add match pattern diagnostics regression test
vandenheuvel Feb 8, 2021
dd9db23
Fix typos in BTreeSet::{first, last} docs
taiki-e Feb 13, 2021
d6dcb3d
CTFE validation: catch ReadPointerAsBytes and better error
RalfJung Feb 13, 2021
a3e0795
Heat up the ICE-y error reporting
BoxyUwU Feb 13, 2021
ee9709f
Fixed minor typo in catch_unwind docs
Feb 13, 2021
68405fd
debug!("paramenv={}paramenv={}paramenv={}paramenv={}")
BoxyUwU Feb 13, 2021
b6144e7
yeet ya fixme into the void
BoxyUwU Feb 13, 2021
a419e11
a wild test has appeared uwu
BoxyUwU Feb 13, 2021
1c3841e
Edit `rustc_arena::DropArena` docs
pierwill Feb 13, 2021
64fe2c1
update message
henryboisdequin Feb 14, 2021
d1a541e
Add tests for Atomic*::fetch_{min,max}
bjorn3 Feb 14, 2021
7bd7126
param_env debugs are instrumental to rustc's success
BoxyUwU Feb 14, 2021
ba72bc9
fix typo
TaKO8Ki Feb 14, 2021
c583860
Remove unnecessary `Option` in `default_doc`
jyn514 Feb 14, 2021
0038eae
rustc_span: Remove obsolete `allow_internal_unstable_backcompat_hack`
petrochenkov Feb 14, 2021
18c94b3
expand: Remove obsolete `ExpansionConfig::keep_macs`
petrochenkov Feb 14, 2021
6e11a8b
expand: Remove redundant calls to configure
petrochenkov Feb 14, 2021
dee5424
Add missing env!-decl variant
lukaslueg Feb 14, 2021
1aa9651
Fix typo in link to CreateSymbolicLinkW documentation.
m-ou-se Feb 14, 2021
3d7fcff
Update library/core/src/macros/mod.rs
lukaslueg Feb 14, 2021
4613b37
Stabilize Arguments::as_str
sfackler Feb 14, 2021
63806cc
Remove redundant bool_to_option feature gate
est31 Feb 15, 2021
ec77574
Update link for extern prelude.
ehuss Feb 15, 2021
fd21eb1
32-bit ARM: Emit `lr` instead of `r14` when specified as an `asm!` ou…
Feb 15, 2021
6931027
:arrow_up: rust-analyzer
lnicola Feb 15, 2021
f7c658f
Rollup merge of #81503 - henryboisdequin:fix-const-fn-arr-err-msg, r=…
jonas-schievink Feb 15, 2021
d2c18d8
Rollup merge of #81897 - vandenheuvel:match_exhaustive_diagnostics_re…
jonas-schievink Feb 15, 2021
5735e5d
Rollup merge of #81975 - Amanieu:seal2, r=m-ou-se
jonas-schievink Feb 15, 2021
d5f0877
Rollup merge of #82009 - BoxyUwU:idontknooow, r=varkor
jonas-schievink Feb 15, 2021
8667478
Rollup merge of #82060 - taiki-e:typo, r=m-ou-se
jonas-schievink Feb 15, 2021
95d3159
Rollup merge of #82061 - RalfJung:ctfe-read-pointer-as-bytes, r=oli-obk
jonas-schievink Feb 15, 2021
c9a104c
Rollup merge of #82063 - NULLx76:fix-minor-typo, r=jonas-schievink
jonas-schievink Feb 15, 2021
ada22ee
Rollup merge of #82067 - BoxyUwU:hahaicantthinkofabadpun, r=oli-obk
jonas-schievink Feb 15, 2021
1b97090
Rollup merge of #82077 - pierwill:edit-droparena, r=lcnr
jonas-schievink Feb 15, 2021
0deeba3
Rollup merge of #82093 - bjorn3:more_atomic_tests, r=kennytm
jonas-schievink Feb 15, 2021
b9cb948
Rollup merge of #82096 - TaKO8Ki:fix-typo, r=GuillaumeGomez
jonas-schievink Feb 15, 2021
f4f052a
Rollup merge of #82106 - jyn514:cleanup-bootstrap, r=Mark-Simulacrum
jonas-schievink Feb 15, 2021
19da712
Rollup merge of #82107 - petrochenkov:minexpclean, r=Aaron1011
jonas-schievink Feb 15, 2021
47c8b4d
Rollup merge of #82118 - lukaslueg:env_decl, r=m-ou-se
jonas-schievink Feb 15, 2021
a5ad91d
Rollup merge of #82119 - m-ou-se:typo, r=dtolnay
jonas-schievink Feb 15, 2021
dfd87a4
Rollup merge of #82120 - sfackler:arguments-as-str, r=dtolnay
jonas-schievink Feb 15, 2021
0d71d96
Rollup merge of #82129 - est31:master, r=jyn514
jonas-schievink Feb 15, 2021
737f2b4
Rollup merge of #82133 - ehuss:extern-prelude-link, r=jyn514
jonas-schievink Feb 15, 2021
f2f0b8b
Rollup merge of #82141 - jrvanwhy:issue-82052, r=sanxiyn
jonas-schievink Feb 15, 2021
4f47637
Rollup merge of #82147 - lnicola:rust-analyzer-2021-02-15, r=jonas-sc…
jonas-schievink Feb 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ pub enum ObligationCauseCode<'tcx> {
/// Inline asm operand type must be `Sized`.
InlineAsmSized,
/// `[T, ..n]` implies that `T` must be `Copy`.
RepeatVec,
/// If the function in the array repeat expression is a `const fn`,
/// display a help message suggesting to move the function call to a
/// new `const` item while saying that `T` doesn't implement `Copy`.
RepeatVec(bool),

/// Types of fields (other than the last, except for packed structs) in a struct must be sized.
FieldSized {
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_mir/src/borrow_check/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ use rustc_trait_selection::traits::{self, ObligationCause, PredicateObligations}
use crate::dataflow::impls::MaybeInitializedPlaces;
use crate::dataflow::move_paths::MoveData;
use crate::dataflow::ResultsCursor;
use crate::transform::{
check_consts::ConstCx, promote_consts::is_const_fn_in_array_repeat_expression,
};

use crate::borrow_check::{
borrow_set::BorrowSet,
Expand Down Expand Up @@ -1988,18 +1991,24 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Operand::Copy(..) | Operand::Constant(..) => {
// These are always okay: direct use of a const, or a value that can evidently be copied.
}
Operand::Move(_) => {
Operand::Move(place) => {
// Make sure that repeated elements implement `Copy`.
let span = body.source_info(location).span;
let ty = operand.ty(body, tcx);
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
let ccx = ConstCx::new_with_param_env(tcx, body, self.param_env);
let is_const_fn =
is_const_fn_in_array_repeat_expression(&ccx, &place, &body);

debug!("check_rvalue: is_const_fn={:?}", is_const_fn);

let def_id = body.source.def_id().expect_local();
self.infcx.report_selection_error(
&traits::Obligation::new(
ObligationCause::new(
span,
self.tcx().hir().local_def_id_to_hir_id(def_id),
traits::ObligationCauseCode::RepeatVec,
traits::ObligationCauseCode::RepeatVec(is_const_fn),
),
self.param_env,
ty::Binder::bind(ty::TraitRef::new(
Expand Down
35 changes: 35 additions & 0 deletions compiler/rustc_mir/src/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,3 +1231,38 @@ pub fn promote_candidates<'tcx>(

promotions
}

/// This function returns `true` if the function being called in the array
/// repeat expression is a `const` function.
crate fn is_const_fn_in_array_repeat_expression<'tcx>(
ccx: &ConstCx<'_, 'tcx>,
place: &Place<'tcx>,
body: &Body<'tcx>,
) -> bool {
match place.as_local() {
// rule out cases such as: `let my_var = some_fn(); [my_var; N]`
Some(local) if body.local_decls[local].is_user_variable() => return false,
None => return false,
_ => {}
}

for block in body.basic_blocks() {
if let Some(Terminator { kind: TerminatorKind::Call { func, destination, .. }, .. }) =
&block.terminator
{
if let Operand::Constant(box Constant { literal: ty::Const { ty, .. }, .. }) = func {
if let ty::FnDef(def_id, _) = *ty.kind() {
if let Some((destination_place, _)) = destination {
if destination_place == place {
if is_const_fn(ccx.tcx, def_id) {
return true;
}
}
}
}
}
}
}

false
}
Original file line number Diff line number Diff line change
Expand Up @@ -1881,10 +1881,26 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
ObligationCauseCode::Coercion { source: _, target } => {
err.note(&format!("required by cast to type `{}`", self.ty_to_string(target)));
}
ObligationCauseCode::RepeatVec => {
ObligationCauseCode::RepeatVec(is_const_fn) => {
err.note(
"the `Copy` trait is required because the repeated element will be copied",
);

if is_const_fn {
err.help(
"consider creating a new `const` item and initializing with the result \
of the function call to be used in the repeat position, like \
`const VAL: Type = const_fn();` and `let x = [VAL; 42];`",
);
}

if self.tcx.sess.is_nightly_build() && is_const_fn {
err.help(
"create an inline `const` block, see PR \
#2920 <https://github.com/rust-lang/rfcs/pull/2920> \
for more information",
);
}
}
ObligationCauseCode::VariableType(hir_id) => {
let parent_node = self.tcx.hir().get_parent_node(hir_id);
Expand Down
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
This directory contains the source code of the rust project, including:

- The test suite
- The bootstrapping build system
- Various submodules for tools, like rustdoc, rls, etc.
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/consts/const-blocks/fn-call-in-non-const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ LL | let _: [Option<Bar>; 2] = [no_copy(); 2];
= help: the following implementations were found:
<Option<T> as Copy>
= note: the `Copy` trait is required because the repeated element will be copied
= help: consider creating a new `const` item and initializing with the result of the function call to be used in the repeat position, like `const VAL: Type = const_fn();` and `let x = [VAL; 42];`
= help: create an inline `const` block, see PR #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information

error: aborting due to previous error

Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/consts/const-fn-in-vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
// should hint to create an inline `const` block
// or to create a new `const` item
let strings: [String; 5] = [String::new(); 5];
//~^ ERROR the trait bound `String: Copy` is not satisfied
println!("{:?}", strings);
}
13 changes: 13 additions & 0 deletions src/test/ui/consts/const-fn-in-vec.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0277]: the trait bound `String: Copy` is not satisfied
--> $DIR/const-fn-in-vec.rs:4:32
|
LL | let strings: [String; 5] = [String::new(); 5];
| ^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
|
= note: the `Copy` trait is required because the repeated element will be copied
= help: consider creating a new `const` item and initializing with the result of the function call to be used in the repeat position, like `const VAL: Type = const_fn();` and `let x = [VAL; 42];`
= help: create an inline `const` block, see PR #2920 <https://github.com/rust-lang/rfcs/pull/2920> for more information

error: aborting due to previous error

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