From 57e67e4ab21b466158a3e7eeaf598baefc18ad72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Sat, 27 May 2023 21:25:25 +0800 Subject: [PATCH 1/8] Don't suggest changing {ImmRef,MutRef} implicit self to be mutable --- .../src/diagnostics/mutability_errors.rs | 28 ++++++++++++++---- tests/ui/borrowck/issue-111554.rs | 28 ++++++++++++++++++ tests/ui/borrowck/issue-111554.stderr | 29 +++++++++++++++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 tests/ui/borrowck/issue-111554.rs create mode 100644 tests/ui/borrowck/issue-111554.stderr diff --git a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs index d0e17bf5a0848..34d466db2b409 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs @@ -416,12 +416,28 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { _, ) = pat.kind { - err.span_suggestion( - upvar_ident.span, - "consider changing this to be mutable", - format!("mut {}", upvar_ident.name), - Applicability::MachineApplicable, - ); + if upvar_ident.name == kw::SelfLower { + for (_, node) in self.infcx.tcx.hir().parent_iter(upvar_hir_id) { + if let Some(fn_decl) = node.fn_decl() { + if !matches!(fn_decl.implicit_self, hir::ImplicitSelfKind::ImmRef | hir::ImplicitSelfKind::MutRef) { + err.span_suggestion( + upvar_ident.span, + "consider changing this to be mutable", + format!("mut {}", upvar_ident.name), + Applicability::MachineApplicable, + ); + break; + } + } + } + } else { + err.span_suggestion( + upvar_ident.span, + "consider changing this to be mutable", + format!("mut {}", upvar_ident.name), + Applicability::MachineApplicable, + ); + } } let tcx = self.infcx.tcx; diff --git a/tests/ui/borrowck/issue-111554.rs b/tests/ui/borrowck/issue-111554.rs new file mode 100644 index 0000000000000..0dad55be3acff --- /dev/null +++ b/tests/ui/borrowck/issue-111554.rs @@ -0,0 +1,28 @@ +struct Foo {} + +impl Foo { + pub fn foo(&mut self) { + || bar(&mut self); + //~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable + } + + pub fn baz(&self) { + || bar(&mut self); + //~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable + //~| ERROR cannot borrow data in a `&` reference as mutable + } + + pub fn qux(mut self) { + || bar(&mut self); + // OK + } + + pub fn quux(self) { + || bar(&mut self); + //~^ ERROR cannot borrow `self` as mutable, as it is not declared as mutable + } +} + +fn bar(_: &mut Foo) {} + +fn main() {} diff --git a/tests/ui/borrowck/issue-111554.stderr b/tests/ui/borrowck/issue-111554.stderr new file mode 100644 index 0000000000000..6b7a42e495999 --- /dev/null +++ b/tests/ui/borrowck/issue-111554.stderr @@ -0,0 +1,29 @@ +error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable + --> $DIR/issue-111554.rs:5:16 + | +LL | || bar(&mut self); + | ^^^^^^^^^ cannot borrow as mutable + +error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable + --> $DIR/issue-111554.rs:10:16 + | +LL | || bar(&mut self); + | ^^^^^^^^^ cannot borrow as mutable + +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/issue-111554.rs:10:16 + | +LL | || bar(&mut self); + | ^^^^^^^^^ cannot borrow as mutable + +error[E0596]: cannot borrow `self` as mutable, as it is not declared as mutable + --> $DIR/issue-111554.rs:21:16 + | +LL | pub fn quux(self) { + | ---- help: consider changing this to be mutable: `mut self` +LL | || bar(&mut self); + | ^^^^^^^^^ cannot borrow as mutable + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0596`. From 2a7c6a99ef5c43311a430a07bd4eeab96a7c5d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Fri, 2 Jun 2023 14:40:44 +0800 Subject: [PATCH 2/8] Fix suggestion for matching struct with `..` on both ends --- compiler/rustc_parse/src/parser/pat.rs | 57 ++++++++++++++++++++------ tests/ui/parser/issue-112188.fixed | 14 +++++++ tests/ui/parser/issue-112188.rs | 14 +++++++ tests/ui/parser/issue-112188.stderr | 37 +++++++++++++++++ tests/ui/parser/issue-49257.stderr | 2 +- 5 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 tests/ui/parser/issue-112188.fixed create mode 100644 tests/ui/parser/issue-112188.rs create mode 100644 tests/ui/parser/issue-112188.stderr diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index c317d96368ee6..fdf365178474d 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -938,7 +938,8 @@ impl<'a> Parser<'a> { let mut etc = false; let mut ate_comma = true; let mut delayed_err: Option> = None; - let mut etc_span = None; + let mut first_etc_and_maybe_comma_span = None; + let mut last_non_comma_dotdot_span = None; while self.token != token::CloseDelim(Delimiter::Brace) { let attrs = match self.parse_outer_attributes() { @@ -969,12 +970,27 @@ impl<'a> Parser<'a> { { etc = true; let mut etc_sp = self.token.span; + if first_etc_and_maybe_comma_span.is_none() { + if let Some(comma_tok) = self + .look_ahead(1, |t| if *t == token::Comma { Some(t.clone()) } else { None }) + { + let nw_span = self + .sess + .source_map() + .span_extend_to_line(comma_tok.span) + .trim_start(comma_tok.span.shrink_to_lo()) + .map(|s| self.sess.source_map().span_until_non_whitespace(s)); + first_etc_and_maybe_comma_span = nw_span.map(|s| etc_sp.to(s)); + } else { + first_etc_and_maybe_comma_span = + Some(self.sess.source_map().span_until_non_whitespace(etc_sp)); + } + } self.recover_bad_dot_dot(); self.bump(); // `..` || `...` || `_` if self.token == token::CloseDelim(Delimiter::Brace) { - etc_span = Some(etc_sp); break; } let token_str = super::token_descr(&self.token); @@ -996,7 +1012,6 @@ impl<'a> Parser<'a> { ate_comma = true; } - etc_span = Some(etc_sp.until(self.token.span)); if self.token == token::CloseDelim(Delimiter::Brace) { // If the struct looks otherwise well formed, recover and continue. if let Some(sp) = comma_sp { @@ -1040,6 +1055,9 @@ impl<'a> Parser<'a> { } }?; ate_comma = this.eat(&token::Comma); + + last_non_comma_dotdot_span = Some(this.prev_token.span); + // We just ate a comma, so there's no need to use // `TrailingToken::Comma` Ok((field, TrailingToken::None)) @@ -1049,15 +1067,30 @@ impl<'a> Parser<'a> { } if let Some(mut err) = delayed_err { - if let Some(etc_span) = etc_span { - err.multipart_suggestion( - "move the `..` to the end of the field list", - vec![ - (etc_span, String::new()), - (self.token.span, format!("{}.. }}", if ate_comma { "" } else { ", " })), - ], - Applicability::MachineApplicable, - ); + if let Some(first_etc_span) = first_etc_and_maybe_comma_span { + if self.prev_token == token::DotDot { + // We have `.., x, ..`. + err.multipart_suggestion( + "remove the starting `..`", + vec![(first_etc_span, String::new())], + Applicability::MachineApplicable, + ); + } else { + if let Some(last_non_comma_dotdot_span) = last_non_comma_dotdot_span { + // We have `.., x`. + err.multipart_suggestion( + "move the `..` to the end of the field list", + vec![ + (first_etc_span, String::new()), + ( + self.token.span.to(last_non_comma_dotdot_span.shrink_to_hi()), + format!("{} .. }}", if ate_comma { "" } else { "," }), + ), + ], + Applicability::MachineApplicable, + ); + } + } } err.emit(); } diff --git a/tests/ui/parser/issue-112188.fixed b/tests/ui/parser/issue-112188.fixed new file mode 100644 index 0000000000000..5e73d8e38de81 --- /dev/null +++ b/tests/ui/parser/issue-112188.fixed @@ -0,0 +1,14 @@ +// run-rustfix + +#![allow(unused)] + +struct Foo { x: i32 } + +fn main() { + let f = Foo { x: 0 }; + let Foo { .. } = f; + let Foo { .. } = f; //~ ERROR expected `}`, found `,` + let Foo { x, .. } = f; + let Foo { x, .. } = f; //~ ERROR expected `}`, found `,` + let Foo { x, .. } = f; //~ ERROR expected `}`, found `,` +} diff --git a/tests/ui/parser/issue-112188.rs b/tests/ui/parser/issue-112188.rs new file mode 100644 index 0000000000000..27ca192e52263 --- /dev/null +++ b/tests/ui/parser/issue-112188.rs @@ -0,0 +1,14 @@ +// run-rustfix + +#![allow(unused)] + +struct Foo { x: i32 } + +fn main() { + let f = Foo { x: 0 }; + let Foo { .. } = f; + let Foo { .., } = f; //~ ERROR expected `}`, found `,` + let Foo { x, .. } = f; + let Foo { .., x } = f; //~ ERROR expected `}`, found `,` + let Foo { .., x, .. } = f; //~ ERROR expected `}`, found `,` +} diff --git a/tests/ui/parser/issue-112188.stderr b/tests/ui/parser/issue-112188.stderr new file mode 100644 index 0000000000000..6d2d8e6a3b055 --- /dev/null +++ b/tests/ui/parser/issue-112188.stderr @@ -0,0 +1,37 @@ +error: expected `}`, found `,` + --> $DIR/issue-112188.rs:10:17 + | +LL | let Foo { .., } = f; + | --^ + | | | + | | expected `}` + | | help: remove this comma + | `..` must be at the end and cannot have a trailing comma + +error: expected `}`, found `,` + --> $DIR/issue-112188.rs:12:17 + | +LL | let Foo { .., x } = f; + | --^ + | | | + | | expected `}` + | `..` must be at the end and cannot have a trailing comma + | +help: move the `..` to the end of the field list + | +LL - let Foo { .., x } = f; +LL + let Foo { x, .. } = f; + | + +error: expected `}`, found `,` + --> $DIR/issue-112188.rs:13:17 + | +LL | let Foo { .., x, .. } = f; + | --^- + | | | + | | expected `}` + | `..` must be at the end and cannot have a trailing comma + | help: remove the starting `..` + +error: aborting due to 3 previous errors + diff --git a/tests/ui/parser/issue-49257.stderr b/tests/ui/parser/issue-49257.stderr index 846467f7f2268..97e16f88b8d32 100644 --- a/tests/ui/parser/issue-49257.stderr +++ b/tests/ui/parser/issue-49257.stderr @@ -25,7 +25,7 @@ LL | let Point { .., y } = p; help: move the `..` to the end of the field list | LL - let Point { .., y } = p; -LL + let Point { y , .. } = p; +LL + let Point { y, .. } = p; | error: expected `}`, found `,` From 1f5361b40c4ccfa2f806cc9f293d8399a5651b9a Mon Sep 17 00:00:00 2001 From: Victor Gil Date: Sun, 4 Jun 2023 15:49:04 +0200 Subject: [PATCH 3/8] Added custom risc32-imac for esp-espidf target --- compiler/rustc_target/src/spec/mod.rs | 1 + .../src/spec/riscv32imac_esp_espidf.rs | 31 +++++++++++++++++++ src/doc/rustc/src/platform-support.md | 1 + src/doc/rustc/src/platform-support/esp-idf.md | 9 +++--- 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 compiler/rustc_target/src/spec/riscv32imac_esp_espidf.rs diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 05cb7e87a9365..99e4ba24afb1c 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -1284,6 +1284,7 @@ supported_targets! { ("riscv32im-unknown-none-elf", riscv32im_unknown_none_elf), ("riscv32imc-unknown-none-elf", riscv32imc_unknown_none_elf), ("riscv32imc-esp-espidf", riscv32imc_esp_espidf), + ("riscv32imac-esp-espidf", riscv32imac_esp_espidf), ("riscv32imac-unknown-none-elf", riscv32imac_unknown_none_elf), ("riscv32imac-unknown-xous-elf", riscv32imac_unknown_xous_elf), ("riscv32gc-unknown-linux-gnu", riscv32gc_unknown_linux_gnu), diff --git a/compiler/rustc_target/src/spec/riscv32imac_esp_espidf.rs b/compiler/rustc_target/src/spec/riscv32imac_esp_espidf.rs new file mode 100644 index 0000000000000..0795065409ad9 --- /dev/null +++ b/compiler/rustc_target/src/spec/riscv32imac_esp_espidf.rs @@ -0,0 +1,31 @@ +use crate::spec::{cvs, PanicStrategy, RelocModel, Target, TargetOptions}; + +pub fn target() -> Target { + Target { + data_layout: "e-m:e-p:32:32-i64:64-n32-S128".into(), + llvm_target: "riscv32".into(), + pointer_width: 32, + arch: "riscv32".into(), + + options: TargetOptions { + families: cvs!["unix"], + os: "espidf".into(), + env: "newlib".into(), + vendor: "espressif".into(), + linker: Some("riscv32-esp-elf-gcc".into()), + cpu: "generic-rv32".into(), + + // As RiscV32IMAC architecture does natively support atomics, + // automatically enable the support for the Rust STD library. + max_atomic_width: Some(64), + atomic_cas: true, + + features: "+m,+a,+c".into(), + panic_strategy: PanicStrategy::Abort, + relocation_model: RelocModel::Static, + emit_debug_gdb_scripts: false, + eh_frame_header: false, + ..Default::default() + }, + } +} diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index 3b2463aa5b2dd..d0d300010cbdc 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -298,6 +298,7 @@ target | std | host | notes `riscv32im-unknown-none-elf` | * | | Bare RISC-V (RV32IM ISA) [`riscv32imac-unknown-xous-elf`](platform-support/riscv32imac-unknown-xous-elf.md) | ? | | RISC-V Xous (RV32IMAC ISA) [`riscv32imc-esp-espidf`](platform-support/esp-idf.md) | ✓ | | RISC-V ESP-IDF +[`riscv32imac-esp-espidf`](platform-support/esp-idf.md) | ✓ | | RISC-V ESP-IDF `riscv64gc-unknown-freebsd` | | | RISC-V FreeBSD `riscv64gc-unknown-fuchsia` | | | RISC-V Fuchsia `riscv64gc-unknown-linux-musl` | | | RISC-V Linux (kernel 4.20, musl 1.2.0) diff --git a/src/doc/rustc/src/platform-support/esp-idf.md b/src/doc/rustc/src/platform-support/esp-idf.md index 8a4ca347e22f5..4bbe35709b087 100644 --- a/src/doc/rustc/src/platform-support/esp-idf.md +++ b/src/doc/rustc/src/platform-support/esp-idf.md @@ -13,11 +13,12 @@ Targets for the [ESP-IDF](https://github.com/espressif/esp-idf) development fram The target names follow this format: `$ARCH-esp-espidf`, where `$ARCH` specifies the target processor architecture. The following targets are currently defined: -| Target name | Target CPU(s) | -|--------------------------------|-----------------------| -| `riscv32imc-esp-espidf` | [ESP32-C3](https://www.espressif.com/en/products/socs/esp32-c3) | +| Target name | Target CPU(s) | Minimum ESP-IDF version | +|--------------------------------|-----------------------|-------------------------| +| `riscv32imc-esp-espidf` | [ESP32-C3](https://www.espressif.com/en/products/socs/esp32-c3) | `v4.3` | +| `riscv32imac-esp-espidf` | [ESP32-C6](https://www.espressif.com/en/products/socs/esp32-c6) | `v5.1` | -The minimum supported ESP-IDF version is `v4.3`, though it is recommended to use the latest stable release if possible. +It is recommended to use the latest ESP-IDF stable release if possible. ## Building the target From c5145dc87a2b750a12e8f36d84ff6fb4f8edfdf4 Mon Sep 17 00:00:00 2001 From: Simon Farre Date: Thu, 25 May 2023 22:22:00 +0200 Subject: [PATCH 4/8] Fix #111961 r=Mark-Simulacrum Makes the Python pretty printer library source'able from within GDB after spawn. This makes `rust-gdb` not the required approach. It also provides the possibility for GUI:s that wrap GDB, to debug Rust using the pretty printer library; as well as other projects such as for instance Pernosco, which previously was not possible. This won't introduce any new unexpected behaviors for users of `rust-gdb` --- src/etc/gdb_load_rust_pretty_printers.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/etc/gdb_load_rust_pretty_printers.py b/src/etc/gdb_load_rust_pretty_printers.py index 856b5df2de70b..491b6ba9e9e69 100644 --- a/src/etc/gdb_load_rust_pretty_printers.py +++ b/src/etc/gdb_load_rust_pretty_printers.py @@ -1,3 +1,14 @@ +# Add this folder to the python sys path; GDB Python-interpreter will now find modules in this path +import sys +from os import path +self_dir = path.dirname(path.realpath(__file__)) +sys.path.append(self_dir) + import gdb import gdb_lookup -gdb_lookup.register_printers(gdb.current_objfile()) + +# current_objfile can be none; even with `gdb foo-app`; sourcing this file after gdb init now works +try: + gdb_lookup.register_printers(gdb.current_objfile()) +except Exception: + gdb_lookup.register_printers(gdb.selected_inferior().progspace) From 62c8c7cca8f4e8133878c6e42ebb3c4236b3e7ff Mon Sep 17 00:00:00 2001 From: Raoul Strackx Date: Mon, 1 May 2023 13:08:26 +0200 Subject: [PATCH 5/8] Correct LVI print function test --- .../run-make/x86_64-fortanix-unknown-sgx-lvi/print.checks | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/print.checks b/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/print.checks index 8a5493650a720..e02fe09488916 100644 --- a/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/print.checks +++ b/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/print.checks @@ -1,6 +1,6 @@ CHECK: print CHECK: lfence -CHECK: lfence -CHECK: lfence -CHECK: callq 0x{{[[:xdigit:]]*}} <_Unwind_Resume> -CHECK-NEXT: ud2 +CHECK: popq +CHECK-NEXT: popq [[REGISTER:%[a-z]+]] +CHECK-NEXT: lfence +CHECK-NEXT: jmpq *[[REGISTER]] From b35f243c89907ba86b55fac5f86f591f7f342cde Mon Sep 17 00:00:00 2001 From: Raoul Strackx Date: Mon, 5 Jun 2023 15:10:03 +0200 Subject: [PATCH 6/8] Verify that (almost) all `ret` instructions have been replaced --- tests/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh b/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh index 235bb603b842b..04a34724518e8 100644 --- a/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh +++ b/tests/run-make/x86_64-fortanix-unknown-sgx-lvi/script.sh @@ -33,6 +33,15 @@ function check { ${objdump} --disassemble-symbols="${func}" --demangle \ ${enclave} > ${asm} ${filecheck} --input-file ${asm} ${checks} + + if [ "${func_re}" != "rust_plus_one_global_asm" && + "${func_re}" != "cmake_plus_one_c_global_asm" ]; then + # The assembler cannot avoid explicit `ret` instructions. Sequences + # of `shlq $0x0, (%rsp); lfence; retq` are used instead. + # https://www.intel.com/content/www/us/en/developer/articles/technical/ + # software-security-guidance/technical-documentation/load-value-injection.html + ${filecheck} --implicit-check-not ret --input-file ${asm} ${checks} + fi } build From 57cbe250069e7c8eca6bf1e89c6a6b37c71b790d Mon Sep 17 00:00:00 2001 From: Kyle Matsuda Date: Fri, 2 Jun 2023 11:07:28 -0600 Subject: [PATCH 7/8] cleanup some skip_binder -> subst_identity --- compiler/rustc_middle/src/ty/instance.rs | 2 +- compiler/rustc_mir_transform/src/shim.rs | 2 +- src/librustdoc/clean/blanket_impl.rs | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index e641d1ef1be6f..c0d591430f7fc 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -586,7 +586,7 @@ impl<'tcx> Instance<'tcx> { if let Some(substs) = self.substs_for_mir_body() { v.subst(tcx, substs) } else { - v.skip_binder() + v.subst_identity() } } diff --git a/compiler/rustc_mir_transform/src/shim.rs b/compiler/rustc_mir_transform/src/shim.rs index ae726dea94426..5f12f1937c06c 100644 --- a/compiler/rustc_mir_transform/src/shim.rs +++ b/compiler/rustc_mir_transform/src/shim.rs @@ -647,7 +647,7 @@ fn build_call_shim<'tcx>( let mut sig = if let Some(sig_substs) = sig_substs { sig.subst(tcx, &sig_substs) } else { - sig.skip_binder() + sig.subst_identity() }; if let CallKind::Indirect(fnty) = call_kind { diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index 83887cc44b96b..9183fdaa08704 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -104,10 +104,10 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { // the post-inference `trait_ref`, as it's more accurate. trait_: Some(clean_trait_ref_with_bindings( cx, - ty::Binder::dummy(trait_ref.skip_binder()), + ty::Binder::dummy(trait_ref.subst_identity()), ThinVec::new(), )), - for_: clean_middle_ty(ty::Binder::dummy(ty.skip_binder()), cx, None), + for_: clean_middle_ty(ty::Binder::dummy(ty.subst_identity()), cx, None), items: cx .tcx .associated_items(impl_def_id) @@ -116,7 +116,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { .collect::>(), polarity: ty::ImplPolarity::Positive, kind: ImplKind::Blanket(Box::new(clean_middle_ty( - ty::Binder::dummy(trait_ref.skip_binder().self_ty()), + ty::Binder::dummy(trait_ref.subst_identity().self_ty()), cx, None, ))), From 467bc9ffd56dc31d6db79820bd07dbdd5f653783 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 5 Jun 2023 15:28:50 -0700 Subject: [PATCH 8/8] diagnostics: do not suggest type name tweaks on type-inferred closure args Fixes #111932 --- compiler/rustc_hir_typeck/src/check.rs | 14 +++++++++- .../rustc_hir_typeck/src/gather_locals.rs | 12 ++++++++- .../src/traits/error_reporting/suggestions.rs | 4 +-- tests/ui/closures/issue-111932.rs | 9 +++++++ tests/ui/closures/issue-111932.stderr | 26 +++++++++++++++++++ tests/ui/unsized-locals/issue-67981.stderr | 5 +--- 6 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 tests/ui/closures/issue-111932.rs create mode 100644 tests/ui/closures/issue-111932.stderr diff --git a/compiler/rustc_hir_typeck/src/check.rs b/compiler/rustc_hir_typeck/src/check.rs index bfabd44bb5792..69ccbf0b58ff3 100644 --- a/compiler/rustc_hir_typeck/src/check.rs +++ b/compiler/rustc_hir_typeck/src/check.rs @@ -96,7 +96,19 @@ pub(super) fn check_fn<'a, 'tcx>( // for simple cases like `fn foo(x: Trait)`, // where we would error once on the parameter as a whole, and once on the binding `x`. if param.pat.simple_ident().is_none() && !params_can_be_unsized { - fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType(ty_span)); + fcx.require_type_is_sized( + param_ty, + param.pat.span, + // ty_span == binding_span iff this is a closure parameter with no type ascription, + // or if it's an implicit `self` parameter + traits::SizedArgumentType( + if ty_span == Some(param.span) && tcx.is_closure(fn_def_id.into()) { + None + } else { + ty_span + }, + ), + ); } fcx.write_ty(param.hir_id, param_ty); diff --git a/compiler/rustc_hir_typeck/src/gather_locals.rs b/compiler/rustc_hir_typeck/src/gather_locals.rs index 38445f2844052..d9b9b34ba583b 100644 --- a/compiler/rustc_hir_typeck/src/gather_locals.rs +++ b/compiler/rustc_hir_typeck/src/gather_locals.rs @@ -129,7 +129,17 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> { self.fcx.require_type_is_sized( var_ty, p.span, - traits::SizedArgumentType(Some(ty_span)), + // ty_span == ident.span iff this is a closure parameter with no type + // ascription, or if it's an implicit `self` parameter + traits::SizedArgumentType( + if ty_span == ident.span + && self.fcx.tcx.is_closure(self.fcx.body_id.into()) + { + None + } else { + Some(ty_span) + }, + ), ); } } else { diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 42038dbc3d82e..01f2782fd284e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2807,8 +2807,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { err.help("unsized locals are gated as an unstable feature"); } } - ObligationCauseCode::SizedArgumentType(sp) => { - if let Some(span) = sp { + ObligationCauseCode::SizedArgumentType(ty_span) => { + if let Some(span) = ty_span { if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() && let ty::Clause::Trait(trait_pred) = clause && let ty::Dynamic(..) = trait_pred.self_ty().kind() diff --git a/tests/ui/closures/issue-111932.rs b/tests/ui/closures/issue-111932.rs new file mode 100644 index 0000000000000..eb3fe08cbc409 --- /dev/null +++ b/tests/ui/closures/issue-111932.rs @@ -0,0 +1,9 @@ +trait Foo: std::fmt::Debug {} + +fn print_foos(foos: impl Iterator) { + foos.for_each(|foo| { //~ ERROR [E0277] + println!("{:?}", foo); //~ ERROR [E0277] + }); +} + +fn main() {} diff --git a/tests/ui/closures/issue-111932.stderr b/tests/ui/closures/issue-111932.stderr new file mode 100644 index 0000000000000..937bdf3bea255 --- /dev/null +++ b/tests/ui/closures/issue-111932.stderr @@ -0,0 +1,26 @@ +error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time + --> $DIR/issue-111932.rs:4:20 + | +LL | foos.for_each(|foo| { + | ^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `(dyn Foo + 'static)` + = note: all function arguments must have a statically known size + = help: unsized fn params are gated as an unstable feature + +error[E0277]: the size for values of type `dyn Foo` cannot be known at compilation time + --> $DIR/issue-111932.rs:5:26 + | +LL | println!("{:?}", foo); + | ---- ^^^ doesn't have a size known at compile-time + | | + | required by a bound introduced by this call + | + = help: the trait `Sized` is not implemented for `dyn Foo` +note: required by a bound in `core::fmt::rt::Argument::<'a>::new_debug` + --> $SRC_DIR/core/src/fmt/rt.rs:LL:COL + = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/unsized-locals/issue-67981.stderr b/tests/ui/unsized-locals/issue-67981.stderr index a4b179ae2fd12..13fdc037ac569 100644 --- a/tests/ui/unsized-locals/issue-67981.stderr +++ b/tests/ui/unsized-locals/issue-67981.stderr @@ -5,10 +5,7 @@ LL | let f: fn([u8]) = |_| {}; | ^ doesn't have a size known at compile-time | = help: the trait `Sized` is not implemented for `[u8]` -help: function arguments must have a statically known size, borrowed types always have a known size - | -LL | let f: fn([u8]) = |&_| {}; - | + + = note: all function arguments must have a statically known size error: aborting due to previous error