about summary refs log tree commit diff
path: root/src/test/ui/unsafe
AgeCommit message (Collapse)AuthorLines
2023-01-11Move /src/test to /testsAlbert Larsan-2151/+0
2023-01-08Mention signature rather than fn pointers when comparing impl/trait methodsMichael Goulet-4/+4
2023-01-03Don't trim path for `unsafe_op_in_unsafe_fn` lintsLéo Lanteri Thauvin-0/+21
2022-11-04Formatting changes + add UI testclubby789-0/+50
2022-10-01bless ui testsMaybe Waffle-4/+4
2022-08-02never consider unsafe blocks unused if they would be required with ↵Ralf Jung-58/+6
unsafe_op_in_unsafe_fn
2022-07-13remove untagged_union feature gateRalf Jung-48/+29
2022-06-21Move some tests to more reasonable directoriesCaio-0/+95
2022-04-26Revert "add `DefId` to unsafety violations and display function path in E0133"Oli Scherer-43/+51
This reverts commit 8b8f6653cfd54525714f02efe7af0a0f830e185c.
2022-04-24only show a simple description in E0133 span labelEmil Gardström-12/+12
2022-04-24add `DefId` to unsafety violations and display function path in E0133Emil Gardström-34/+34
this enables consumers to access the function definition that was reported to be unsafe
2022-03-24Check if call return type is visibly uninhabited when building MIRTomasz Miąsko-4/+59
2022-03-21Don't run UB in test suiteSmitty-54/+107
This splits ui/unsafe/union.rs to make it so only the non-UB parts are run. It also means we can do more testing of the location of error messages.
2022-02-28Tweak diagnosticsEsteban Kuber-4/+8
* Recover from invalid `'label: ` before block. * Make suggestion to enclose statements in a block multipart. * Point at `match`, `while`, `loop` and `unsafe` keywords when failing to parse their expression. * Do not suggest `{ ; }`. * Do not suggest `|` when very unlikely to be what was wanted (in `let` statements).
2022-02-20Improve `unused_unsafe` lintFrank Steffahn-4/+16
Main motivation: Fixes some issues with the current behavior. This PR is more-or-less completely re-implementing the unused_unsafe lint; it’s also only done in the MIR-version of the lint, the set of tests for the `-Zthir-unsafeck` version no longer succeeds (and is thus disabled, see `lint-unused-unsafe.rs`). On current nightly, ```rs unsafe fn unsf() {} fn inner_ignored() { unsafe { #[allow(unused_unsafe)] unsafe { unsf() } } } ``` doesn’t create any warnings. This situation is not unrealistic to come by, the inner `unsafe` block could e.g. come from a macro. Actually, this PR even includes removal of one unused `unsafe` in the standard library that was missed in a similar situation. (The inner `unsafe` coming from an external macro hides the warning, too.) The reason behind this problem is how the check currently works: * While generating MIR, it already skips nested unsafe blocks (i.e. unsafe nested in other unsafe) so that the inner one is always the one considered unused * To differentiate the cases of no unsafe operations inside the `unsafe` vs. a surrounding `unsafe` block, there’s some ad-hoc magic walking up the HIR to look for surrounding used `unsafe` blocks. There’s a lot of problems with this approach besides the one presented above. E.g. the MIR-building uses checks for `unsafe_op_in_unsafe_fn` lint to decide early whether or not `unsafe` blocks in an `unsafe fn` are redundant and ought to be removed. ```rs unsafe fn granular_disallow_op_in_unsafe_fn() { unsafe { #[deny(unsafe_op_in_unsafe_fn)] { unsf(); } } } ``` ``` error: call to unsafe function is unsafe and requires unsafe block (error E0133) --> src/main.rs:13:13 | 13 | unsf(); | ^^^^^^ call to unsafe function | note: the lint level is defined here --> src/main.rs:11:16 | 11 | #[deny(unsafe_op_in_unsafe_fn)] | ^^^^^^^^^^^^^^^^^^^^^^ = note: consult the function's documentation for information on how to avoid undefined behavior warning: unnecessary `unsafe` block --> src/main.rs:10:5 | 9 | unsafe fn granular_disallow_op_in_unsafe_fn() { | --------------------------------------------- because it's nested under this `unsafe` fn 10 | unsafe { | ^^^^^^ unnecessary `unsafe` block | = note: `#[warn(unused_unsafe)]` on by default ``` Here, the intermediate `unsafe` was ignored, even though it contains a unsafe operation that is not allowed to happen in an `unsafe fn` without an additional `unsafe` block. Also closures were problematic and the workaround/algorithms used on current nightly didn’t work properly. (I skipped trying to fully understand what it was supposed to do, because this PR uses a completely different approach.) ```rs fn nested() { unsafe { unsafe { unsf() } } } ``` ``` warning: unnecessary `unsafe` block --> src/main.rs:10:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block 10 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block | = note: `#[warn(unused_unsafe)]` on by default ``` vs ```rs fn nested() { let _ = || unsafe { let _ = || unsafe { unsf() }; }; } ``` ``` warning: unnecessary `unsafe` block --> src/main.rs:9:16 | 9 | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block | = note: `#[warn(unused_unsafe)]` on by default warning: unnecessary `unsafe` block --> src/main.rs:10:20 | 10 | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block ``` *note that this warning kind-of suggests that **both** unsafe blocks are redundant* -------------------------------------------------------------------------------- I also dislike the fact that it always suggests keeping the outermost `unsafe`. E.g. for ```rs fn granularity() { unsafe { unsafe { unsf() } unsafe { unsf() } unsafe { unsf() } } } ``` I prefer if `rustc` suggests removing the more-course outer-level `unsafe` instead of the fine-grained inner `unsafe` blocks, which it currently does on nightly: ``` warning: unnecessary `unsafe` block --> src/main.rs:10:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block 10 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block | = note: `#[warn(unused_unsafe)]` on by default warning: unnecessary `unsafe` block --> src/main.rs:11:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block 10 | unsafe { unsf() } 11 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block warning: unnecessary `unsafe` block --> src/main.rs:12:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block ... 12 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block ``` -------------------------------------------------------------------------------- Needless to say, this PR addresses all these points. For context, as far as my understanding goes, the main advantage of skipping inner unsafe blocks was that a test case like ```rs fn top_level_used() { unsafe { unsf(); unsafe { unsf() } unsafe { unsf() } unsafe { unsf() } } } ``` should generate some warning because there’s redundant nested `unsafe`, however every single `unsafe` block _does_ contain some statement that uses it. Of course this PR doesn’t aim change the warnings on this kind of code example, because the current behavior, warning on all the inner `unsafe` blocks, makes sense in this case. As mentioned, during MIR building all the unsafe blocks *are* kept now, and usage is attributed to them. The way to still generate a warning like ``` warning: unnecessary `unsafe` block --> src/main.rs:11:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block 10 | unsf(); 11 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block | = note: `#[warn(unused_unsafe)]` on by default warning: unnecessary `unsafe` block --> src/main.rs:12:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block ... 12 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block warning: unnecessary `unsafe` block --> src/main.rs:13:9 | 9 | unsafe { | ------ because it's nested under this `unsafe` block ... 13 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block ``` in this case is by emitting a `unused_unsafe` warning for all of the `unsafe` blocks that are _within a **used** unsafe block_. The previous code had a little HIR traversal already anyways to collect a set of all the unsafe blocks (in order to afterwards determine which ones are unused afterwards). This PR uses such a traversal to do additional things including logic like _always_ warn for an `unsafe` block that’s inside of another **used** unsafe block. The traversal is expanded to include nested closures in the same go, this simplifies a lot of things. The whole logic around `unsafe_op_in_unsafe_fn` is a little complicated, there’s some test cases of corner-cases in this PR. (The implementation involves differentiating between whether a used unsafe block was used exclusively by operations where `allow(unsafe_op_in_unsafe_fn)` was active.) The main goal was to make sure that code should compile successfully if all the `unused_unsafe`-warnings are addressed _simultaneously_ (by removing the respective `unsafe` blocks) no matter how complicated the patterns of `unsafe_op_in_unsafe_fn` being disallowed and allowed throughout the function are. -------------------------------------------------------------------------------- One noteworthy design decision I took here: An `unsafe` block with `allow(unused_unsafe)` **is considered used** for the purposes of linting about redundant contained unsafe blocks. So while ```rs fn granularity() { unsafe { //~ ERROR: unnecessary `unsafe` block unsafe { unsf() } unsafe { unsf() } unsafe { unsf() } } } ``` warns for the outer `unsafe` block, ```rs fn top_level_ignored() { #[allow(unused_unsafe)] unsafe { #[deny(unused_unsafe)] { unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block } } } ``` warns on the inner ones.
2022-01-12Remove ui tests for LLVM-style inline assemblyTomasz Miąsko-26/+4
2021-12-12Stabilize asm! and global_asm!Amanieu d'Antras-5/+6
They are also removed from the prelude as per the decision in https://github.com/rust-lang/rust/issues/87228. stdarch and compiler-builtins are updated to work with the new, stable asm! and global_asm! macros.
2021-12-11Tweak assoc type obligation spansEsteban Kuber-3/+5
* Point at RHS of associated type in obligation span * Point at `impl` assoc type on projection error * Reduce verbosity of recursive obligations * Point at source of binding lifetime obligation * Tweak "required bound" note * Tweak "expected... found opaque (return) type" labels * Point at set type in impl assoc type WF errors
2021-11-06Stabilize `const_raw_ptr_deref` for `*const T`Jacob Pratt-3/+2
This stabilizes dereferencing immutable raw pointers in const contexts. It does not stabilize `*mut T` dereferencing. This is placed behind the `const_raw_mut_ptr_deref` feature gate.
2021-10-15Bless testsCameron Steffen-4/+4
2021-09-15Move some tests to more reasonable directoriesCaio-0/+98
2021-08-23Auto merge of #83302 - camsteffen:write-piece-unchecked, r=dtolnaybors-2/+17
Get piece unchecked in `write` We already use specialized `zip`, but it seems like we can do a little better by not checking `pieces` length at all. `Arguments` constructors are now unsafe. So the `format_args!` expansion now includes an `unsafe` block. <details> <summary>Local Bench Diff</summary> ```text name before ns/iter after ns/iter diff ns/iter diff % speedup fmt::write_str_macro1 22,967 19,718 -3,249 -14.15% x 1.16 fmt::write_str_macro2 35,527 32,654 -2,873 -8.09% x 1.09 fmt::write_str_macro_debug 571,953 575,973 4,020 0.70% x 0.99 fmt::write_str_ref 9,579 9,459 -120 -1.25% x 1.01 fmt::write_str_value 9,573 9,572 -1 -0.01% x 1.00 fmt::write_u128_max 176 173 -3 -1.70% x 1.02 fmt::write_u128_min 138 134 -4 -2.90% x 1.03 fmt::write_u64_max 139 136 -3 -2.16% x 1.02 fmt::write_u64_min 129 135 6 4.65% x 0.96 fmt::write_vec_macro1 24,401 22,273 -2,128 -8.72% x 1.10 fmt::write_vec_macro2 37,096 35,602 -1,494 -4.03% x 1.04 fmt::write_vec_macro_debug 588,291 589,575 1,284 0.22% x 1.00 fmt::write_vec_ref 9,568 9,732 164 1.71% x 0.98 fmt::write_vec_value 9,516 9,625 109 1.15% x 0.99 ``` </details>
2021-08-17Add needs-asm-support to more testsJosh Stone-4/+5
These were found as test failures on s390x for RHEL and Fedora.
2021-08-16Add unnecessary unsafe testCameron Steffen-2/+17
2021-08-15Fix ui tests for llvm_asm! deprecationAmanieu d'Antras-4/+5
2021-08-03Test dropping union fields moreSmitty-0/+148
2021-07-30Properly find owner of closure in THIR unsafeckLeSeulArtichaut-0/+15
Co-authored-by: FabianWolff <fabian.wolff@alumni.ethz.ch>
2021-07-10Implement Mutation- and BorrowOfLayoutConstrainedField in thir-unsafeckFabian Wolff-11/+359
2021-07-09Check for union field accesses in THIR unsafeckSmitty-0/+246
2021-06-21Fix unused_unsafe with compiler-generated unsafeCameron Steffen-11/+12
2021-05-27Rollup merge of #85564 - ↵Dylan DPC-0/+27
pnkfelix:issue-85435-readd-capture-disjoint-fields-gate, r=nikomatsakis readd capture disjoint fields gate This readds a feature gate guard that was added in PR #83521. (Basically, there were unintended consequences to the code exposed by removing the feature gate guard.) The root bug still remains to be resolved, as discussed in issue #85561. This is just a band-aid suitable for a beta backport. Cc issue #85435 Note that the latter issue is unfixed until we backport this (or another fix) to 1.53 beta
2021-05-25Run THIR unsafeck on `unsafe_op_in_unsafe_fn` testLeSeulArtichaut-16/+141
2021-05-24Apply suggestions from code review Felix S Klock II-0/+2
test THIR unsafeck too Co-authored-by: Léo Lanteri Thauvin <leseulartichaut@gmail.com>
2021-05-24Replace more "NULL" with "null"LeSeulArtichaut-4/+4
2021-05-21Apply suggestions from code review Felix S Klock II-3/+0
(removing confusing comment from my test, since the comment reflects the bad undesirable behavior that is being fixed here.)
2021-05-21Regression test for issue 85435.Felix S. Klock II-0/+28
2021-05-21Check for initialization of layout-restricted typesLeSeulArtichaut-2/+33
2021-05-20Check for raw pointer dereference in THIR unsafeckLeSeulArtichaut-4/+60
2021-05-14Check for inline assembly in THIR unsafeckSmitty-0/+50
2021-05-11Test `-Zthir-unsafeck` for unused unsafe blocksLeSeulArtichaut-2/+19
2021-05-11Test `-Zthir-unsafeck` for unsafe function callsLeSeulArtichaut-3/+45
2021-05-09remove const_fn feature gateRalf Jung-2/+1
2021-05-02Change 'NULL' to 'null'Brent Kerby-6/+6
2021-03-27make unaligned_refereces future-incompat lint warn-by-default, and remove ↵Ralf Jung-127/+0
the safe_packed_borrows lint that it replaces
2021-02-18Stabilize `unsafe_op_in_unsafe_fn` lintLeSeulArtichaut-17/+16
2021-01-23Permit mutable references in all const contextsoli-1/+16
2021-01-19Auto merge of #81110 - LeSeulArtichaut:fix-unused-unsafe-label, r=RalfJungbors-16/+39
Fix `unused_unsafe` label with `unsafe_block_in_unsafe_fn Previously, the following code: ```rust #![feature(unsafe_block_in_unsafe_fn)] unsafe fn foo() { unsafe { unsf() } } unsafe fn unsf() {} ``` Would give the following warning: ``` warning: unnecessary `unsafe` block --> src/lib.rs:4:5 | 4 | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block | = note: `#[warn(unused_unsafe)]` on by default ``` which doesn't point out that the block is in an `unsafe fn`. Tracking issue: #71668 cc #79208
2021-01-17Fix `unused_unsafe` label with `unsafe_block_in_unsafe_fnLeSeulArtichaut-16/+39
2021-01-17Auto merge of #80942 - c410-f3r:tests-tests-tests, r=petrochenkovbors-0/+16
Move some tests to more reasonable directories - 2 All tests with a score equal or greater than 1.0 were moved to their respective directories by issuing ```bash cat FILE | tr -s " " | tr -d '():' | sort -k3 | awk '$3 >= 1' | cut -d " " -f1-2 | sed 's;\\;/;g' | xargs -n2 git mv ``` **Observation**: The first column values is the only column with results greater zero To attest the confidentiality of the model, some manual revision of at least of tests is needed and this process will be tracked in the following list: * `src/test/ui/abi/issue-28676.rs` OK #28676 * `src/test/ui/array-slice-vec/issue-15730.rs` OK * `src/test/ui/associated-types/issue-24338.rs` OK #54823 * `src/test/ui/associated-types/issue-48551.rs` Looks OK #48551 * `src/test/ui/associated-types/issue-50301.rs` Looks OK #63577 ... cc #73494 r? `@petrochenkov`
2021-01-16Move some tests to more reasonable directories - 2Caio-0/+16
Address comments Update limits