From 4ced4f339599d1f2f137cb23b9b1c4aeb081e200 Mon Sep 17 00:00:00 2001 From: Son Date: Tue, 21 Aug 2018 10:51:12 +1000 Subject: [PATCH 01/16] Add doc for impl From for Addr --- src/libstd/net/addr.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libstd/net/addr.rs b/src/libstd/net/addr.rs index e80c3eeb876ce..ff35325ab4fda 100644 --- a/src/libstd/net/addr.rs +++ b/src/libstd/net/addr.rs @@ -554,6 +554,7 @@ impl FromInner for SocketAddrV6 { #[stable(feature = "ip_from_ip", since = "1.16.0")] impl From for SocketAddr { + /// Converts a [`SocketAddrV4`] into a [`SocketAddr::V4`]. fn from(sock4: SocketAddrV4) -> SocketAddr { SocketAddr::V4(sock4) } @@ -561,6 +562,7 @@ impl From for SocketAddr { #[stable(feature = "ip_from_ip", since = "1.16.0")] impl From for SocketAddr { + /// Converts a [`SocketAddrV6`] into a [`SocketAddr::V6`]. fn from(sock6: SocketAddrV6) -> SocketAddr { SocketAddr::V6(sock6) } @@ -568,6 +570,12 @@ impl From for SocketAddr { #[stable(feature = "addr_from_into_ip", since = "1.17.0")] impl> From<(I, u16)> for SocketAddr { + /// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`]. + /// + /// This conversion creates a [`SocketAddr::V4`] for a [`IpAddr::V4`] + /// and creates a [`SocketAddr::V6`] for a [`IpAddr::V6`]. + /// + /// `u16` is treated as port of the newly created [`SocketAddr`]. fn from(pieces: (I, u16)) -> SocketAddr { SocketAddr::new(pieces.0.into(), pieces.1) } From 1a0e8f95f54195831fe681dc4e4f4fa97d96e20e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 10 Sep 2018 11:27:21 +0200 Subject: [PATCH 02/16] Remove namespace for keywords --- src/librustdoc/html/render.rs | 4 ++-- src/test/rustdoc/keyword.rs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 972c2f0e15c5e..3dc55c5556b7f 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1827,8 +1827,8 @@ impl Context { *slot.borrow_mut() = self.current.clone(); }); - let mut title = if it.is_primitive() { - // No need to include the namespace for primitive types + let mut title = if it.is_primitive() || it.is_keyword() { + // No need to include the namespace for primitive types and keywords String::new() } else { self.current.join("::") diff --git a/src/test/rustdoc/keyword.rs b/src/test/rustdoc/keyword.rs index b255ffddafac8..73a026c581be5 100644 --- a/src/test/rustdoc/keyword.rs +++ b/src/test/rustdoc/keyword.rs @@ -15,6 +15,7 @@ // @has foo/index.html '//h2[@id="keywords"]' 'Keywords' // @has foo/index.html '//a[@href="keyword.match.html"]' 'match' // @has foo/keyword.match.html '//a[@class="keyword"]' 'match' +// @has foo/keyword.match.html '//span[@class="in-band"]' 'Keyword match' // @has foo/keyword.match.html '//section[@id="main"]//div[@class="docblock"]//p' 'this is a test!' // @!has foo/index.html '//a/@href' 'foo/index.html' // @!has foo/foo/index.html From fab6b06b899a2844df4ed5ce65a7fa7947cf18cb Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 Sep 2018 23:00:10 +0200 Subject: [PATCH 03/16] Add treat-err-as-bug flag in rustdoc --- src/librustdoc/core.rs | 19 +++++++++++-------- src/librustdoc/lib.rs | 13 ++++++++++--- src/test/rustdoc-ui/treat-err-as-bug.rs | 13 +++++++++++++ src/test/rustdoc-ui/treat-err-as-bug.stderr | 21 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 src/test/rustdoc-ui/treat-err-as-bug.rs create mode 100644 src/test/rustdoc-ui/treat-err-as-bug.stderr diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index a312913a69c17..2feeecb388f38 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -260,9 +260,10 @@ impl DocAccessLevels for AccessLevels { /// /// If the given `error_format` is `ErrorOutputType::Json` and no `SourceMap` is given, a new one /// will be created for the handler. -pub fn new_handler(error_format: ErrorOutputType, source_map: Option>) - -> errors::Handler -{ +pub fn new_handler(error_format: ErrorOutputType, + source_map: Option>, + treat_err_as_bug: bool, +) -> errors::Handler { // rustdoc doesn't override (or allow to override) anything from this that is relevant here, so // stick to the defaults let sessopts = Options::default(); @@ -299,7 +300,7 @@ pub fn new_handler(error_format: ErrorOutputType, source_map: Option, describe_lints: bool, mut manual_passes: Vec, - mut default_passes: passes::DefaultPassOption) - -> (clean::Crate, RenderInfo, Vec) -{ + mut default_passes: passes::DefaultPassOption, + treat_err_as_bug: bool, +) -> (clean::Crate, RenderInfo, Vec) { // Parse, resolve, and typecheck the given crate. let cpath = match input { @@ -388,7 +389,9 @@ pub fn run_core(search_paths: SearchPaths, }; driver::spawn_thread_pool(sessopts, move |sessopts| { let source_map = Lrc::new(source_map::SourceMap::new(sessopts.file_path_mapping())); - let diagnostic_handler = new_handler(error_format, Some(source_map.clone())); + let diagnostic_handler = new_handler(error_format, + Some(source_map.clone()), + treat_err_as_bug); let mut sess = session::build_session_( sessopts, cpath, diagnostic_handler, source_map, diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index bc471d427048b..5f03ee5f68a0c 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -404,8 +404,11 @@ fn main_args(args: &[String]) -> isize { `short` (instead was `{}`)", arg)); } }; + let treat_err_as_bug = matches.opt_strs("Z").iter().any(|x| { + *x == "treat-err-as-bug" + }); - let diag = core::new_handler(error_format, None); + let diag = core::new_handler(error_format, None, treat_err_as_bug); // check for deprecated options check_deprecated_options(&matches, &diag); @@ -560,7 +563,7 @@ fn main_args(args: &[String]) -> isize { let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, error_format, move |out| { let Output { krate, passes, renderinfo } = out; - let diag = core::new_handler(error_format, None); + let diag = core::new_handler(error_format, None, treat_err_as_bug); info!("going to format"); match output_format.as_ref().map(|s| &**s) { Some("html") | None => { @@ -691,6 +694,9 @@ where R: 'static + Send, let force_unstable_if_unmarked = matches.opt_strs("Z").iter().any(|x| { *x == "force-unstable-if-unmarked" }); + let treat_err_as_bug = matches.opt_strs("Z").iter().any(|x| { + *x == "treat-err-as-bug" + }); let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format); @@ -703,7 +709,8 @@ where R: 'static + Send, core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot, display_warnings, crate_name.clone(), force_unstable_if_unmarked, edition, cg, error_format, - lint_opts, lint_cap, describe_lints, manual_passes, default_passes); + lint_opts, lint_cap, describe_lints, manual_passes, default_passes, + treat_err_as_bug); info!("finished with rustc"); diff --git a/src/test/rustdoc-ui/treat-err-as-bug.rs b/src/test/rustdoc-ui/treat-err-as-bug.rs new file mode 100644 index 0000000000000..0c4c5dc316e19 --- /dev/null +++ b/src/test/rustdoc-ui/treat-err-as-bug.rs @@ -0,0 +1,13 @@ +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -Ztreat-err-as-bug --error-format=human + +pub fn foo() { diff --git a/src/test/rustdoc-ui/treat-err-as-bug.stderr b/src/test/rustdoc-ui/treat-err-as-bug.stderr new file mode 100644 index 0000000000000..d96415ed050ba --- /dev/null +++ b/src/test/rustdoc-ui/treat-err-as-bug.stderr @@ -0,0 +1,21 @@ +error: this file contains an un-closed delimiter + --> $DIR/treat-err-as-bug.rs:13:16 + | +13 | pub fn foo() { + | - ^ + | | + | un-closed delimiter + +thread '' panicked at 'encountered error with `-Z treat_err_as_bug', librustc_errors/lib.rs:486:13 +note: Run with `RUST_BACKTRACE=1` for a backtrace. + +error: internal compiler error: unexpected panic + +note: the compiler unexpectedly panicked. this is a bug. + +note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports + +note: rustc 1.30.0-dev running on x86_64-apple-darwin + +note: compiler flags: -Z ui-testing -Z unstable-options -Z treat-err-as-bug + From 818938f3442391888242e630a367d6394b59e096 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 14 Sep 2018 20:58:58 +0200 Subject: [PATCH 04/16] Remove treat-err-as-bug test --- src/test/rustdoc-ui/treat-err-as-bug.rs | 13 ------------- src/test/rustdoc-ui/treat-err-as-bug.stderr | 21 --------------------- 2 files changed, 34 deletions(-) delete mode 100644 src/test/rustdoc-ui/treat-err-as-bug.rs delete mode 100644 src/test/rustdoc-ui/treat-err-as-bug.stderr diff --git a/src/test/rustdoc-ui/treat-err-as-bug.rs b/src/test/rustdoc-ui/treat-err-as-bug.rs deleted file mode 100644 index 0c4c5dc316e19..0000000000000 --- a/src/test/rustdoc-ui/treat-err-as-bug.rs +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// compile-flags: -Ztreat-err-as-bug --error-format=human - -pub fn foo() { diff --git a/src/test/rustdoc-ui/treat-err-as-bug.stderr b/src/test/rustdoc-ui/treat-err-as-bug.stderr deleted file mode 100644 index d96415ed050ba..0000000000000 --- a/src/test/rustdoc-ui/treat-err-as-bug.stderr +++ /dev/null @@ -1,21 +0,0 @@ -error: this file contains an un-closed delimiter - --> $DIR/treat-err-as-bug.rs:13:16 - | -13 | pub fn foo() { - | - ^ - | | - | un-closed delimiter - -thread '' panicked at 'encountered error with `-Z treat_err_as_bug', librustc_errors/lib.rs:486:13 -note: Run with `RUST_BACKTRACE=1` for a backtrace. - -error: internal compiler error: unexpected panic - -note: the compiler unexpectedly panicked. this is a bug. - -note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports - -note: rustc 1.30.0-dev running on x86_64-apple-darwin - -note: compiler flags: -Z ui-testing -Z unstable-options -Z treat-err-as-bug - From fc6e1ed1e1cce232a654f7c585aec09e143cb46e Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Fri, 14 Sep 2018 14:06:05 +0200 Subject: [PATCH 05/16] Regression test for rust-lang/rust#53675. (Includes a couple variations on the theme. I confirmed that the ones in `in_expression_position` and `what_if_we_use_panic_directly_in_expr` both failed back on "rustc 1.30.0-nightly (0f063aef6 2018-09-03)".) --- .../issues/issue-53675-a-test-called-panic.rs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/ui/issues/issue-53675-a-test-called-panic.rs diff --git a/src/test/ui/issues/issue-53675-a-test-called-panic.rs b/src/test/ui/issues/issue-53675-a-test-called-panic.rs new file mode 100644 index 0000000000000..8a35b36d46d64 --- /dev/null +++ b/src/test/ui/issues/issue-53675-a-test-called-panic.rs @@ -0,0 +1,36 @@ +// rust-lang/rust#53675: At one point the compiler errored when a test +// named `panic` used the `assert!` macro in expression position. + +// compile-pass +// compile-flags: --test + +mod in_expression_position { + #[test] + fn panic() { + assert!(true) + } +} + +mod in_statement_position { + #[test] + fn panic() { + assert!(true); + } +} + +mod what_if_we_use_panic_directly_in_expr { + #[test] + #[should_panic] + fn panic() { + panic!("in expr") + } +} + + +mod what_if_we_use_panic_directly_in_stmt { + #[test] + #[should_panic] + fn panic() { + panic!("in stmt"); + } +} From 19840793e57e4903e00edac963f30c9de34b1ea3 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 16 Sep 2018 20:03:44 +0800 Subject: [PATCH 06/16] lint to change numeric literal instead of into --- src/librustc_typeck/check/demand.rs | 90 +++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 18 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 4e22ead8db987..2f80edd5433dc 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -419,6 +419,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { if needs_paren { "(" } else { "" }, src, if needs_paren { ")" } else { "" }); + let suffix_suggestion = format!( + "{}{}{}{}", + if needs_paren { "(" } else { "" }, + src, + expected_ty, + if needs_paren { ")" } else { "" }, + ); + + let is_suffixed = |expr: &hir::Expr| { + if let hir::ExprKind::Lit(lit) = &expr.node { + lit.node.is_suffixed() + } else { + false + } + }; match (&expected_ty.sty, &checked_ty.sty) { (&ty::Int(ref exp), &ty::Int(ref found)) => { @@ -444,12 +459,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } _ => { - err.span_suggestion_with_applicability( - expr.span, - &format!("{}, which {}", msg, will_sign_extend), - into_suggestion, - Applicability::MachineApplicable - ); + if is_suffixed(expr) { + err.span_suggestion_with_applicability( + expr.span, + &format!( + "change the type of the numeric literal from `{}` to `{}`", + checked_ty, + expected_ty, + ), + suffix_suggestion, + Applicability::MaybeIncorrect, + ); + } else { + err.span_suggestion_with_applicability( + expr.span, + &format!("{}, which {}", msg, will_sign_extend), + into_suggestion, + Applicability::MachineApplicable + ); + } } } true @@ -477,12 +505,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } _ => { - err.span_suggestion_with_applicability( - expr.span, - &format!("{}, which {}", msg, will_zero_extend), - into_suggestion, - Applicability::MachineApplicable - ); + if is_suffixed(expr) { + err.span_suggestion_with_applicability( + expr.span, + &format!( + "change the type of the numeric literal from `{}` to `{}`", + checked_ty, + expected_ty, + ), + suffix_suggestion, + Applicability::MaybeIncorrect, + ); + } else { + err.span_suggestion_with_applicability( + expr.span, + &format!("{}, which {}", msg, will_zero_extend), + into_suggestion, + Applicability::MachineApplicable + ); + } } } true @@ -583,12 +624,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } (&ty::Float(ref exp), &ty::Float(ref found)) => { if found.bit_width() < exp.bit_width() { - err.span_suggestion_with_applicability( - expr.span, - &format!("{} in a lossless way", msg), - into_suggestion, - Applicability::MachineApplicable - ); + if is_suffixed(expr) { + err.span_suggestion_with_applicability( + expr.span, + &format!( + "change the type of the numeric literal from `{}` to `{}`", + checked_ty, + expected_ty, + ), + suffix_suggestion, + Applicability::MaybeIncorrect, + ); + } else { + err.span_suggestion_with_applicability( + expr.span, + &format!("{} in a lossless way", msg), + into_suggestion, + Applicability::MachineApplicable + ); + } } else if can_cast { err.span_suggestion_with_applicability( expr.span, From 0ff0669b79d768dd32ceefeb76cad7332221492b Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 16 Sep 2018 21:12:23 +0800 Subject: [PATCH 07/16] replace old literal in expr --- src/librustc_typeck/check/demand.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 2f80edd5433dc..25559bdec76f5 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -422,7 +422,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let suffix_suggestion = format!( "{}{}{}{}", if needs_paren { "(" } else { "" }, - src, + { + // 42u8 + // ^^ + let lit_offset = src.len() - checked_ty.to_string().len(); + &src[..lit_offset] + }, expected_ty, if needs_paren { ")" } else { "" }, ); From 17a28f706395bab81047c5e4fd3d903304cc5c72 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 16 Sep 2018 21:13:11 +0800 Subject: [PATCH 08/16] add test for numeric literal cast --- .../ui/mismatched_types/numeric-literal-cast.rs | 15 +++++++++++++++ .../mismatched_types/numeric-literal-cast.stderr | 13 +++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/test/ui/mismatched_types/numeric-literal-cast.rs create mode 100644 src/test/ui/mismatched_types/numeric-literal-cast.stderr diff --git a/src/test/ui/mismatched_types/numeric-literal-cast.rs b/src/test/ui/mismatched_types/numeric-literal-cast.rs new file mode 100644 index 0000000000000..8d8c9d75973b6 --- /dev/null +++ b/src/test/ui/mismatched_types/numeric-literal-cast.rs @@ -0,0 +1,15 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn foo(_: u16) {} +fn main() { + foo(8u8); +} + diff --git a/src/test/ui/mismatched_types/numeric-literal-cast.stderr b/src/test/ui/mismatched_types/numeric-literal-cast.stderr new file mode 100644 index 0000000000000..8fda824311500 --- /dev/null +++ b/src/test/ui/mismatched_types/numeric-literal-cast.stderr @@ -0,0 +1,13 @@ +error[E0308]: mismatched types + --> $DIR/numeric-literal-cast.rs:13:9 + | +LL | foo(8u8); + | ^^^ expected u16, found u8 +help: change the type of the numeric literal from `u8` to `u16` + | +LL | foo(8u16); + | ^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`. From d31a2a0c603c6a7ec2a69e2b5658bcca016dbc29 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sun, 16 Sep 2018 23:13:17 +0800 Subject: [PATCH 09/16] trim type numeric literal suffix --- src/librustc_typeck/check/demand.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 25559bdec76f5..ebed49249499c 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -422,12 +422,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let suffix_suggestion = format!( "{}{}{}{}", if needs_paren { "(" } else { "" }, - { - // 42u8 - // ^^ - let lit_offset = src.len() - checked_ty.to_string().len(); - &src[..lit_offset] - }, + src.trim_right_matches(&checked_ty.to_string()), expected_ty, if needs_paren { ")" } else { "" }, ); From 9d6c4f09c4b309fd45379996975f4075ba37258b Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Mon, 17 Sep 2018 09:24:33 +0800 Subject: [PATCH 10/16] merge into/literal suggestion for DRY --- src/librustc_typeck/check/demand.rs | 119 ++++++++++++---------------- 1 file changed, 52 insertions(+), 67 deletions(-) diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index ebed49249499c..e78cd4891a5ea 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -415,19 +415,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { src, if needs_paren { ")" } else { "" }, expected_ty); - let into_suggestion = format!("{}{}{}.into()", - if needs_paren { "(" } else { "" }, - src, - if needs_paren { ")" } else { "" }); - let suffix_suggestion = format!( - "{}{}{}{}", + let into_suggestion = format!( + "{}{}{}.into()", if needs_paren { "(" } else { "" }, - src.trim_right_matches(&checked_ty.to_string()), - expected_ty, + src, if needs_paren { ")" } else { "" }, ); - - let is_suffixed = |expr: &hir::Expr| { + let literal_is_ty_suffixed = |expr: &hir::Expr| { if let hir::ExprKind::Lit(lit) = &expr.node { lit.node.is_suffixed() } else { @@ -435,6 +429,42 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } }; + let into_sugg = into_suggestion.clone(); + let suggest_to_change_suffix_or_into = |err: &mut DiagnosticBuilder, + note: Option<&str>| { + let suggest_msg = if literal_is_ty_suffixed(expr) { + format!( + "change the type of the numeric literal from `{}` to `{}`", + checked_ty, + expected_ty, + ) + } else { + match note { + Some(note) => format!("{}, which {}", msg, note), + _ => format!("{} in a lossless way", msg), + } + }; + + let suffix_suggestion = format!( + "{}{}{}{}", + if needs_paren { "(" } else { "" }, + src.trim_right_matches(&checked_ty.to_string()), + expected_ty, + if needs_paren { ")" } else { "" }, + ); + + err.span_suggestion_with_applicability( + expr.span, + &suggest_msg, + if literal_is_ty_suffixed(expr) { + suffix_suggestion + } else { + into_sugg + }, + Applicability::MachineApplicable, + ); + }; + match (&expected_ty.sty, &checked_ty.sty) { (&ty::Int(ref exp), &ty::Int(ref found)) => { match (found.bit_width(), exp.bit_width()) { @@ -459,25 +489,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } _ => { - if is_suffixed(expr) { - err.span_suggestion_with_applicability( - expr.span, - &format!( - "change the type of the numeric literal from `{}` to `{}`", - checked_ty, - expected_ty, - ), - suffix_suggestion, - Applicability::MaybeIncorrect, - ); - } else { - err.span_suggestion_with_applicability( - expr.span, - &format!("{}, which {}", msg, will_sign_extend), - into_suggestion, - Applicability::MachineApplicable - ); - } + suggest_to_change_suffix_or_into( + err, + Some(will_sign_extend), + ); } } true @@ -505,25 +520,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } _ => { - if is_suffixed(expr) { - err.span_suggestion_with_applicability( - expr.span, - &format!( - "change the type of the numeric literal from `{}` to `{}`", - checked_ty, - expected_ty, - ), - suffix_suggestion, - Applicability::MaybeIncorrect, - ); - } else { - err.span_suggestion_with_applicability( - expr.span, - &format!("{}, which {}", msg, will_zero_extend), - into_suggestion, - Applicability::MachineApplicable - ); - } + suggest_to_change_suffix_or_into( + err, + Some(will_zero_extend), + ); } } true @@ -624,25 +624,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } (&ty::Float(ref exp), &ty::Float(ref found)) => { if found.bit_width() < exp.bit_width() { - if is_suffixed(expr) { - err.span_suggestion_with_applicability( - expr.span, - &format!( - "change the type of the numeric literal from `{}` to `{}`", - checked_ty, - expected_ty, - ), - suffix_suggestion, - Applicability::MaybeIncorrect, - ); - } else { - err.span_suggestion_with_applicability( - expr.span, - &format!("{} in a lossless way", msg), - into_suggestion, - Applicability::MachineApplicable - ); - } + suggest_to_change_suffix_or_into( + err, + None, + ); } else if can_cast { err.span_suggestion_with_applicability( expr.span, From 2fb6585f33bf1af3b6b5143347eb534cebd257e4 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Mon, 17 Sep 2018 09:29:57 +0800 Subject: [PATCH 11/16] add test for float/integer --- .../mismatched_types/numeric-literal-cast.rs | 7 ++++- .../numeric-literal-cast.stderr | 28 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/test/ui/mismatched_types/numeric-literal-cast.rs b/src/test/ui/mismatched_types/numeric-literal-cast.rs index 8d8c9d75973b6..516b2e8dd30bf 100644 --- a/src/test/ui/mismatched_types/numeric-literal-cast.rs +++ b/src/test/ui/mismatched_types/numeric-literal-cast.rs @@ -9,7 +9,12 @@ // except according to those terms. fn foo(_: u16) {} +fn foo1(_: f64) {} +fn foo2(_: i32) {} + fn main() { - foo(8u8); + foo(1u8); + foo1(2f32); + foo2(3i16); } diff --git a/src/test/ui/mismatched_types/numeric-literal-cast.stderr b/src/test/ui/mismatched_types/numeric-literal-cast.stderr index 8fda824311500..e2fe1a0914d4f 100644 --- a/src/test/ui/mismatched_types/numeric-literal-cast.stderr +++ b/src/test/ui/mismatched_types/numeric-literal-cast.stderr @@ -1,13 +1,33 @@ error[E0308]: mismatched types - --> $DIR/numeric-literal-cast.rs:13:9 + --> $DIR/numeric-literal-cast.rs:16:9 | -LL | foo(8u8); +LL | foo(1u8); | ^^^ expected u16, found u8 help: change the type of the numeric literal from `u8` to `u16` | -LL | foo(8u16); +LL | foo(1u16); | ^^^^ -error: aborting due to previous error +error[E0308]: mismatched types + --> $DIR/numeric-literal-cast.rs:17:10 + | +LL | foo1(2f32); + | ^^^^ expected f64, found f32 +help: change the type of the numeric literal from `f32` to `f64` + | +LL | foo1(2f64); + | ^^^^ + +error[E0308]: mismatched types + --> $DIR/numeric-literal-cast.rs:18:10 + | +LL | foo2(3i16); + | ^^^^ expected i32, found i16 +help: change the type of the numeric literal from `i16` to `i32` + | +LL | foo2(3i32); + | ^^^^ + +error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0308`. From e4e4039c5b70807edfe41f6511b56520b3786ef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alva=20Sn=C3=A6d=C3=ADs?= Date: Mon, 17 Sep 2018 03:38:15 +0000 Subject: [PATCH 12/16] libsyntax: fix casing in error message --- src/libsyntax/feature_gate.rs | 2 +- .../ui/feature-gates/feature-gate-panic-implementation.rs | 2 +- .../ui/feature-gates/feature-gate-panic-implementation.stderr | 4 ++-- .../panic-implementation-deprecated.stderr | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 7266d807d3ba8..603dfad38eb19 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -1138,7 +1138,7 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG Gated(Stability::Deprecated("https://github.com/rust-lang/rust/issues/44489\ #issuecomment-415140224"), "panic_implementation", - "This attribute was renamed to `panic_handler`", + "this attribute was renamed to `panic_handler`", cfg_fn!(panic_implementation))), // RFC 2070 diff --git a/src/test/ui/feature-gates/feature-gate-panic-implementation.rs b/src/test/ui/feature-gates/feature-gate-panic-implementation.rs index 84e5f302c1676..ca51154884f12 100644 --- a/src/test/ui/feature-gates/feature-gate-panic-implementation.rs +++ b/src/test/ui/feature-gates/feature-gate-panic-implementation.rs @@ -15,7 +15,7 @@ use core::panic::PanicInfo; -#[panic_implementation] //~ ERROR This attribute was renamed to `panic_handler` (see issue #44489) +#[panic_implementation] //~ ERROR this attribute was renamed to `panic_handler` (see issue #44489) fn panic(info: &PanicInfo) -> ! { loop {} } diff --git a/src/test/ui/feature-gates/feature-gate-panic-implementation.stderr b/src/test/ui/feature-gates/feature-gate-panic-implementation.stderr index 926a49ae83199..a54780468c42d 100644 --- a/src/test/ui/feature-gates/feature-gate-panic-implementation.stderr +++ b/src/test/ui/feature-gates/feature-gate-panic-implementation.stderr @@ -1,7 +1,7 @@ -error[E0658]: This attribute was renamed to `panic_handler` (see issue #44489) +error[E0658]: this attribute was renamed to `panic_handler` (see issue #44489) --> $DIR/feature-gate-panic-implementation.rs:18:1 | -LL | #[panic_implementation] //~ ERROR This attribute was renamed to `panic_handler` (see issue #44489) +LL | #[panic_implementation] //~ ERROR this attribute was renamed to `panic_handler` (see issue #44489) | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: add #![feature(panic_implementation)] to the crate attributes to enable diff --git a/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr b/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr index 43f51447ac46e..c869dddb715c6 100644 --- a/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr +++ b/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr @@ -1,4 +1,4 @@ -error: use of deprecated attribute `panic_implementation`: This attribute was renamed to `panic_handler`. See https://github.com/rust-lang/rust/issues/44489#issuecomment-415140224 +error: use of deprecated attribute `panic_implementation`: this attribute was renamed to `panic_handler`. See https://github.com/rust-lang/rust/issues/44489#issuecomment-415140224 --> $DIR/panic-implementation-deprecated.rs:19:1 | LL | #[panic_implementation] From 82e17504143e7f26427269fabfb99129a602c8b9 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Sat, 15 Sep 2018 06:27:55 +0200 Subject: [PATCH 13/16] Add -Z dont-buffer-diagnostics, a way to force NLL to immediately emit its diagnostics. This is mainly intended for `rustc` developers who want to see a diagnostic in its original context in the control flow. Two uses cases for that are: * `-Z treat-err-as-bug` which then allows extraction of a stack-trace to the origin of the error (a case that is so important that we make that flag imply this one, effectively). * `RUST_LOG=... rustc`, in which case it is often useful to see the logging statements that occurred immediately prior to the point where the diagnostic was signalled. Drive-by: Added some documentation pointing future devs at HandlerFlags, and documented the fields of `HandlerFlags` itself. --- src/librustc/session/config.rs | 2 ++ src/librustc/session/mod.rs | 2 ++ src/librustc_errors/diagnostic_builder.rs | 14 ++++++++++++-- src/librustc_errors/lib.rs | 11 +++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 2a9732bf02c98..25698129d82b3 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1331,6 +1331,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options, "disable user provided type assertion in NLL"), nll_dont_emit_read_for_match: bool = (false, parse_bool, [UNTRACKED], "in match codegen, do not include ReadForMatch statements (used by mir-borrowck)"), + dont_buffer_diagnostics: bool = (false, parse_bool, [UNTRACKED], + "emit diagnostics rather than buffering (breaks NLL error downgrading, sorting)."), polonius: bool = (false, parse_bool, [UNTRACKED], "enable polonius-based borrow-checker"), codegen_time_graph: bool = (false, parse_bool, [UNTRACKED], diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 619262abb0bf5..52e1ab477038d 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -1012,6 +1012,7 @@ pub fn build_session_with_source_map( let can_emit_warnings = !(warnings_allow || cap_lints_allow); let treat_err_as_bug = sopts.debugging_opts.treat_err_as_bug; + let dont_buffer_diagnostics = sopts.debugging_opts.dont_buffer_diagnostics; let report_delayed_bugs = sopts.debugging_opts.report_delayed_bugs; let external_macro_backtrace = sopts.debugging_opts.external_macro_backtrace; @@ -1059,6 +1060,7 @@ pub fn build_session_with_source_map( can_emit_warnings, treat_err_as_bug, report_delayed_bugs, + dont_buffer_diagnostics, external_macro_backtrace, ..Default::default() }, diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 1b34898b99084..5e962a4af32f6 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -21,6 +21,10 @@ use std::thread::panicking; use syntax_pos::{MultiSpan, Span}; /// Used for emitting structured error messages and other diagnostic information. +/// +/// If there is some state in a downstream crate you would like to +/// access in the methods of `DiagnosticBuilder` here, consider +/// extending `HandlerFlags`, accessed via `self.handler.flags`. #[must_use] #[derive(Clone)] pub struct DiagnosticBuilder<'a> { @@ -89,8 +93,14 @@ impl<'a> DiagnosticBuilder<'a> { self.cancel(); } - /// Buffers the diagnostic for later emission. - pub fn buffer(self, buffered_diagnostics: &mut Vec) { + /// Buffers the diagnostic for later emission, unless handler + /// has disabled such buffering. + pub fn buffer(mut self, buffered_diagnostics: &mut Vec) { + if self.handler.flags.dont_buffer_diagnostics || self.handler.flags.treat_err_as_bug { + self.emit(); + return; + } + // We need to use `ptr::read` because `DiagnosticBuilder` // implements `Drop`. let diagnostic; diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 3582c2359c8b9..d0ea6fba5ebb3 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -303,9 +303,20 @@ thread_local!(pub static TRACK_DIAGNOSTICS: Cell = #[derive(Default)] pub struct HandlerFlags { + /// If false, warning-level lints are suppressed. + /// (rustc: see `--allow warnings` and `--cap-lints`) pub can_emit_warnings: bool, + /// If true, error-level diagnostics are upgraded to bug-level. + /// (rustc: see `-Z treat-err-as-bug`) pub treat_err_as_bug: bool, + /// If true, immediately emit diagnostics that would otherwise be buffered. + /// (rustc: see `-Z dont-buffer-diagnostics` and `-Z treat-err-as-bug`) + pub dont_buffer_diagnostics: bool, + /// If true, immediately print bugs registered with `delay_span_bug`. + /// (rustc: see `-Z report-delayed-bugs`) pub report_delayed_bugs: bool, + /// show macro backtraces even for non-local macros. + /// (rustc: see `-Z external-macro-backtrace`) pub external_macro_backtrace: bool, } From 79da7a0a2f3c5490e1b8a4233795424bcf05520a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alva=20Sn=C3=A6d=C3=ADs?= Date: Mon, 17 Sep 2018 03:40:31 +0000 Subject: [PATCH 14/16] libsyntax: add optional help message for deprecated features --- src/librustc_lint/builtin.rs | 4 ++-- src/libsyntax/feature_gate.rs | 15 +++++++++------ .../feature-gate-dropck-ugeh-2.stderr | 2 +- .../panic-implementation-deprecated.stderr | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index b662b82501393..f9e717f8d456e 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -783,7 +783,7 @@ impl EarlyLintPass for DeprecatedAttr { fn check_attribute(&mut self, cx: &EarlyContext, attr: &ast::Attribute) { for &&(n, _, ref g) in &self.depr_attrs { if attr.name() == n { - if let &AttributeGate::Gated(Stability::Deprecated(link), + if let &AttributeGate::Gated(Stability::Deprecated(link, suggestion), ref name, ref reason, _) = g { @@ -792,7 +792,7 @@ impl EarlyLintPass for DeprecatedAttr { let mut err = cx.struct_span_lint(DEPRECATED, attr.span, &msg); err.span_suggestion_short_with_applicability( attr.span, - "remove this attribute", + suggestion.unwrap_or("remove this attribute"), String::new(), Applicability::MachineApplicable ); diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 603dfad38eb19..96328be2dadf2 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -711,7 +711,7 @@ pub enum AttributeGate { impl AttributeGate { fn is_deprecated(&self) -> bool { match *self { - Gated(Stability::Deprecated(_), ..) => true, + Gated(Stability::Deprecated(_, _), ..) => true, _ => false, } } @@ -720,8 +720,9 @@ impl AttributeGate { #[derive(Copy, Clone, Debug)] pub enum Stability { Unstable, - // Argument is tracking issue link. - Deprecated(&'static str), + // First argument is tracking issue link; second argument is an optional + // help message, which defaults to "remove this attribute" + Deprecated(&'static str, Option<&'static str>), } // fn() is not Debug @@ -1044,7 +1045,7 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG ("no_builtins", Whitelisted, Ungated), ("no_mangle", Whitelisted, Ungated), ("no_debug", Whitelisted, Gated( - Stability::Deprecated("https://github.com/rust-lang/rust/issues/29721"), + Stability::Deprecated("https://github.com/rust-lang/rust/issues/29721", None), "no_debug", "the `#[no_debug]` attribute was an experimental feature that has been \ deprecated due to lack of demand", @@ -1057,7 +1058,8 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG cfg_fn!(omit_gdb_pretty_printer_section))), ("unsafe_destructor_blind_to_params", Normal, - Gated(Stability::Deprecated("https://github.com/rust-lang/rust/issues/34761"), + Gated(Stability::Deprecated("https://github.com/rust-lang/rust/issues/34761", + Some("replace this attribute with `#[may_dangle]`")), "dropck_parametricity", "unsafe_destructor_blind_to_params has been replaced by \ may_dangle and will be removed in the future", @@ -1136,7 +1138,8 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG ("panic_implementation", Normal, Gated(Stability::Deprecated("https://github.com/rust-lang/rust/issues/44489\ - #issuecomment-415140224"), + #issuecomment-415140224", + Some("replace this attribute with `#[panic_handler]`")), "panic_implementation", "this attribute was renamed to `panic_handler`", cfg_fn!(panic_implementation))), diff --git a/src/test/ui/feature-gates/feature-gate-dropck-ugeh-2.stderr b/src/test/ui/feature-gates/feature-gate-dropck-ugeh-2.stderr index 80d81ea03cb6d..d3d5bd498cfe8 100644 --- a/src/test/ui/feature-gates/feature-gate-dropck-ugeh-2.stderr +++ b/src/test/ui/feature-gates/feature-gate-dropck-ugeh-2.stderr @@ -2,7 +2,7 @@ error: use of deprecated attribute `dropck_parametricity`: unsafe_destructor_bli --> $DIR/feature-gate-dropck-ugeh-2.rs:17:5 | LL | #[unsafe_destructor_blind_to_params] - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace this attribute with `#[may_dangle]` | note: lint level defined here --> $DIR/feature-gate-dropck-ugeh-2.rs:11:9 diff --git a/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr b/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr index c869dddb715c6..fabfba94878f5 100644 --- a/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr +++ b/src/test/ui/panic-implementation/panic-implementation-deprecated.stderr @@ -2,7 +2,7 @@ error: use of deprecated attribute `panic_implementation`: this attribute was re --> $DIR/panic-implementation-deprecated.rs:19:1 | LL | #[panic_implementation] - | ^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute + | ^^^^^^^^^^^^^^^^^^^^^^^ help: replace this attribute with `#[panic_handler]` | note: lint level defined here --> $DIR/panic-implementation-deprecated.rs:13:9 From ff617943e9c0f67742ab47c162210918bb2a3e1d Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 17 Sep 2018 20:11:29 -0400 Subject: [PATCH 15/16] Remove REAMDE with now-out-of-date docs about docs. --- src/doc/README.md | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 src/doc/README.md diff --git a/src/doc/README.md b/src/doc/README.md deleted file mode 100644 index 5f25894afd76d..0000000000000 --- a/src/doc/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# Rust documentations - -## Building - -To generate all the docs, follow the "Building Documentation" instructions in -the README in the root of the repository. This will convert the distributed -Markdown docs to HTML and generate HTML doc for the books, 'std' and 'extra' -libraries. - -To generate HTML documentation from one source file/crate, do something like: - -~~~~text -rustdoc --output html-doc/ --output-format html ../src/libstd/path.rs -~~~~ - -(This, of course, requires a working build of the `rustdoc` tool.) - -## Additional notes - -To generate an HTML version of a doc from Markdown manually, you can do -something like: - -~~~~text -rustdoc reference.md -~~~~ - -(`reference.md` being the Rust Reference Manual.) - -An overview of how to use the `rustdoc` command is available [in the docs][1]. -Further details are available from the command line by with `rustdoc --help`. - -[1]: https://github.com/rust-lang/rust/blob/master/src/doc/rustdoc/src/what-is-rustdoc.md From 993d02283ec9802ad413069dcdbbf9ef464b636e Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 17 Sep 2018 21:10:36 -0400 Subject: [PATCH 16/16] OsStr: Document that it's not NUL terminated I somehow got confused into thinking this was the case, but it's definitely not. Let's help the common case of people who have an `OsStr` and need to call e.g. Unix APIs. --- src/libstd/ffi/os_str.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index 237af2f04e59d..e9390630445a1 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -34,7 +34,9 @@ use sys_common::{AsInner, IntoInner, FromInner}; /// /// `OsString` and [`OsStr`] bridge this gap by simultaneously representing Rust /// and platform-native string values, and in particular allowing a Rust string -/// to be converted into an "OS" string with no cost if possible. +/// to be converted into an "OS" string with no cost if possible. A consequence +/// of this is that `OsString` instances are *not* `NUL` terminated; in order +/// to pass to e.g. Unix system call, you should create a [`CStr`]. /// /// `OsString` is to [`&OsStr`] as [`String`] is to [`&str`]: the former /// in each pair are owned strings; the latter are borrowed @@ -65,6 +67,7 @@ use sys_common::{AsInner, IntoInner, FromInner}; /// /// [`OsStr`]: struct.OsStr.html /// [`&OsStr`]: struct.OsStr.html +/// [`CStr`]: struct.CStr.html /// [`From`]: ../convert/trait.From.html /// [`String`]: ../string/struct.String.html /// [`&str`]: ../primitive.str.html