From 4b536a63f07af3d69d9aef4002c3e7dabb449db9 Mon Sep 17 00:00:00 2001 From: Charisee Date: Fri, 4 Feb 2022 19:16:29 +0000 Subject: [PATCH 01/10] added documentation for cfg_panic --- src/attributes/testing.md | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index e0181b1c3..8a7b048c3 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -81,6 +81,50 @@ fn mytest() { assert_eq!(1, 2, "values don't match"); } ``` +### Different panic strategies + +The `cfg_panic` feature makes it possible to exercise different lines of code depending on the panic strategy. +The possible value is either `unwind` or `abort`. +The following is a playful example on choosing the right beverage. + +```toml +#![feature(cfg_panic)] + +#[cfg(panic = "unwind")] +fn ah(){ println!("Spit it out!!!!");} + +#[cfg(not(panic="unwind"))] +fn ah(){ println!("This is not your party. Run!!!!");} + +fn drink(beverage: &str){ + if beverage == "lemonade"{ ah();} + else{println!("Some refreshing {} is all I need.", beverage);} +} + +fn main(){ + drink("water"); + drink("lemonade"); +} +``` + +Here is the same example rewritten. +```toml +#![feature(cfg_panic)] + +fn drink(beverage: &str) { + // You shouldn't drink too much sugary beverages. + if beverage == "lemonade" { + if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");} + else{ println!("Spit it out!!!!");} + } + else{ println!("Some refreshing {} is all I need.", beverage); } +} + +fn main() { + drink("water"); + drink("lemonade"); +} +``` [_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax [_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax From 976ce291dc8759876985eea61e4059065efbbed5 Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Fri, 4 Feb 2022 11:35:16 -0800 Subject: [PATCH 02/10] Update testing.md --- src/attributes/testing.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index 8a7b048c3..0781a8d08 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -87,7 +87,7 @@ The `cfg_panic` feature makes it possible to exercise different lines of code de The possible value is either `unwind` or `abort`. The following is a playful example on choosing the right beverage. -```toml +```rust #![feature(cfg_panic)] #[cfg(panic = "unwind")] @@ -108,7 +108,7 @@ fn main(){ ``` Here is the same example rewritten. -```toml +```rust #![feature(cfg_panic)] fn drink(beverage: &str) { From 031945be5883fc102d8ad4e867405f70ad8cd8f8 Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Fri, 4 Feb 2022 11:37:10 -0800 Subject: [PATCH 03/10] Update testing.md --- src/attributes/testing.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index 0781a8d08..5e0c1a01a 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -97,13 +97,13 @@ fn ah(){ println!("Spit it out!!!!");} fn ah(){ println!("This is not your party. Run!!!!");} fn drink(beverage: &str){ - if beverage == "lemonade"{ ah();} - else{println!("Some refreshing {} is all I need.", beverage);} + if beverage == "lemonade"{ ah();} + else{println!("Some refreshing {} is all I need.", beverage);} } fn main(){ - drink("water"); - drink("lemonade"); + drink("water"); + drink("lemonade"); } ``` @@ -113,11 +113,11 @@ Here is the same example rewritten. fn drink(beverage: &str) { // You shouldn't drink too much sugary beverages. - if beverage == "lemonade" { - if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");} - else{ println!("Spit it out!!!!");} - } - else{ println!("Some refreshing {} is all I need.", beverage); } + if beverage == "lemonade" { + if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");} + else{ println!("Spit it out!!!!");} + } + else{ println!("Some refreshing {} is all I need.", beverage); } } fn main() { From 0cc85c7147d831995c70c29ecd3439e55405c04d Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Fri, 4 Feb 2022 11:38:51 -0800 Subject: [PATCH 04/10] Update testing.md --- src/attributes/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index 5e0c1a01a..ef552bede 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -81,7 +81,7 @@ fn mytest() { assert_eq!(1, 2, "values don't match"); } ``` -### Different panic strategies +## Different panic strategies The `cfg_panic` feature makes it possible to exercise different lines of code depending on the panic strategy. The possible value is either `unwind` or `abort`. From 7ab0aa022d9c28118468926709378c11308a4994 Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Fri, 4 Feb 2022 11:43:45 -0800 Subject: [PATCH 05/10] Update testing.md --- src/attributes/testing.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index ef552bede..edb2e8333 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -88,7 +88,6 @@ The possible value is either `unwind` or `abort`. The following is a playful example on choosing the right beverage. ```rust -#![feature(cfg_panic)] #[cfg(panic = "unwind")] fn ah(){ println!("Spit it out!!!!");} @@ -109,7 +108,6 @@ fn main(){ Here is the same example rewritten. ```rust -#![feature(cfg_panic)] fn drink(beverage: &str) { // You shouldn't drink too much sugary beverages. From 3e2faa1d9cded9a49fbe09ad73d33bb2a045ae9a Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Mon, 7 Feb 2022 10:04:31 -0800 Subject: [PATCH 06/10] Update testing.md --- src/attributes/testing.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index edb2e8333..dab584e82 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -85,6 +85,7 @@ fn mytest() { The `cfg_panic` feature makes it possible to exercise different lines of code depending on the panic strategy. The possible value is either `unwind` or `abort`. + The following is a playful example on choosing the right beverage. ```rust @@ -124,6 +125,9 @@ fn main() { } ``` +Note that when the code is compiled with a certain panic strategy it still might do something different if a crate was compiled with a different strategy. +For instance, if `#[cfg(panic = "unwind")]` is set to `true` and a crate is compiled with`-C panic=abort`. + [_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax [_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax [`Termination`]: ../../std/process/trait.Termination.html From 09eadce94c28723d2ed9a5d02d1c3d79c94b713c Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Mon, 7 Feb 2022 12:50:19 -0800 Subject: [PATCH 07/10] Update src/attributes/testing.md Co-authored-by: bjorn3 --- src/attributes/testing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index dab584e82..4ed022a6b 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -125,7 +125,7 @@ fn main() { } ``` -Note that when the code is compiled with a certain panic strategy it still might do something different if a crate was compiled with a different strategy. +Note that when the code is compiled with a certain panic strategy it still might do something different if another crate was compiled with a different strategy. For instance, if `#[cfg(panic = "unwind")]` is set to `true` and a crate is compiled with`-C panic=abort`. [_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax From 14bb695ab22335e7232d99e562624529c895d0ef Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Mon, 14 Mar 2022 19:56:11 +0000 Subject: [PATCH 08/10] Added cfg panic to conditional-compilation section --- src/conditional-compilation.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index 8085cbfab..dbe204731 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -253,6 +253,19 @@ fn on_32bit_unix() { fn needs_not_foo() { // ... } + +// This function is only included when the panic strategy is set to unwind +#[cfg(panic = "unwind")] +fn when_unwinding() { + // ... +} + +// This function is only included when the panic strategy is not set to unwind +#[cfg(not(panic="unwind"))] +fn when_aborting() { + // ... +} + ``` The `cfg` attribute is allowed anywhere attributes are allowed. From 464866ff9d9169d921ae20eb27f916128321f05b Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Mon, 14 Mar 2022 20:03:46 +0000 Subject: [PATCH 09/10] Add files via upload --- src/attributes/testing.md | 46 --------------------------------------- 1 file changed, 46 deletions(-) diff --git a/src/attributes/testing.md b/src/attributes/testing.md index 4ed022a6b..e0181b1c3 100644 --- a/src/attributes/testing.md +++ b/src/attributes/testing.md @@ -81,52 +81,6 @@ fn mytest() { assert_eq!(1, 2, "values don't match"); } ``` -## Different panic strategies - -The `cfg_panic` feature makes it possible to exercise different lines of code depending on the panic strategy. -The possible value is either `unwind` or `abort`. - -The following is a playful example on choosing the right beverage. - -```rust - -#[cfg(panic = "unwind")] -fn ah(){ println!("Spit it out!!!!");} - -#[cfg(not(panic="unwind"))] -fn ah(){ println!("This is not your party. Run!!!!");} - -fn drink(beverage: &str){ - if beverage == "lemonade"{ ah();} - else{println!("Some refreshing {} is all I need.", beverage);} -} - -fn main(){ - drink("water"); - drink("lemonade"); -} -``` - -Here is the same example rewritten. -```rust - -fn drink(beverage: &str) { - // You shouldn't drink too much sugary beverages. - if beverage == "lemonade" { - if cfg!(panic="abort"){ println!("This is not your party. Run!!!!");} - else{ println!("Spit it out!!!!");} - } - else{ println!("Some refreshing {} is all I need.", beverage); } -} - -fn main() { - drink("water"); - drink("lemonade"); -} -``` - -Note that when the code is compiled with a certain panic strategy it still might do something different if another crate was compiled with a different strategy. -For instance, if `#[cfg(panic = "unwind")]` is set to `true` and a crate is compiled with`-C panic=abort`. [_MetaListNameValueStr_]: ../attributes.md#meta-item-attribute-syntax [_MetaNameValueStr_]: ../attributes.md#meta-item-attribute-syntax From 267f8159975451c3b61121156e5ee57d037937c4 Mon Sep 17 00:00:00 2001 From: Charisee Chiw Date: Mon, 14 Mar 2022 23:05:54 +0000 Subject: [PATCH 10/10] Add files via upload --- src/conditional-compilation.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/conditional-compilation.md b/src/conditional-compilation.md index dbe204731..6966cec4f 100644 --- a/src/conditional-compilation.md +++ b/src/conditional-compilation.md @@ -208,6 +208,15 @@ production. For example, it controls the behavior of the standard library's Set when the crate being compiled is being compiled with the `proc_macro` [crate type]. +### `panic` + +Key-value option set depending on the panic strategy. Note that more values may be added in the future. + +Example values: + +* `"abort"` +* `"unwind"` + ## Forms of conditional compilation ### The `cfg` attribute @@ -260,12 +269,6 @@ fn when_unwinding() { // ... } -// This function is only included when the panic strategy is not set to unwind -#[cfg(not(panic="unwind"))] -fn when_aborting() { - // ... -} - ``` The `cfg` attribute is allowed anywhere attributes are allowed.