From be1ea4bc7901a14e38602518bd9ec1177904e3cb Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 21 Aug 2020 07:49:15 +0000 Subject: [PATCH 1/9] Use str::to_owned for `format!` without formatting arguments --- library/alloc/src/lib.rs | 1 + library/alloc/src/macros.rs | 16 ++++- .../issue-75742-format_without_fmt_args.rs | 39 ++++++++++ src/test/pretty/issue-4264.pp | 71 +++++++++++-------- src/test/ui/borrowck/issue-64453.rs | 2 + src/test/ui/borrowck/issue-64453.stderr | 20 +++++- 6 files changed, 116 insertions(+), 33 deletions(-) create mode 100644 src/test/codegen/issue-75742-format_without_fmt_args.rs diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index b33cb3ad8e839..78ab5c7b809e5 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -97,6 +97,7 @@ #![feature(exact_size_is_empty)] #![feature(exclusive_range_pattern)] #![feature(extend_one)] +#![feature(fmt_as_str)] #![feature(fmt_internals)] #![feature(fn_traits)] #![feature(fundamental)] diff --git a/library/alloc/src/macros.rs b/library/alloc/src/macros.rs index 2f744618d6936..806234ea364c4 100644 --- a/library/alloc/src/macros.rs +++ b/library/alloc/src/macros.rs @@ -101,9 +101,21 @@ macro_rules! vec { /// ``` #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] +#[allow_internal_unstable(fmt_as_str)] macro_rules! format { ($($arg:tt)*) => {{ - let res = $crate::fmt::format($crate::__export::format_args!($($arg)*)); - res + // NOTE: `format_args!` borrows from temporaries. This means that + // `match` is necessary to extend the lifetime of the temporaries until after + // the `Arguments` is no longer used. The same pattern is used + // inside `format_args!` itself. + let r = match $crate::__export::format_args!($($arg)*) { + // HACK: We hope that constant propagation will make LLVM optimize out + // this match. + args => match args.as_str() { + Some(s) => $crate::borrow::ToOwned::to_owned(s), + None => $crate::fmt::format(args), + } + }; + r }} } diff --git a/src/test/codegen/issue-75742-format_without_fmt_args.rs b/src/test/codegen/issue-75742-format_without_fmt_args.rs new file mode 100644 index 0000000000000..3201688fb0301 --- /dev/null +++ b/src/test/codegen/issue-75742-format_without_fmt_args.rs @@ -0,0 +1,39 @@ +// min-llvm-version: 10.0.0 +// compile-flags: -C opt-level=3 +#![crate_type = "rlib"] +#![feature(format_args_capture)] + +// Make sure allocation not happen when result of `format!` is unused and +// there are no formatting arguments. + +// CHECK-LABEL: @format_wo_fmt_args +// CHECK-NEXT: {{"_ZN[^:]+"}}: +// CHECK-NEXT: ret +#[no_mangle] +pub fn format_wo_fmt_args() { + format!(""); + format!("a long story"); + format!("a long story {{"); +} + +// CHECK-LABEL: @format_wo_fmt_args_ret +// CHECK-NOT: Arguments +#[no_mangle] +pub fn format_wo_fmt_args_ret() -> String { + format!("a long story") +} + +// CHECK-LABEL: @format_w_fmt_args_ret_1 +// CHECK: alloc::fmt::format +#[no_mangle] +pub fn format_w_fmt_args_ret_1(n: usize) -> String { + format!("a long story: {}", n) +} + +// CHECK-LABEL: @format_w_fmt_args_ret_2 +// CHECK: core::fmt::ArgumentV1::from_usize +// CHECK: alloc::fmt::format +#[no_mangle] +pub fn format_w_fmt_args_ret_2(n: usize, width: usize) -> String { + format!("a long story {n:width$}") +} diff --git a/src/test/pretty/issue-4264.pp b/src/test/pretty/issue-4264.pp index 7b0a00282fbb0..dd708a3fdcf72 100644 --- a/src/test/pretty/issue-4264.pp +++ b/src/test/pretty/issue-4264.pp @@ -30,35 +30,48 @@ ({ - let res = - ((::alloc::fmt::format as - for<'r> fn(Arguments<'r>) -> String {format})(((::core::fmt::Arguments::new_v1 - as - fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test" - as - &str)] - as - [&str; 1]) - as - &[&str; 1]), - (&(match (() - as - ()) - { - () - => - ([] - as - [ArgumentV1; 0]), - } - as - [ArgumentV1; 0]) - as - &[ArgumentV1; 0])) - as - Arguments)) - as String); - (res as String) + let r = + (match ((::core::fmt::Arguments::new_v1 as + fn(&[&'static str], &[ArgumentV1]) -> Arguments {Arguments::new_v1})((&([("test" + as + &str)] + as + [&str; 1]) + as + &[&str; 1]), + (&(match (() + as + ()) + { + () + => + ([] + as + [ArgumentV1; 0]), + } + as + [ArgumentV1; 0]) + as + &[ArgumentV1; 0])) + as Arguments) { + args => + (match ((args as Arguments).as_str() as + Option<&str>) { + Some(s) => + ((::alloc::borrow::ToOwned::to_owned as + for<'r> fn(&'r str) -> ::Owned {::to_owned})((s + as + &str)) + as String), + None => + ((::alloc::fmt::format as + for<'r> fn(Arguments<'r>) -> String {format})((args + as + Arguments)) + as String), + } as String), + } as String); + (r as String) } as String); } as ()) pub type Foo = [i32; (3 as usize)]; diff --git a/src/test/ui/borrowck/issue-64453.rs b/src/test/ui/borrowck/issue-64453.rs index 3e803f3b6d8b2..8ccb8f0050685 100644 --- a/src/test/ui/borrowck/issue-64453.rs +++ b/src/test/ui/borrowck/issue-64453.rs @@ -4,6 +4,8 @@ struct Value; static settings_dir: String = format!(""); //~^ ERROR calls in statics are limited to constant functions //~| ERROR calls in statics are limited to constant functions +//~| ERROR calls in statics are limited to constant functions +//~| ERROR calls in statics are limited to constant functions fn from_string(_: String) -> Value { Value diff --git a/src/test/ui/borrowck/issue-64453.stderr b/src/test/ui/borrowck/issue-64453.stderr index fba801983cf4e..38cbb7fc9585a 100644 --- a/src/test/ui/borrowck/issue-64453.stderr +++ b/src/test/ui/borrowck/issue-64453.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of static item `settings_dir` - --> $DIR/issue-64453.rs:14:37 + --> $DIR/issue-64453.rs:16:37 | LL | let settings_data = from_string(settings_dir); | ^^^^^^^^^^^^ move occurs because `settings_dir` has type `String`, which does not implement the `Copy` trait @@ -20,7 +20,23 @@ LL | static settings_dir: String = format!(""); | = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 3 previous errors +error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants + --> $DIR/issue-64453.rs:4:31 + | +LL | static settings_dir: String = format!(""); + | ^^^^^^^^^^^ + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants + --> $DIR/issue-64453.rs:4:31 + | +LL | static settings_dir: String = format!(""); + | ^^^^^^^^^^^ + | + = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 5 previous errors Some errors have detailed explanations: E0015, E0507. For more information about an error, try `rustc --explain E0015`. From eb0ca617d22e4759e459e4b2926f7a51c6fa25ce Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 04:07:48 +0000 Subject: [PATCH 2/9] [dnm] enable fail tests --- .github/workflows/ci.yml | 5 ++--- src/ci/github-actions/ci.yml | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 490258f9c09bb..c695b2eb4dee1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,10 +46,9 @@ jobs: - name: x86_64-gnu-llvm-8 os: ubuntu-latest-xl env: {} - - name: x86_64-gnu-tools - env: - CI_ONLY_WHEN_SUBMODULES_CHANGED: 1 + - name: x86_64-gnu os: ubuntu-latest-xl + env: {} timeout-minutes: 600 runs-on: "${{ matrix.os }}" steps: diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index ea7e65a116836..99ce05813929d 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -279,9 +279,7 @@ jobs: - name: x86_64-gnu-llvm-8 <<: *job-linux-xl - - name: x86_64-gnu-tools - env: - CI_ONLY_WHEN_SUBMODULES_CHANGED: 1 + - name: x86_64-gnu <<: *job-linux-xl try: From 4c9b5a6a708767d51eb7a940677e255cf0bc3220 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 04:07:48 +0000 Subject: [PATCH 3/9] [dnm] enable fail tests --- src/ci/scripts/run-build-from-ci.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ci/scripts/run-build-from-ci.sh b/src/ci/scripts/run-build-from-ci.sh index c02117f459de0..8515fba1e86aa 100755 --- a/src/ci/scripts/run-build-from-ci.sh +++ b/src/ci/scripts/run-build-from-ci.sh @@ -15,7 +15,9 @@ export SRC=. # the environment rustup self uninstall -y || true if [ -z "${IMAGE+x}" ]; then - src/ci/run.sh + if ! src/ci/run.sh; then + cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/issue-75742-format_without_fmt_args.ll + fi else src/ci/docker/run.sh "${IMAGE}" fi From 5c38c5994de8358b4b4163bdc4b487307e7455c4 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 04:07:48 +0000 Subject: [PATCH 4/9] [dnm] enable fail tests --- src/ci/run.sh | 2 +- src/ci/scripts/run-build-from-ci.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ci/run.sh b/src/ci/run.sh index 5231aa2e76619..33d0e41fe3839 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -150,7 +150,7 @@ else ncpus=$(grep processor /proc/cpuinfo | wc -l) fi -if [ ! -z "$SCRIPT" ]; then +if [ -n "$SCRIPT" ]; then sh -x -c "$SCRIPT" else do_make() { diff --git a/src/ci/scripts/run-build-from-ci.sh b/src/ci/scripts/run-build-from-ci.sh index 8515fba1e86aa..97c2cd448a750 100755 --- a/src/ci/scripts/run-build-from-ci.sh +++ b/src/ci/scripts/run-build-from-ci.sh @@ -16,7 +16,7 @@ export SRC=. rustup self uninstall -y || true if [ -z "${IMAGE+x}" ]; then if ! src/ci/run.sh; then - cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/issue-75742-format_without_fmt_args.ll + cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/*.ll fi else src/ci/docker/run.sh "${IMAGE}" From 9b848c146ce8a2e24caab87e5368bb95d48528a7 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 04:07:48 +0000 Subject: [PATCH 5/9] [dnm] enable fail tests --- src/ci/run.sh | 6 ++++++ src/ci/scripts/run-build-from-ci.sh | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ci/run.sh b/src/ci/run.sh index 33d0e41fe3839..f13419c27d211 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -118,6 +118,12 @@ datecheck() { } datecheck trap datecheck EXIT +print_ll_files() { + echo "== printing ll file ==" + cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/*.ll + echo "== end ==" +} +trap print_ll_files EXIT # We've had problems in the past of shell scripts leaking fds into the sccache # server (#48192) which causes Cargo to erroneously think that a build script diff --git a/src/ci/scripts/run-build-from-ci.sh b/src/ci/scripts/run-build-from-ci.sh index 97c2cd448a750..c02117f459de0 100755 --- a/src/ci/scripts/run-build-from-ci.sh +++ b/src/ci/scripts/run-build-from-ci.sh @@ -15,9 +15,7 @@ export SRC=. # the environment rustup self uninstall -y || true if [ -z "${IMAGE+x}" ]; then - if ! src/ci/run.sh; then - cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/*.ll - fi + src/ci/run.sh else src/ci/docker/run.sh "${IMAGE}" fi From f575f0b1b8a89770b1e488b840346c9d69d87846 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 04:07:48 +0000 Subject: [PATCH 6/9] [dnm] enable fail tests --- src/ci/run.sh | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/ci/run.sh b/src/ci/run.sh index f13419c27d211..73b234e3e23b7 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -8,7 +8,7 @@ fi if [ "$NO_CHANGE_USER" = "" ]; then if [ "$LOCAL_USER_ID" != "" ]; then - useradd --shell /bin/bash -u $LOCAL_USER_ID -o -c "" -m user + useradd --shell /bin/bash -u "$LOCAL_USER_ID" -o -c "" -m user export HOME=/home/user unset LOCAL_USER_ID exec su --preserve-environment -c "env PATH=$PATH \"$0\"" user @@ -32,7 +32,7 @@ if [ "$SET_HARD_RLIMIT_STACK" = "1" ]; then fi fi -ci_dir=`cd $(dirname $0) && pwd` +ci_dir=$(cd "$(dirname "$0")" && pwd) source "$ci_dir/shared.sh" if command -v python > /dev/null; then @@ -119,6 +119,9 @@ datecheck() { datecheck trap datecheck EXIT print_ll_files() { + if [[ $RUST_CHECK_TARGET != x86_64-unknown-linux-gnu ]]; then + return + fi echo "== printing ll file ==" cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/*.ll echo "== end ==" @@ -132,13 +135,13 @@ trap print_ll_files EXIT SCCACHE_IDLE_TIMEOUT=10800 sccache --start-server || true if [ "$RUN_CHECK_WITH_PARALLEL_QUERIES" != "" ]; then - $SRC/configure --enable-parallel-compiler + "$SRC"/configure --enable-parallel-compiler CARGO_INCREMENTAL=0 $PYTHON ../x.py check rm -f config.toml rm -rf build fi -$SRC/configure $RUST_CONFIGURE_ARGS +"$SRC"/configure "$RUST_CONFIGURE_ARGS" retry make prepare @@ -153,7 +156,7 @@ if isMacOS; then else cat /proc/cpuinfo || true cat /proc/meminfo || true - ncpus=$(grep processor /proc/cpuinfo | wc -l) + ncpus=$(grep -c processor /proc/cpuinfo) fi if [ -n "$SCRIPT" ]; then @@ -161,7 +164,7 @@ if [ -n "$SCRIPT" ]; then else do_make() { echo "make -j $ncpus $1" - make -j $ncpus $1 + make -j "$ncpus" "$1" local retval=$? return $retval } From 37759c38326845d1f396ec5b946237686c0c5b76 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 08:55:16 +0000 Subject: [PATCH 7/9] Revert "[dnm] enable fail tests" This reverts commit f575f0b1b8a89770b1e488b840346c9d69d87846. --- src/ci/run.sh | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/ci/run.sh b/src/ci/run.sh index 73b234e3e23b7..f13419c27d211 100755 --- a/src/ci/run.sh +++ b/src/ci/run.sh @@ -8,7 +8,7 @@ fi if [ "$NO_CHANGE_USER" = "" ]; then if [ "$LOCAL_USER_ID" != "" ]; then - useradd --shell /bin/bash -u "$LOCAL_USER_ID" -o -c "" -m user + useradd --shell /bin/bash -u $LOCAL_USER_ID -o -c "" -m user export HOME=/home/user unset LOCAL_USER_ID exec su --preserve-environment -c "env PATH=$PATH \"$0\"" user @@ -32,7 +32,7 @@ if [ "$SET_HARD_RLIMIT_STACK" = "1" ]; then fi fi -ci_dir=$(cd "$(dirname "$0")" && pwd) +ci_dir=`cd $(dirname $0) && pwd` source "$ci_dir/shared.sh" if command -v python > /dev/null; then @@ -119,9 +119,6 @@ datecheck() { datecheck trap datecheck EXIT print_ll_files() { - if [[ $RUST_CHECK_TARGET != x86_64-unknown-linux-gnu ]]; then - return - fi echo "== printing ll file ==" cat build/x86_64-unknown-linux-gnu/test/codegen/issue-75742-format_without_fmt_args/*.ll echo "== end ==" @@ -135,13 +132,13 @@ trap print_ll_files EXIT SCCACHE_IDLE_TIMEOUT=10800 sccache --start-server || true if [ "$RUN_CHECK_WITH_PARALLEL_QUERIES" != "" ]; then - "$SRC"/configure --enable-parallel-compiler + $SRC/configure --enable-parallel-compiler CARGO_INCREMENTAL=0 $PYTHON ../x.py check rm -f config.toml rm -rf build fi -"$SRC"/configure "$RUST_CONFIGURE_ARGS" +$SRC/configure $RUST_CONFIGURE_ARGS retry make prepare @@ -156,7 +153,7 @@ if isMacOS; then else cat /proc/cpuinfo || true cat /proc/meminfo || true - ncpus=$(grep -c processor /proc/cpuinfo) + ncpus=$(grep processor /proc/cpuinfo | wc -l) fi if [ -n "$SCRIPT" ]; then @@ -164,7 +161,7 @@ if [ -n "$SCRIPT" ]; then else do_make() { echo "make -j $ncpus $1" - make -j "$ncpus" "$1" + make -j $ncpus $1 local retval=$? return $retval } From 81d8490b50847671e155d00226169ede0610a68e Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 08:55:16 +0000 Subject: [PATCH 8/9] Revert "[dnm] enable fail tests" This reverts commit f575f0b1b8a89770b1e488b840346c9d69d87846. --- src/ci/github-actions/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ci/github-actions/ci.yml b/src/ci/github-actions/ci.yml index 99ce05813929d..2234cc90fd5f6 100644 --- a/src/ci/github-actions/ci.yml +++ b/src/ci/github-actions/ci.yml @@ -273,11 +273,11 @@ jobs: strategy: matrix: include: - - name: mingw-check - <<: *job-linux-xl + # - name: mingw-check + # <<: *job-linux-xl - - name: x86_64-gnu-llvm-8 - <<: *job-linux-xl + # - name: x86_64-gnu-llvm-8 + # <<: *job-linux-xl - name: x86_64-gnu <<: *job-linux-xl From 9195bc21f04b416391b2db4ba1da21b1019c3dd3 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 28 Sep 2020 08:55:16 +0000 Subject: [PATCH 9/9] Revert "[dnm] enable fail tests" This reverts commit f575f0b1b8a89770b1e488b840346c9d69d87846. --- .github/workflows/ci.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c695b2eb4dee1..f83195fcefc6d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,12 +40,6 @@ jobs: strategy: matrix: include: - - name: mingw-check - os: ubuntu-latest-xl - env: {} - - name: x86_64-gnu-llvm-8 - os: ubuntu-latest-xl - env: {} - name: x86_64-gnu os: ubuntu-latest-xl env: {}