From 37ce212f1f0a009a181d3a0a27dbd52505d4ac07 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Wed, 23 Sep 2020 12:01:25 +0200 Subject: [PATCH 01/11] make exp_m1 examples more representative of use With this commit, the examples for exp_m1 would fail if x.exp() - 1.0 is used instead of x.exp_m1(). --- library/std/src/f32.rs | 9 +++++---- library/std/src/f64.rs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/library/std/src/f32.rs b/library/std/src/f32.rs index 59c2da5273bde..cd9065b3a2115 100644 --- a/library/std/src/f32.rs +++ b/library/std/src/f32.rs @@ -719,12 +719,13 @@ impl f32 { /// # Examples /// /// ``` - /// let x = 6.0f32; + /// let x = 1e-8_f32; /// - /// // e^(ln(6)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 5.0).abs(); + /// // for very small x, e^x is approximately 1 + x + x^2 / 2 + /// let approx = x + x * x / 2.0; + /// let abs_difference = (x.exp_m1() - approx).abs(); /// - /// assert!(abs_difference <= f32::EPSILON); + /// assert!(abs_difference < 1e-10); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/f64.rs b/library/std/src/f64.rs index bd094bdb55dc3..e412f89432c76 100644 --- a/library/std/src/f64.rs +++ b/library/std/src/f64.rs @@ -721,12 +721,13 @@ impl f64 { /// # Examples /// /// ``` - /// let x = 7.0_f64; + /// let x = 1e-16_f64; /// - /// // e^(ln(7)) - 1 - /// let abs_difference = (x.ln().exp_m1() - 6.0).abs(); + /// // for very small x, e^x is approximately 1 + x + x^2 / 2 + /// let approx = x + x * x / 2.0; + /// let abs_difference = (x.exp_m1() - approx).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference < 1e-20); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] From 50d3ddcb0cbc36f782fa5939d1ef24422f6902d4 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Wed, 23 Sep 2020 12:02:49 +0200 Subject: [PATCH 02/11] make ln_1p examples more representative of use With this commit, the examples for ln_1p would fail if (x + 1.0).ln() is used instead of x.ln_1p(). --- library/std/src/f32.rs | 9 +++++---- library/std/src/f64.rs | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/library/std/src/f32.rs b/library/std/src/f32.rs index cd9065b3a2115..ed975c4287981 100644 --- a/library/std/src/f32.rs +++ b/library/std/src/f32.rs @@ -740,12 +740,13 @@ impl f32 { /// # Examples /// /// ``` - /// let x = std::f32::consts::E - 1.0; + /// let x = 1e-8_f32; /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); + /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 + /// let approx = x - x * x / 2.0; + /// let abs_difference = (x.ln_1p() - approx).abs(); /// - /// assert!(abs_difference <= f32::EPSILON); + /// assert!(abs_difference < 1e-10); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/f64.rs b/library/std/src/f64.rs index e412f89432c76..8d0a85e056f71 100644 --- a/library/std/src/f64.rs +++ b/library/std/src/f64.rs @@ -742,12 +742,13 @@ impl f64 { /// # Examples /// /// ``` - /// let x = std::f64::consts::E - 1.0; + /// let x = 1e-16_f64; /// - /// // ln(1 + (e - 1)) == ln(e) == 1 - /// let abs_difference = (x.ln_1p() - 1.0).abs(); + /// // for very small x, ln(1 + x) is approximately x - x^2 / 2 + /// let approx = x - x * x / 2.0; + /// let abs_difference = (x.ln_1p() - approx).abs(); /// - /// assert!(abs_difference < 1e-10); + /// assert!(abs_difference < 1e-20); /// ``` #[must_use = "method returns a new number and does not mutate the original value"] #[stable(feature = "rust1", since = "1.0.0")] From 01b0aff1df1dd5ee7c60e8fbeff15cc3edaa3208 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 22 Jul 2020 12:17:36 +0200 Subject: [PATCH 03/11] Add std::panic::panic_box. --- library/std/src/panic.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 4281867314cca..06ce66c10f7ea 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -23,6 +23,20 @@ pub use crate::panicking::{set_hook, take_hook}; #[stable(feature = "panic_hooks", since = "1.10.0")] pub use core::panic::{Location, PanicInfo}; +/// Panic the current thread, with the given payload as the panic message. +/// +/// This supports an arbitrary panic payload, instead of just (formatted) strings. +/// +/// The message is attached as a `Box<'static + Any + Send>`, which can be +/// accessed using [`PanicInfo::payload`]. +/// +/// See the [`panic!`] macro for more information about panicking. +#[unstable(feature = "panic_box", issue = "none")] +#[inline] +pub fn panic_box(msg: M) -> ! { + crate::panicking::begin_panic(msg); +} + /// A marker trait which represents "panic safe" types in Rust. /// /// This trait is implemented by default for many types and behaves similarly in From 16201da6a4ff613d00ca3680c43cbb1b52f60cf1 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 12:29:13 +0200 Subject: [PATCH 04/11] Rename panic_box to panic_any. --- library/std/src/panic.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 06ce66c10f7ea..9a756e4bbb4d4 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -23,17 +23,17 @@ pub use crate::panicking::{set_hook, take_hook}; #[stable(feature = "panic_hooks", since = "1.10.0")] pub use core::panic::{Location, PanicInfo}; -/// Panic the current thread, with the given payload as the panic message. +/// Panic the current thread with the given message as the panic payload. /// -/// This supports an arbitrary panic payload, instead of just (formatted) strings. +/// The message can be of any (`Any + Send`) type, not just strings. /// -/// The message is attached as a `Box<'static + Any + Send>`, which can be -/// accessed using [`PanicInfo::payload`]. +/// The message is wrapped in a `Box<'static + Any + Send>`, which can be +/// accessed later using [`PanicInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. #[unstable(feature = "panic_box", issue = "none")] #[inline] -pub fn panic_box(msg: M) -> ! { +pub fn panic_any(msg: M) -> ! { crate::panicking::begin_panic(msg); } From 9e16213610f606de0e44ac2b26ab9666bf0e83bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 26 Oct 2020 16:28:56 -0700 Subject: [PATCH 05/11] Suggest calling associated `fn` inside `trait`s When calling a function that doesn't exist inside of a trait's associated `fn`, and another associated `fn` in that trait has that name, suggest calling it with the appropriate fully-qualified path. Expand the label to be more descriptive. Prompted by the following user experience: https://users.rust-lang.org/t/cannot-find-function/50663 --- compiler/rustc_resolve/src/late.rs | 24 +++----- .../rustc_resolve/src/late/diagnostics.rs | 58 +++++++++++++++---- .../ui/resolve/associated-fn-called-as-fn.rs | 32 ++++++++++ .../resolve/associated-fn-called-as-fn.stderr | 15 +++++ src/test/ui/resolve/issue-14254.stderr | 30 +++++----- src/test/ui/resolve/issue-2356.stderr | 6 +- .../resolve/resolve-assoc-suggestions.stderr | 4 +- .../resolve-speculative-adjustment.stderr | 2 +- .../assoc-type-in-method-return.stderr | 2 +- 9 files changed, 124 insertions(+), 49 deletions(-) create mode 100644 src/test/ui/resolve/associated-fn-called-as-fn.rs create mode 100644 src/test/ui/resolve/associated-fn-called-as-fn.stderr diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 7517ab66170a2..9fe42590d3eae 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -353,8 +353,8 @@ impl<'a> PathSource<'a> { #[derive(Default)] struct DiagnosticMetadata<'ast> { - /// The current trait's associated types' ident, used for diagnostic suggestions. - current_trait_assoc_types: Vec, + /// The current trait's associated items' ident, used for diagnostic suggestions. + current_trait_assoc_items: Option<&'ast [P]>, /// The current self type if inside an impl (used for better errors). current_self_type: Option, @@ -1148,26 +1148,18 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { result } - /// When evaluating a `trait` use its associated types' idents for suggestionsa in E0412. + /// When evaluating a `trait` use its associated types' idents for suggestions in E0412. fn with_trait_items( &mut self, - trait_items: &Vec>, + trait_items: &'ast Vec>, f: impl FnOnce(&mut Self) -> T, ) -> T { - let trait_assoc_types = replace( - &mut self.diagnostic_metadata.current_trait_assoc_types, - trait_items - .iter() - .filter_map(|item| match &item.kind { - AssocItemKind::TyAlias(_, _, bounds, _) if bounds.is_empty() => { - Some(item.ident) - } - _ => None, - }) - .collect(), + let trait_assoc_items = replace( + &mut self.diagnostic_metadata.current_trait_assoc_items, + Some(&trait_items[..]), ); let result = f(self); - self.diagnostic_metadata.current_trait_assoc_types = trait_assoc_types; + self.diagnostic_metadata.current_trait_assoc_items = trait_assoc_items; result } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index c24b383f3b811..75dc54e4d6531 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -30,7 +30,21 @@ type Res = def::Res; enum AssocSuggestion { Field, MethodWithSelf, - AssocItem, + AssocFn, + AssocType, + AssocConst, +} + +impl AssocSuggestion { + fn action(&self) -> &'static str { + match self { + AssocSuggestion::Field => "use the available field", + AssocSuggestion::MethodWithSelf => "call the method with the fully-qualified path", + AssocSuggestion::AssocFn => "call the associated function", + AssocSuggestion::AssocConst => "use the associated `const`", + AssocSuggestion::AssocType => "use the associated type", + } + } } crate enum MissingLifetimeSpot<'tcx> { @@ -386,15 +400,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { AssocSuggestion::MethodWithSelf if self_is_available => { err.span_suggestion( span, - "try", + "you might have meant to call the method", format!("self.{}", path_str), Applicability::MachineApplicable, ); } - AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => { + AssocSuggestion::MethodWithSelf + | AssocSuggestion::AssocFn + | AssocSuggestion::AssocConst + | AssocSuggestion::AssocType => { err.span_suggestion( span, - "try", + &format!("you might have meant to {}", candidate.action()), format!("Self::{}", path_str), Applicability::MachineApplicable, ); @@ -1048,9 +1065,19 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { } } - for assoc_type_ident in &self.diagnostic_metadata.current_trait_assoc_types { - if *assoc_type_ident == ident { - return Some(AssocSuggestion::AssocItem); + if let Some(items) = self.diagnostic_metadata.current_trait_assoc_items { + for assoc_item in &items[..] { + if assoc_item.ident == ident { + return Some(match &assoc_item.kind { + ast::AssocItemKind::Const(..) => AssocSuggestion::AssocConst, + ast::AssocItemKind::Fn(_, sig, ..) if sig.decl.has_self() => { + AssocSuggestion::MethodWithSelf + } + ast::AssocItemKind::Fn(..) => AssocSuggestion::AssocFn, + ast::AssocItemKind::TyAlias(..) => AssocSuggestion::AssocType, + ast::AssocItemKind::MacCall(_) => continue, + }); + } } } @@ -1066,11 +1093,20 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> { ) { let res = binding.res(); if filter_fn(res) { - return Some(if self.r.has_self.contains(&res.def_id()) { - AssocSuggestion::MethodWithSelf + if self.r.has_self.contains(&res.def_id()) { + return Some(AssocSuggestion::MethodWithSelf); } else { - AssocSuggestion::AssocItem - }); + match res { + Res::Def(DefKind::AssocFn, _) => return Some(AssocSuggestion::AssocFn), + Res::Def(DefKind::AssocConst, _) => { + return Some(AssocSuggestion::AssocConst); + } + Res::Def(DefKind::AssocTy, _) => { + return Some(AssocSuggestion::AssocType); + } + _ => {} + } + } } } } diff --git a/src/test/ui/resolve/associated-fn-called-as-fn.rs b/src/test/ui/resolve/associated-fn-called-as-fn.rs new file mode 100644 index 0000000000000..f31f3d67b5be5 --- /dev/null +++ b/src/test/ui/resolve/associated-fn-called-as-fn.rs @@ -0,0 +1,32 @@ +struct S; +impl Foo for S { + fn parse(s:&str) { + for c in s.chars() { + match c { + '0'..='9' => collect_primary(&c), //~ ERROR cannot find function `collect_primary` + //~^ HELP you might have meant to call the associated function + '+' | '-' => println!("We got a sign: {}", c), + _ => println!("Not a number!") + } + } + } +} +trait Foo { + fn collect_primary(ch:&char) { } + fn parse(s:&str); +} +trait Bar { + fn collect_primary(ch:&char) { } + fn parse(s:&str) { + for c in s.chars() { + match c { + '0'..='9' => collect_primary(&c), //~ ERROR cannot find function `collect_primary` + //~^ HELP you might have meant to call the associated function + '+' | '-' => println!("We got a sign: {}", c), + _ => println!("Not a number!") + } + } + } +} + +fn main() {} diff --git a/src/test/ui/resolve/associated-fn-called-as-fn.stderr b/src/test/ui/resolve/associated-fn-called-as-fn.stderr new file mode 100644 index 0000000000000..fbdea30d551fd --- /dev/null +++ b/src/test/ui/resolve/associated-fn-called-as-fn.stderr @@ -0,0 +1,15 @@ +error[E0425]: cannot find function `collect_primary` in this scope + --> $DIR/associated-fn-called-as-fn.rs:6:30 + | +LL | '0'..='9' => collect_primary(&c), + | ^^^^^^^^^^^^^^^ help: you might have meant to call the associated function: `Self::collect_primary` + +error[E0425]: cannot find function `collect_primary` in this scope + --> $DIR/associated-fn-called-as-fn.rs:23:30 + | +LL | '0'..='9' => collect_primary(&c), + | ^^^^^^^^^^^^^^^ help: you might have meant to call the associated function: `Self::collect_primary` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0425`. diff --git a/src/test/ui/resolve/issue-14254.stderr b/src/test/ui/resolve/issue-14254.stderr index 97d42aa8ef4a5..b1f45adb8b714 100644 --- a/src/test/ui/resolve/issue-14254.stderr +++ b/src/test/ui/resolve/issue-14254.stderr @@ -2,7 +2,7 @@ error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:19:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `a` in this scope --> $DIR/issue-14254.rs:21:9 @@ -14,7 +14,7 @@ error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:28:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `x` in this scope --> $DIR/issue-14254.rs:30:9 @@ -38,7 +38,7 @@ error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:36:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error[E0425]: cannot find value `b` in this scope --> $DIR/issue-14254.rs:38:9 @@ -50,7 +50,7 @@ error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:45:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `x` in this scope --> $DIR/issue-14254.rs:47:9 @@ -74,7 +74,7 @@ error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:53:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error[E0425]: cannot find value `b` in this scope --> $DIR/issue-14254.rs:55:9 @@ -86,61 +86,61 @@ error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:62:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:64:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:71:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:73:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:80:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:82:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:89:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:91:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error[E0425]: cannot find function `baz` in this scope --> $DIR/issue-14254.rs:98:9 | LL | baz(); - | ^^^ help: try: `self.baz` + | ^^^ help: you might have meant to call the method: `self.baz` error[E0425]: cannot find value `bah` in this scope --> $DIR/issue-14254.rs:100:9 | LL | bah; - | ^^^ help: try: `Self::bah` + | ^^^ help: you might have meant to call the associated function: `Self::bah` error: aborting due to 24 previous errors diff --git a/src/test/ui/resolve/issue-2356.stderr b/src/test/ui/resolve/issue-2356.stderr index 0339daa0d6a18..8083233c01b92 100644 --- a/src/test/ui/resolve/issue-2356.stderr +++ b/src/test/ui/resolve/issue-2356.stderr @@ -8,7 +8,7 @@ error[E0425]: cannot find function `clone` in this scope --> $DIR/issue-2356.rs:24:5 | LL | clone(); - | ^^^^^ help: try: `self.clone` + | ^^^^^ help: you might have meant to call the method: `self.clone` error[E0425]: cannot find function `default` in this scope --> $DIR/issue-2356.rs:31:5 @@ -16,7 +16,7 @@ error[E0425]: cannot find function `default` in this scope LL | default(); | ^^^^^^^ | -help: try +help: you might have meant to call the associated function | LL | Self::default(); | ^^^^^^^^^^^^^ @@ -35,7 +35,7 @@ error[E0425]: cannot find function `shave` in this scope --> $DIR/issue-2356.rs:41:5 | LL | shave(4); - | ^^^^^ help: try: `Self::shave` + | ^^^^^ help: you might have meant to call the associated function: `Self::shave` error[E0425]: cannot find function `purr` in this scope --> $DIR/issue-2356.rs:43:5 diff --git a/src/test/ui/resolve/resolve-assoc-suggestions.stderr b/src/test/ui/resolve/resolve-assoc-suggestions.stderr index a05ac0f854395..b6acaeb8cc232 100644 --- a/src/test/ui/resolve/resolve-assoc-suggestions.stderr +++ b/src/test/ui/resolve/resolve-assoc-suggestions.stderr @@ -20,7 +20,7 @@ error[E0412]: cannot find type `Type` in this scope --> $DIR/resolve-assoc-suggestions.rs:23:16 | LL | let _: Type; - | ^^^^ help: try: `Self::Type` + | ^^^^ help: you might have meant to use the associated type: `Self::Type` error[E0531]: cannot find tuple struct or tuple variant `Type` in this scope --> $DIR/resolve-assoc-suggestions.rs:25:13 @@ -50,7 +50,7 @@ error[E0425]: cannot find value `method` in this scope --> $DIR/resolve-assoc-suggestions.rs:34:9 | LL | method; - | ^^^^^^ help: try: `self.method` + | ^^^^^^ help: you might have meant to call the method: `self.method` error: aborting due to 9 previous errors diff --git a/src/test/ui/resolve/resolve-speculative-adjustment.stderr b/src/test/ui/resolve/resolve-speculative-adjustment.stderr index 892b50309a905..1c34af6d0ffe5 100644 --- a/src/test/ui/resolve/resolve-speculative-adjustment.stderr +++ b/src/test/ui/resolve/resolve-speculative-adjustment.stderr @@ -20,7 +20,7 @@ error[E0425]: cannot find function `method` in this scope --> $DIR/resolve-speculative-adjustment.rs:25:9 | LL | method(); - | ^^^^^^ help: try: `self.method` + | ^^^^^^ help: you might have meant to call the method: `self.method` error: aborting due to 4 previous errors diff --git a/src/test/ui/suggestions/assoc-type-in-method-return.stderr b/src/test/ui/suggestions/assoc-type-in-method-return.stderr index bf908d36d2e3f..202e4a16eada7 100644 --- a/src/test/ui/suggestions/assoc-type-in-method-return.stderr +++ b/src/test/ui/suggestions/assoc-type-in-method-return.stderr @@ -2,7 +2,7 @@ error[E0412]: cannot find type `Bla` in this scope --> $DIR/assoc-type-in-method-return.rs:3:25 | LL | fn to_bla(&self) -> Bla; - | ^^^ help: try: `Self::Bla` + | ^^^ help: you might have meant to use the associated type: `Self::Bla` error: aborting due to previous error From a9d334d386e5abf79d8ee60f94bf32147b755c4c Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 28 Oct 2020 21:21:41 +0100 Subject: [PATCH 06/11] Update panic_any feature name. Co-authored-by: Camelid --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 9a756e4bbb4d4..ad91933d65102 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -31,7 +31,7 @@ pub use core::panic::{Location, PanicInfo}; /// accessed later using [`PanicInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. -#[unstable(feature = "panic_box", issue = "none")] +#[unstable(feature = "panic_any", issue = "none")] #[inline] pub fn panic_any(msg: M) -> ! { crate::panicking::begin_panic(msg); From b48fee010c92dde304154ba45c0e41d396e60568 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 28 Oct 2020 21:23:45 +0100 Subject: [PATCH 07/11] Add tracking issue number for panic_any. --- library/std/src/panic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index ad91933d65102..d18b94b6c1aef 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -31,7 +31,7 @@ pub use core::panic::{Location, PanicInfo}; /// accessed later using [`PanicInfo::payload`]. /// /// See the [`panic!`] macro for more information about panicking. -#[unstable(feature = "panic_any", issue = "none")] +#[unstable(feature = "panic_any", issue = "78500")] #[inline] pub fn panic_any(msg: M) -> ! { crate::panicking::begin_panic(msg); From dcbf2f324f3e3c116b3a7f45b501e2382e89510d Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 29 Oct 2020 09:45:15 -0700 Subject: [PATCH 08/11] rustc_llvm: unwrap LLVMMetadataRef before casting Directly casting the opaque pointer was [reported] to cause an "incomplete type" error with GCC 9.3: ``` llvm-wrapper/RustWrapper.cpp:939:31: required from here /usr/include/c++/9.3/type_traits:1301:12: error: invalid use of incomplete type 'struct LLVMOpaqueMetadata' 1301 | struct is_base_of | ^~~~~~~~~~ In file included from [...]/rust/src/llvm-project/llvm/include/llvm-c/BitReader.h:23, from llvm-wrapper/LLVMWrapper.h:1, from llvm-wrapper/RustWrapper.cpp:1: [...]/rust/src/llvm-project/llvm/include/llvm-c/Types.h:89:16: note: forward declaration of 'struct LLVMOpaqueMetadata' 89 | typedef struct LLVMOpaqueMetadata *LLVMMetadataRef; | ^~~~~~~~~~~~~~~~~~ ``` [reported]: https://zulip-archive.rust-lang.org/182449tcompilerhelp/12215halprustcllvmbuildfail.html#214915124 A simple `unwrap` fixes the issue. --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 3beb328339e7a..c689ac9427abe 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -936,7 +936,7 @@ extern "C" LLVMValueRef LLVMRustDIBuilderInsertDeclareAtEnd( return wrap(Builder->insertDeclare( unwrap(V), unwrap(VarInfo), Builder->createExpression(llvm::ArrayRef(AddrOps, AddrOpsCount)), - DebugLoc(cast(DL)), + DebugLoc(cast(unwrap(DL))), unwrap(InsertAtEnd))); } From ad278943ada85b8c283423e7ef21a7e28a6a1ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Fri, 30 Oct 2020 00:00:00 +0000 Subject: [PATCH 09/11] Move compiletest meta tests to a separate directory --- .../expected-error-correct-rev.a.stderr} | 2 +- .../expected-error-correct-rev.rs} | 2 +- src/test/ui/{meta-revision-bad.rs => meta/revision-bad.rs} | 0 src/test/ui/{meta-revision-ok.rs => meta/revision-ok.rs} | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename src/test/ui/{meta-expected-error-correct-rev.a.stderr => meta/expected-error-correct-rev.a.stderr} (89%) rename src/test/ui/{meta-expected-error-correct-rev.rs => meta/expected-error-correct-rev.rs} (68%) rename src/test/ui/{meta-revision-bad.rs => meta/revision-bad.rs} (100%) rename src/test/ui/{meta-revision-ok.rs => meta/revision-ok.rs} (83%) diff --git a/src/test/ui/meta-expected-error-correct-rev.a.stderr b/src/test/ui/meta/expected-error-correct-rev.a.stderr similarity index 89% rename from src/test/ui/meta-expected-error-correct-rev.a.stderr rename to src/test/ui/meta/expected-error-correct-rev.a.stderr index 5e6980a9dd1f7..df4dbdbc8e62d 100644 --- a/src/test/ui/meta-expected-error-correct-rev.a.stderr +++ b/src/test/ui/meta/expected-error-correct-rev.a.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/meta-expected-error-correct-rev.rs:7:18 + --> $DIR/expected-error-correct-rev.rs:7:18 | LL | let x: u32 = 22_usize; | --- ^^^^^^^^ expected `u32`, found `usize` diff --git a/src/test/ui/meta-expected-error-correct-rev.rs b/src/test/ui/meta/expected-error-correct-rev.rs similarity index 68% rename from src/test/ui/meta-expected-error-correct-rev.rs rename to src/test/ui/meta/expected-error-correct-rev.rs index b06a64b15c87c..26798c3dfc29f 100644 --- a/src/test/ui/meta-expected-error-correct-rev.rs +++ b/src/test/ui/meta/expected-error-correct-rev.rs @@ -1,6 +1,6 @@ // revisions: a -// Counterpart to `meta-expected-error-wrong-rev.rs` +// Counterpart to `expected-error-wrong-rev.rs` #[cfg(a)] fn foo() { diff --git a/src/test/ui/meta-revision-bad.rs b/src/test/ui/meta/revision-bad.rs similarity index 100% rename from src/test/ui/meta-revision-bad.rs rename to src/test/ui/meta/revision-bad.rs diff --git a/src/test/ui/meta-revision-ok.rs b/src/test/ui/meta/revision-ok.rs similarity index 83% rename from src/test/ui/meta-revision-ok.rs rename to src/test/ui/meta/revision-ok.rs index 7df9a6ea48fae..bbeae41b8bb95 100644 --- a/src/test/ui/meta-revision-ok.rs +++ b/src/test/ui/meta/revision-ok.rs @@ -1,5 +1,5 @@ // Meta test for compiletest: check that when we give the right error -// patterns, the test passes. See all `meta-revision-bad.rs`. +// patterns, the test passes. See all `revision-bad.rs`. // run-fail // revisions: foo bar From affb47fa5750bae1adfbbcf8032bb252369620cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Fri, 30 Oct 2020 00:00:00 +0000 Subject: [PATCH 10/11] Add a test for compiletest rustc-env & unset-rustc-env directives --- src/test/ui/meta/auxiliary/env.rs | 9 +++++++++ src/test/ui/meta/rustc-env.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/test/ui/meta/auxiliary/env.rs create mode 100644 src/test/ui/meta/rustc-env.rs diff --git a/src/test/ui/meta/auxiliary/env.rs b/src/test/ui/meta/auxiliary/env.rs new file mode 100644 index 0000000000000..b3644d8d5943f --- /dev/null +++ b/src/test/ui/meta/auxiliary/env.rs @@ -0,0 +1,9 @@ +// Check that aux builds can also use rustc-env, but environment is configured +// separately from the main test case. +// +// rustc-env:COMPILETEST_BAR=bar + +pub fn test() { + assert_eq!(option_env!("COMPILETEST_FOO"), None); + assert_eq!(env!("COMPILETEST_BAR"), "bar"); +} diff --git a/src/test/ui/meta/rustc-env.rs b/src/test/ui/meta/rustc-env.rs new file mode 100644 index 0000000000000..7d4e005be10cf --- /dev/null +++ b/src/test/ui/meta/rustc-env.rs @@ -0,0 +1,18 @@ +// Compiletest meta test checking that rustc-env and unset-rustc-env directives +// can be used to configure environment for rustc. +// +// run-pass +// aux-build:env.rs +// rustc-env:COMPILETEST_FOO=foo +// +// An environment variable that is likely to be set, but should be safe to unset. +// unset-rustc-env:PWD + +extern crate env; + +fn main() { + assert_eq!(env!("COMPILETEST_FOO"), "foo"); + assert_eq!(option_env!("COMPILETEST_BAR"), None); + assert_eq!(option_env!("PWD"), None); + env::test(); +} From 59c6ae615e5547610c3348b466a45ff2a5b3d935 Mon Sep 17 00:00:00 2001 From: Maarten de Vries Date: Fri, 30 Oct 2020 14:05:53 +0100 Subject: [PATCH 11/11] Use SOCK_CLOEXEC and accept4() on more platforms. --- library/std/src/sys/unix/net.rs | 38 +++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/library/std/src/sys/unix/net.rs b/library/std/src/sys/unix/net.rs index 74c7db27226ef..71c6aa5a0e7ea 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -55,9 +55,18 @@ impl Socket { pub fn new_raw(fam: c_int, ty: c_int) -> io::Result { unsafe { cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { - // On Linux we pass the SOCK_CLOEXEC flag to atomically create - // the socket and set it as CLOEXEC, added in 2.6.27. + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { + // On platforms that support it we pass the SOCK_CLOEXEC + // flag to atomically create the socket and set it as + // CLOEXEC. On Linux this was added in 2.6.27. let fd = cvt(libc::socket(fam, ty | libc::SOCK_CLOEXEC, 0))?; Ok(Socket(FileDesc::new(fd))) } else { @@ -83,7 +92,15 @@ impl Socket { let mut fds = [0, 0]; cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { // Like above, set cloexec atomically cvt(libc::socketpair(fam, ty | libc::SOCK_CLOEXEC, 0, fds.as_mut_ptr()))?; Ok((Socket(FileDesc::new(fds[0])), Socket(FileDesc::new(fds[1])))) @@ -174,9 +191,18 @@ impl Socket { pub fn accept(&self, storage: *mut sockaddr, len: *mut socklen_t) -> io::Result { // Unfortunately the only known way right now to accept a socket and // atomically set the CLOEXEC flag is to use the `accept4` syscall on - // Linux. This was added in 2.6.28, glibc 2.10 and musl 0.9.5. + // platforms that support it. On Linux, this was added in 2.6.28, + // glibc 2.10 and musl 0.9.5. cfg_if::cfg_if! { - if #[cfg(target_os = "linux")] { + if #[cfg(any( + target_os = "android", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "illumos", + target_os = "linux", + target_os = "netbsd", + target_os = "opensbd", + ))] { let fd = cvt_r(|| unsafe { libc::accept4(self.0.raw(), storage, len, libc::SOCK_CLOEXEC) })?;