From b08bc7836be6a53e733190f3174128fe8d005737 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 16 Feb 2021 09:59:38 +0100 Subject: [PATCH 01/12] fix MIR fn-ptr pretty-printing --- compiler/rustc_middle/src/ty/print/pretty.rs | 20 ++++++++++++-------- compiler/rustc_mir/src/interpret/operand.rs | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index a8d9995bd0b2f..f934e31a534d2 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1030,15 +1030,19 @@ pub trait PrettyPrinter<'tcx>: )?; } (Scalar::Ptr(ptr), ty::FnPtr(_)) => { - // FIXME: this can ICE when the ptr is dangling or points to a non-function. - // We should probably have a helper method to share code with the "Byte strings" + // FIXME: We should probably have a helper method to share code with the "Byte strings" // printing above (which also has to handle pointers to all sorts of things). - let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn(); - self = self.typed_value( - |this| this.print_value_path(instance.def_id(), instance.substs), - |this| this.print_type(ty), - " as ", - )?; + match self.tcx().get_global_alloc(ptr.alloc_id) { + Some(GlobalAlloc::Function(instance)) => { + self = self.typed_value( + |this| this.print_value_path(instance.def_id(), instance.substs), + |this| this.print_type(ty), + " as ", + )?; + } + Some(_) => p!(""), + None => p!(""), + } } // For function type zsts just printing the path is enough (Scalar::Int(int), ty::FnDef(d, s)) if int == ScalarInt::ZST => { diff --git a/compiler/rustc_mir/src/interpret/operand.rs b/compiler/rustc_mir/src/interpret/operand.rs index 88236458a213a..4b131ecb863ad 100644 --- a/compiler/rustc_mir/src/interpret/operand.rs +++ b/compiler/rustc_mir/src/interpret/operand.rs @@ -106,7 +106,7 @@ impl std::fmt::Display for ImmTy<'tcx, Tag> { } ScalarMaybeUninit::Uninit => cx.typed_value( |mut this| { - this.write_str("{uninit ")?; + this.write_str("uninit ")?; Ok(this) }, |this| this.print_type(ty), From 026be9dc26c4cd867e2b406dee8c787afb224cfe Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 18 Feb 2021 10:11:57 +0800 Subject: [PATCH 02/12] Keep consistency in example for Stdin StdinLock Stdin uses handle whereas StdinLock uses stdin_lock, changed it to handle. --- library/std/src/io/stdio.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 1160011f35287..80933f6de3d72 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -251,8 +251,8 @@ pub struct Stdin { /// let mut buffer = String::new(); /// let stdin = io::stdin(); // We get `Stdin` here. /// { -/// let mut stdin_lock = stdin.lock(); // We get `StdinLock` here. -/// stdin_lock.read_to_string(&mut buffer)?; +/// let mut handle = stdin.lock(); // We get `StdinLock` here. +/// handle.read_to_string(&mut buffer)?; /// } // `StdinLock` is dropped here. /// Ok(()) /// } From 5fd1ebe50f5bea799efd4ab70a816a731df34319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 11 Feb 2021 12:53:42 +0300 Subject: [PATCH 03/12] Fix panic in 'remove semicolon' when types are not local It's not possible to check if removing a semicolon fixes the type error when checking match arms and one or both of the last arm's and the current arm's return types are imported "opaque" types. In these cases we don't generate a "consider removing semicolon" suggestions. Fixes #81839 --- compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs index 9e6c11d9dddbb..67068322733ed 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs @@ -1078,8 +1078,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { "both opaque, likely future {:?} {:?} {:?} {:?}", last_def_id, last_bounds, exp_def_id, exp_bounds ); - let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_def_id.expect_local()); - let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_def_id.expect_local()); + + let (last_local_id, exp_local_id) = + match (last_def_id.as_local(), exp_def_id.as_local()) { + (Some(last_hir_id), Some(exp_hir_id)) => (last_hir_id, exp_hir_id), + (_, _) => return None, + }; + + let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_local_id); + let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_local_id); + match ( &self.tcx.hir().expect_item(last_hir_id).kind, &self.tcx.hir().expect_item(exp_hir_id).kind, From ad47fb1ca9d1eb66752611ae9f4d2c183daa9145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 11 Feb 2021 13:37:00 +0300 Subject: [PATCH 04/12] Check opaque type def ids before bailing out --- compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs index 67068322733ed..37e9e6b4d3d0d 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs @@ -1073,6 +1073,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let last_expr_ty = self.node_ty(last_expr.hir_id); let needs_box = match (last_expr_ty.kind(), expected_ty.kind()) { + (ty::Opaque(last_def_id, _), ty::Opaque(exp_def_id, _)) + if last_def_id == exp_def_id => + { + StatementAsExpression::CorrectType + } (ty::Opaque(last_def_id, last_bounds), ty::Opaque(exp_def_id, exp_bounds)) => { debug!( "both opaque, likely future {:?} {:?} {:?} {:?}", From 15fdccc6ae636b0f5663e2e75e0a115d60b8633c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 11 Feb 2021 20:17:57 +0300 Subject: [PATCH 05/12] Update 'match-prev-arm-needing-semi' --- .../ui/suggestions/match-prev-arm-needing-semi.stderr | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr b/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr index e9803a78f94b3..fae0c498b440a 100644 --- a/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr +++ b/src/test/ui/suggestions/match-prev-arm-needing-semi.stderr @@ -24,13 +24,10 @@ help: consider `await`ing on the `Future` | LL | false => async_dummy().await, | ^^^^^^ -help: consider removing this semicolon and boxing the expressions - | -LL | Box::new(async_dummy()) -LL | -LL | } -LL | false => Box::new(async_dummy()), +help: consider removing this semicolon | +LL | async_dummy() + | -- error[E0308]: `match` arms have incompatible types --> $DIR/match-prev-arm-needing-semi.rs:39:18 From 9ef67e09a4c2fdfd528ac174e3aec2fe0e97f347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 18 Feb 2021 16:47:01 +0300 Subject: [PATCH 06/12] Add regression test --- .../ui/suggestions/auxiliary/issue-81839.rs | 9 +++++++ src/test/ui/suggestions/issue-81839.rs | 17 ++++++++++++ src/test/ui/suggestions/issue-81839.stderr | 27 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/test/ui/suggestions/auxiliary/issue-81839.rs create mode 100644 src/test/ui/suggestions/issue-81839.rs create mode 100644 src/test/ui/suggestions/issue-81839.stderr diff --git a/src/test/ui/suggestions/auxiliary/issue-81839.rs b/src/test/ui/suggestions/auxiliary/issue-81839.rs new file mode 100644 index 0000000000000..5683c45adf26d --- /dev/null +++ b/src/test/ui/suggestions/auxiliary/issue-81839.rs @@ -0,0 +1,9 @@ +// edition:2018 + +pub struct Test {} + +impl Test { + pub async fn answer_str(&self, _s: &str) -> Test { + Test {} + } +} diff --git a/src/test/ui/suggestions/issue-81839.rs b/src/test/ui/suggestions/issue-81839.rs new file mode 100644 index 0000000000000..0b9b7aefe735d --- /dev/null +++ b/src/test/ui/suggestions/issue-81839.rs @@ -0,0 +1,17 @@ +// aux-build:issue-81839.rs +// edition:2018 + +extern crate issue_81839; + +async fn test(ans: &str, num: i32, cx: &issue_81839::Test) -> u32 { + match num { + 1 => { + cx.answer_str("hi"); + } + _ => cx.answer_str("hi"), //~ `match` arms have incompatible types + } + + 1 +} + +fn main() {} diff --git a/src/test/ui/suggestions/issue-81839.stderr b/src/test/ui/suggestions/issue-81839.stderr new file mode 100644 index 0000000000000..1a289d39e4467 --- /dev/null +++ b/src/test/ui/suggestions/issue-81839.stderr @@ -0,0 +1,27 @@ +error[E0308]: `match` arms have incompatible types + --> $DIR/issue-81839.rs:11:14 + | +LL | / match num { +LL | | 1 => { +LL | | cx.answer_str("hi"); + | | -------------------- + | | | | + | | | help: consider removing this semicolon + | | this is found to be of type `()` +LL | | } +LL | | _ => cx.answer_str("hi"), + | | ^^^^^^^^^^^^^^^^^^^ expected `()`, found opaque type +LL | | } + | |_____- `match` arms have incompatible types + | + ::: $DIR/auxiliary/issue-81839.rs:6:49 + | +LL | pub async fn answer_str(&self, _s: &str) -> Test { + | ---- the `Output` of this `async fn`'s found opaque type + | + = note: expected type `()` + found opaque type `impl Future` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From 9b9c5eaa5c17cd3d4e0e941b8215c4a7513dbc5e Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Thu, 18 Feb 2021 16:10:28 +0100 Subject: [PATCH 07/12] rustc: Show `@path` usage in stable The feature was stabilized in #66172, but the usage string was not updated to be shown. Signed-off-by: Miguel Ojeda --- compiler/rustc_driver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 15b984acac590..c6635b6db274b 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -821,7 +821,7 @@ fn usage(verbose: bool, include_unstable_options: bool, nightly_build: bool) { } else { "\n --help -v Print the full set of options rustc accepts" }; - let at_path = if verbose && nightly_build { + let at_path = if verbose { " @path Read newline separated options from `path`\n" } else { "" From 1839748772a535cd694851092aa4ccab9374492c Mon Sep 17 00:00:00 2001 From: Alexander Ronald Altman Date: Fri, 1 Jan 2021 11:40:00 -0800 Subject: [PATCH 08/12] `impl PartialEq for char`; symmetry for #78636 Also fixes the "since" version of the original. --- library/proc_macro/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index a89e7b53e43c4..5f1f7d8cac418 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -842,13 +842,20 @@ impl fmt::Debug for Punct { } } -#[stable(feature = "proc_macro_punct_eq", since = "1.49.0")] +#[stable(feature = "proc_macro_punct_eq", since = "1.50.0")] impl PartialEq for Punct { fn eq(&self, rhs: &char) -> bool { self.as_char() == *rhs } } +#[stable(feature = "proc_macro_punct_eq_flipped", since = "1.52.0")] +impl PartialEq for char { + fn eq(&self, rhs: &Punct) -> bool { + *self == rhs.as_char() + } +} + /// An identifier (`ident`). #[derive(Clone)] #[stable(feature = "proc_macro_lib2", since = "1.29.0")] From bf8563dc9b15eb8bf61b0dd4c77001a33597e60b Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 19 Feb 2021 19:34:46 -0800 Subject: [PATCH 09/12] Fix minor mistake in LTO docs. `-C lto=true` isn't a valid option. --- src/doc/rustc/src/codegen-options/index.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/rustc/src/codegen-options/index.md b/src/doc/rustc/src/codegen-options/index.md index 51e7d987d9d82..1883346430be0 100644 --- a/src/doc/rustc/src/codegen-options/index.md +++ b/src/doc/rustc/src/codegen-options/index.md @@ -299,9 +299,9 @@ opt-level=0`](#opt-level)). That is: * When `-C lto` is not specified: * `codegen-units=1`: disable LTO. * `opt-level=0`: disable LTO. -* When `-C lto=true`: - * `lto=true`: 16 codegen units, perform fat LTO across crates. - * `codegen-units=1` + `lto=true`: 1 codegen unit, fat LTO across crates. +* When `-C lto` is specified: + * `lto`: 16 codegen units, perform fat LTO across crates. + * `codegen-units=1` + `lto`: 1 codegen unit, fat LTO across crates. See also [linker-plugin-lto](#linker-plugin-lto) for cross-language LTO. From e90674574d21f8245716c0644b0c8aa2e1702b54 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 20 Feb 2021 11:34:35 +0100 Subject: [PATCH 10/12] fn ptr pretty printing: fall back to raw ptr printing --- compiler/rustc_middle/src/ty/print/pretty.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index f934e31a534d2..cc41fd855e83b 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -1018,7 +1018,7 @@ pub trait PrettyPrinter<'tcx>: p!(write("{:?}", char::try_from(int).unwrap())) } // Raw pointers - (Scalar::Int(int), ty::RawPtr(_)) => { + (Scalar::Int(int), ty::RawPtr(_) | ty::FnPtr(_)) => { let data = int.assert_bits(self.tcx().data_layout.pointer_size); self = self.typed_value( |mut this| { @@ -1040,8 +1040,7 @@ pub trait PrettyPrinter<'tcx>: " as ", )?; } - Some(_) => p!(""), - None => p!(""), + _ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?, } } // For function type zsts just printing the path is enough From 30716855056045650e88a98e9d6240ef33c33758 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 20 Feb 2021 15:05:50 +0100 Subject: [PATCH 11/12] Don't render [src] link on dummy spans --- src/librustdoc/clean/types.rs | 4 ++++ src/librustdoc/html/render/mod.rs | 3 +++ 2 files changed, 7 insertions(+) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 3e7196fa7fa03..9a2319f6e379d 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1852,6 +1852,10 @@ impl Span { self.0 } + crate fn is_dummy(&self) -> bool { + self.0.is_dummy() + } + crate fn filename(&self, sess: &Session) -> FileName { sess.source_map().span_to_filename(self.0) } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index f5eb92c1bb5aa..7ca355ed11cc7 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1638,6 +1638,9 @@ impl Context<'_> { /// may happen, for example, with externally inlined items where the source /// of their crate documentation isn't known. fn src_href(&self, item: &clean::Item) -> Option { + if item.source.is_dummy() { + return None; + } let mut root = self.root_path(); let mut path = String::new(); let cnum = item.source.cnum(self.sess()); From 0c511c9115cc4f8eff7ad50878af032427076c4c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 20 Feb 2021 15:06:20 +0100 Subject: [PATCH 12/12] Add test for no src links on dummy spans --- src/test/rustdoc/src-links-auto-impls.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/test/rustdoc/src-links-auto-impls.rs diff --git a/src/test/rustdoc/src-links-auto-impls.rs b/src/test/rustdoc/src-links-auto-impls.rs new file mode 100644 index 0000000000000..a1d183df0f1f2 --- /dev/null +++ b/src/test/rustdoc/src-links-auto-impls.rs @@ -0,0 +1,12 @@ +#![crate_name = "foo"] + +// @has foo/struct.Unsized.html +// @has - '//h3[@id="impl-Sized"]/code' 'impl !Sized for Unsized' +// @!has - '//h3[@id="impl-Sized"]/a[@class="srclink"]' '[src]' +// @has - '//h3[@id="impl-Sync"]/code' 'impl Sync for Unsized' +// @!has - '//h3[@id="impl-Sync"]/a[@class="srclink"]' '[src]' +// @has - '//h3[@id="impl-Any"]/code' 'impl Any for T' +// @has - '//h3[@id="impl-Any"]/a[@class="srclink"]' '[src]' +pub struct Unsized { + data: [u8], +}