From ee3fc9dff8f2cff217883cd4a6f979677a1263f9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 15:47:14 -0400 Subject: [PATCH 01/15] never consider unsafe blocks unused if they would be required with unsafe_op_in_unsafe_fn --- .../rustc_errors/src/diagnostic_builder.rs | 2 +- .../rustc_mir_build/src/check_unsafety.rs | 8 +- .../rustc_mir_transform/src/check_unsafety.rs | 37 +- src/test/ui/span/lint-unused-unsafe-thir.rs | 4 +- .../ui/span/lint-unused-unsafe-thir.stderr | 18 +- .../ui/span/lint-unused-unsafe.mir.stderr | 548 ++---------------- src/test/ui/span/lint-unused-unsafe.rs | 74 +-- ...rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr | 38 +- .../unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs | 2 - ...fc-2585-unsafe_op_in_unsafe_fn.thir.stderr | 24 +- 10 files changed, 124 insertions(+), 631 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs index 9e68ee282e652..04159bff4ff41 100644 --- a/compiler/rustc_errors/src/diagnostic_builder.rs +++ b/compiler/rustc_errors/src/diagnostic_builder.rs @@ -566,7 +566,7 @@ impl Drop for DiagnosticBuilderInner<'_> { ), )); handler.emit_diagnostic(&mut self.diagnostic); - panic!(); + panic!("error was constructed but not emitted"); } } // `.emit()` was previously called, or maybe we're during `.cancel()`. diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index 864caf0ba3197..e3157a4300195 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -75,10 +75,10 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { match self.safety_context { SafetyContext::BuiltinUnsafeBlock => {} SafetyContext::UnsafeBlock { ref mut used, .. } => { - if !self.body_unsafety.is_unsafe() || !unsafe_op_in_unsafe_fn_allowed { - // Mark this block as useful - *used = true; - } + // Mark this block as useful (even inside `unsafe fn`, where it is technically + // redundant -- but we want to eventually enable `unsafe_op_in_unsafe_fn` by + // default which will require those blocks). + *used = true; } SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {} SafetyContext::UnsafeFn => { diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index d564f48016626..9ea87245d034a 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -5,9 +5,9 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::hir_id::HirId; use rustc_hir::intravisit; use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor}; +use rustc_middle::mir::*; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt}; -use rustc_middle::{lint, mir::*}; use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::Level; @@ -259,7 +259,7 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { violations: impl IntoIterator, new_used_unsafe_blocks: impl IntoIterator, ) { - use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn}; + use UsedUnsafeBlockData::*; let update_entry = |this: &mut Self, hir_id, new_usage| { match this.used_unsafe_blocks.entry(hir_id) { @@ -299,15 +299,11 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { } }), Safety::BuiltinUnsafe => {} - Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|violation| { + Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| { update_entry( self, hir_id, - match self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, violation.lint_root).0 - { - Level::Allow => AllAllowedInUnsafeFn(violation.lint_root), - _ => SomeDisallowedInUnsafeFn, - }, + SomeDisallowedInUnsafeFn, ) }), }; @@ -522,6 +518,11 @@ fn unsafety_check_result<'tcx>( } fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { + if matches!(kind, UnusedUnsafe::InUnsafeFn(..)) { + // We do *not* warn here, these unsafe blocks are actually required when + // `unsafe_op_in_unsafe_fn` is warn or higher. + return; + } let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id)); tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| { let msg = "unnecessary `unsafe` block"; @@ -535,25 +536,7 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { "because it's nested under this `unsafe` block", ); } - UnusedUnsafe::InUnsafeFn(id, usage_lint_root) => { - db.span_label( - tcx.sess.source_map().guess_head_span(tcx.hir().span(id)), - "because it's nested under this `unsafe` fn", - ) - .note( - "this `unsafe` block does contain unsafe operations, \ - but those are already allowed in an `unsafe fn`", - ); - let (level, source) = - tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, usage_lint_root); - assert_eq!(level, Level::Allow); - lint::explain_lint_level_source( - UNSAFE_OP_IN_UNSAFE_FN, - Level::Allow, - source, - &mut db, - ); - } + UnusedUnsafe::InUnsafeFn(_id, _usage_lint_root) => unreachable!(), } db.emit(); diff --git a/src/test/ui/span/lint-unused-unsafe-thir.rs b/src/test/ui/span/lint-unused-unsafe-thir.rs index 95a537ed28230..adb72c26bba47 100644 --- a/src/test/ui/span/lint-unused-unsafe-thir.rs +++ b/src/test/ui/span/lint-unused-unsafe-thir.rs @@ -22,7 +22,7 @@ fn bad1() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary `unsafe` block unsafe fn bad3() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad4() { unsafe { callback(||{}) } } //~ ERROR: unnecessary `unsafe` block -unsafe fn bad5() { unsafe { unsf() } } //~ ERROR: unnecessary `unsafe` block +unsafe fn bad5() { unsafe { unsf() } } fn bad6() { unsafe { // don't put the warning here unsafe { //~ ERROR: unnecessary `unsafe` block @@ -31,7 +31,7 @@ fn bad6() { } } unsafe fn bad7() { - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { unsafe { //~ ERROR: unnecessary `unsafe` block unsf() } diff --git a/src/test/ui/span/lint-unused-unsafe-thir.stderr b/src/test/ui/span/lint-unused-unsafe-thir.stderr index 6654910c5cdc3..3bcbb759775aa 100644 --- a/src/test/ui/span/lint-unused-unsafe-thir.stderr +++ b/src/test/ui/span/lint-unused-unsafe-thir.stderr @@ -30,14 +30,6 @@ error: unnecessary `unsafe` block LL | fn bad4() { unsafe { callback(||{}) } } | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe-thir.rs:25:20 - | -LL | unsafe fn bad5() { unsafe { unsf() } } - | ---------------- ^^^^^^ unnecessary `unsafe` block - | | - | because it's nested under this `unsafe` fn - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe-thir.rs:28:9 | @@ -54,13 +46,5 @@ LL | unsafe { LL | unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe-thir.rs:34:5 - | -LL | unsafe fn bad7() { - | ---------------- because it's nested under this `unsafe` fn -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors diff --git a/src/test/ui/span/lint-unused-unsafe.mir.stderr b/src/test/ui/span/lint-unused-unsafe.mir.stderr index 850550a1d8f70..d8412908c7383 100644 --- a/src/test/ui/span/lint-unused-unsafe.mir.stderr +++ b/src/test/ui/span/lint-unused-unsafe.mir.stderr @@ -28,17 +28,6 @@ error: unnecessary `unsafe` block LL | fn bad4() { unsafe { callback(||{}) } } | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:30:20 - | -LL | unsafe fn bad5() { unsafe { unsf() } } - | ---------------- ^^^^^^ unnecessary `unsafe` block - | | - | because it's nested under this `unsafe` fn - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - = note: `#[allow(unsafe_op_in_unsafe_fn)]` on by default - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:32:5 | @@ -51,17 +40,6 @@ error: unnecessary `unsafe` block LL | unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:40:9 - | -LL | unsafe fn bad7() { - | ---------------- because it's nested under this `unsafe` fn -LL | unsafe { -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:74:9 | @@ -272,91 +250,32 @@ error: unnecessary `unsafe` block LL | unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:197:13 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | unsafe { -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:194:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:198:13 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:199:13 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:205:9 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:203:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:207:13 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:208:13 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | unsafe { + | ------ because it's nested under this `unsafe` block ... LL | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:209:13 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | unsafe { + | ------ because it's nested under this `unsafe` block ... LL | unsafe { unsf() } | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:220:17 @@ -398,19 +317,12 @@ LL | unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:254:9 + --> $DIR/lint-unused-unsafe.rs:255:13 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:252:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:268:13 @@ -630,91 +542,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:409:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:406:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:410:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:411:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:417:20 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:415:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:419:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:420:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:421:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:432:28 @@ -756,19 +609,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:466:20 + --> $DIR/lint-unused-unsafe.rs:467:24 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:464:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:480:24 @@ -988,91 +834,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:622:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { let _ = || unsf(); }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:619:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:623:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { let _ = || unsf(); }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:624:24 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { let _ = || unsf(); }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:630:20 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:628:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:632:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsf(); LL | let _ = || unsafe { let _ = || unsf(); }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:633:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { let _ = || unsf(); }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:634:24 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { let _ = || unsf(); }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:645:28 @@ -1114,19 +901,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:679:20 + --> $DIR/lint-unused-unsafe.rs:680:24 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:677:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:693:24 @@ -1256,91 +1036,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:784:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:781:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:785:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:786:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:792:24 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:790:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:794:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:795:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:796:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:807:32 @@ -1382,19 +1103,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:841:24 + --> $DIR/lint-unused-unsafe.rs:842:28 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:839:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:855:28 @@ -1524,91 +1238,32 @@ error: unnecessary `unsafe` block LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:942:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:939:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:943:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:944:28 - | -LL | unsafe fn granularity_2() { - | ------------------------- because it's nested under this `unsafe` fn -... -LL | let _ = || unsafe { unsf() }; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:950:24 - | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:948:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:952:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn -... +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block +LL | unsf(); LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:953:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:954:28 | -LL | unsafe fn top_level_used_2() { - | ---------------------------- because it's nested under this `unsafe` fn +LL | let _ = || unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = || unsafe { unsf() }; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:965:32 @@ -1650,19 +1305,12 @@ LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:999:24 + --> $DIR/lint-unused-unsafe.rs:1000:28 | -LL | unsafe fn granular_disallow_op_in_unsafe_fn_3() { - | ----------------------------------------------- because it's nested under this `unsafe` fn LL | let _ = || unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:997:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ + | ------ because it's nested under this `unsafe` block +LL | let _ = || unsafe { + | ^^^^^^ unnecessary `unsafe` block error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1013:28 @@ -1672,21 +1320,6 @@ LL | let _ = || unsafe { LL | let _ = || unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1044:9 - | -LL | unsafe fn multiple_unsafe_op_in_unsafe_fn_allows() { - | -------------------------------------------------- because it's nested under this `unsafe` fn -LL | unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:1045:21 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1059:29 | @@ -1726,87 +1359,32 @@ error: unnecessary `unsafe` block LL | let _ = async { unsafe { | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1074:33 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { let _ = async { unsf() }; }}; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/lint-unused-unsafe.rs:1071:17 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1075:33 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { let _ = async { unsf() }; }}; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1076:33 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { let _ = async { unsf() }; }}; - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - -error: unnecessary `unsafe` block - --> $DIR/lint-unused-unsafe.rs:1078:29 - | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | let _ = async { unsafe { - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` - error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1080:33 | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn -... +LL | let _ = async { unsafe { + | ------ because it's nested under this `unsafe` block +LL | let _ = async { unsf() }; LL | let _ = async { unsafe { let _ = async { unsf() }; }}; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1081:33 | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn +LL | let _ = async { unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = async { unsafe { let _ = async { unsf() }; }}; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1082:33 | -LL | async unsafe fn async_blocks() { - | ------------------------------ because it's nested under this `unsafe` fn +LL | let _ = async { unsafe { + | ------ because it's nested under this `unsafe` block ... LL | let _ = async { unsafe { let _ = async { unsf() }; }}; | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` error: unnecessary `unsafe` block --> $DIR/lint-unused-unsafe.rs:1092:22 @@ -1820,5 +1398,5 @@ error: unnecessary `unsafe` block LL | let _x: [(); unsafe { unsafe { size() } }] = []; | ^^^^^^ unnecessary `unsafe` block -error: aborting due to 201 previous errors +error: aborting due to 174 previous errors diff --git a/src/test/ui/span/lint-unused-unsafe.rs b/src/test/ui/span/lint-unused-unsafe.rs index f8d1dff3572be..5d042768be002 100644 --- a/src/test/ui/span/lint-unused-unsafe.rs +++ b/src/test/ui/span/lint-unused-unsafe.rs @@ -27,7 +27,7 @@ fn bad1() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary `unsafe` block unsafe fn bad3() { unsafe {} } //~ ERROR: unnecessary `unsafe` block fn bad4() { unsafe { callback(||{}) } } //~ ERROR: unnecessary `unsafe` block -unsafe fn bad5() { unsafe { unsf() } } //~ ERROR: unnecessary `unsafe` block +unsafe fn bad5() { unsafe { unsf() } } fn bad6() { unsafe { //~ ERROR: unnecessary `unsafe` block unsafe { // don't put the warning here @@ -37,7 +37,7 @@ fn bad6() { } unsafe fn bad7() { unsafe { //~ ERROR: unnecessary `unsafe` block - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { unsf() } } @@ -194,15 +194,15 @@ mod additional_tests { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { unsafe { //~ ERROR: unnecessary `unsafe` block - unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block - unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block - unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block + unsafe { unsf() } + unsafe { unsf() } + unsafe { unsf() } } } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { unsf(); unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block unsafe { unsf() } //~ ERROR: unnecessary `unsafe` block @@ -251,8 +251,8 @@ mod additional_tests { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - unsafe { //~ ERROR: unnecessary `unsafe` block - unsafe { + unsafe { + unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -406,15 +406,15 @@ mod additional_tests_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf(); let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block @@ -463,8 +463,8 @@ mod additional_tests_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -619,15 +619,15 @@ mod additional_tests_even_more_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { let _ = || unsf(); }; + let _ = || unsafe { let _ = || unsf(); }; + let _ = || unsafe { let _ = || unsf(); }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { let _ = || unsf(); let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { let _ = || unsf(); }; //~ ERROR: unnecessary `unsafe` block @@ -676,8 +676,8 @@ mod additional_tests_even_more_closures { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { let _ = || unsf(); @@ -781,15 +781,15 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf(); let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block @@ -838,8 +838,8 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -939,15 +939,15 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granularity_2() { let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; + let _ = || unsafe { unsf() }; }; } #[allow(unsafe_op_in_unsafe_fn)] unsafe fn top_level_used_2() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = || unsafe { unsf(); let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block let _ = || unsafe { unsf() }; //~ ERROR: unnecessary `unsafe` block @@ -996,8 +996,8 @@ mod item_likes { #[allow(unsafe_op_in_unsafe_fn)] unsafe fn granular_disallow_op_in_unsafe_fn_3() { - let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = || unsafe { + let _ = || unsafe { + let _ = || unsafe { //~ ERROR: unnecessary `unsafe` block #[deny(unsafe_op_in_unsafe_fn)] { unsf(); @@ -1041,7 +1041,7 @@ mod additional_tests_extra { #[warn(unsafe_op_in_unsafe_fn)] unsafe fn multiple_unsafe_op_in_unsafe_fn_allows() { - unsafe { //~ ERROR: unnecessary `unsafe` block + unsafe { #[allow(unsafe_op_in_unsafe_fn)] { unsf(); @@ -1071,11 +1071,11 @@ mod additional_tests_extra { #[allow(unsafe_op_in_unsafe_fn)] { let _ = async { unsafe { //~ ERROR: unnecessary `unsafe` block - let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block - let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block - let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block + let _ = async { unsafe { let _ = async { unsf() }; }}; + let _ = async { unsafe { let _ = async { unsf() }; }}; + let _ = async { unsafe { let _ = async { unsf() }; }}; }}; - let _ = async { unsafe { //~ ERROR: unnecessary `unsafe` block + let _ = async { unsafe { let _ = async { unsf() }; let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block let _ = async { unsafe { let _ = async { unsf() }; }}; //~ ERROR: unnecessary `unsafe` block diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr index fd58e1b1ebe37..b968174dd2d7e 100644 --- a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr +++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.mir.stderr @@ -81,40 +81,8 @@ error: unnecessary `unsafe` block LL | unsafe { unsafe { unsf() } } | ^^^^^^ unnecessary `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:60:5 - | -LL | unsafe fn allow_level() { - | ----------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:53:9 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:72:9 - | -LL | unsafe fn nested_allow_level() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - | - = note: this `unsafe` block does contain unsafe operations, but those are already allowed in an `unsafe fn` -note: the lint level is defined here - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:65:13 - | -LL | #[allow(unsafe_op_in_unsafe_fn)] - | ^^^^^^^^^^^^^^^^^^^^^^ - error[E0133]: call to unsafe function is unsafe and requires unsafe block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:78:5 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5 | LL | unsf(); | ^^^^^^ call to unsafe function @@ -122,13 +90,13 @@ LL | unsf(); = note: consult the function's documentation for information on how to avoid undefined behavior error[E0133]: call to unsafe function is unsafe and requires unsafe function or block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:83:9 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9 | LL | unsf(); | ^^^^^^ call to unsafe function | = note: consult the function's documentation for information on how to avoid undefined behavior -error: aborting due to 13 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0133`. diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs index 30b072340341b..db1e916a36c1f 100644 --- a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs +++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.rs @@ -58,7 +58,6 @@ unsafe fn allow_level() { VOID = (); unsafe { unsf() } - //~^ ERROR unnecessary `unsafe` block } unsafe fn nested_allow_level() { @@ -70,7 +69,6 @@ unsafe fn nested_allow_level() { VOID = (); unsafe { unsf() } - //~^ ERROR unnecessary `unsafe` block } } diff --git a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr index 2ba6a72930df8..e365293657e30 100644 --- a/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr +++ b/src/test/ui/unsafe/rfc-2585-unsafe_op_in_unsafe_fn.thir.stderr @@ -83,26 +83,8 @@ LL | unsafe { unsafe { unsf() } } | | | because it's nested under this `unsafe` block -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:60:5 - | -LL | unsafe fn allow_level() { - | ----------------------- because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - -error: unnecessary `unsafe` block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:72:9 - | -LL | unsafe fn nested_allow_level() { - | ------------------------------ because it's nested under this `unsafe` fn -... -LL | unsafe { unsf() } - | ^^^^^^ unnecessary `unsafe` block - error[E0133]: call to unsafe function `unsf` is unsafe and requires unsafe block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:78:5 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:76:5 | LL | unsf(); | ^^^^^^ call to unsafe function @@ -110,13 +92,13 @@ LL | unsf(); = note: consult the function's documentation for information on how to avoid undefined behavior error[E0133]: call to unsafe function `unsf` is unsafe and requires unsafe function or block - --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:83:9 + --> $DIR/rfc-2585-unsafe_op_in_unsafe_fn.rs:81:9 | LL | unsf(); | ^^^^^^ call to unsafe function | = note: consult the function's documentation for information on how to avoid undefined behavior -error: aborting due to 13 previous errors +error: aborting due to 11 previous errors For more information about this error, try `rustc --explain E0133`. From 3e44ca95dd844863eeb9bba44209e43d2afb81a4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 17:04:52 -0400 Subject: [PATCH 02/15] remove some unused code and types --- compiler/rustc_middle/src/mir/query.rs | 22 +------ .../rustc_mir_transform/src/check_unsafety.rs | 64 +++++-------------- 2 files changed, 17 insertions(+), 69 deletions(-) diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index dd9f8795f94ff..594c14a642ded 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -2,7 +2,7 @@ use crate::mir::{Body, ConstantKind, Promoted}; use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt}; -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::vec_map::VecMap; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; @@ -115,21 +115,6 @@ pub enum UnusedUnsafe { /// `unsafe` block nested under another (used) `unsafe` block /// > ``… because it's nested under this `unsafe` block`` InUnsafeBlock(hir::HirId), - /// `unsafe` block nested under `unsafe fn` - /// > ``… because it's nested under this `unsafe fn` `` - /// - /// the second HirId here indicates the first usage of the `unsafe` block, - /// which allows retrieval of the LintLevelSource for why that operation would - /// have been permitted without the block - InUnsafeFn(hir::HirId, hir::HirId), -} - -#[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)] -pub enum UsedUnsafeBlockData { - SomeDisallowedInUnsafeFn, - // the HirId here indicates the first usage of the `unsafe` block - // (i.e. the one that's first encountered in the MIR traversal of the unsafety check) - AllAllowedInUnsafeFn(hir::HirId), } #[derive(TyEncodable, TyDecodable, HashStable, Debug)] @@ -138,10 +123,7 @@ pub struct UnsafetyCheckResult { pub violations: Vec, /// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint. - /// - /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether - /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. - pub used_unsafe_blocks: FxHashMap, + pub used_unsafe_blocks: FxHashSet, /// This is `Some` iff the item is not a closure. pub unused_unsafes: Option>, diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 9ea87245d034a..a9eb60c7a15ad 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -1,4 +1,4 @@ -use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LocalDefId}; @@ -11,7 +11,6 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}; use rustc_session::lint::Level; -use std::collections::hash_map; use std::ops::Bound; pub struct UnsafetyChecker<'a, 'tcx> { @@ -26,7 +25,7 @@ pub struct UnsafetyChecker<'a, 'tcx> { /// /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. - used_unsafe_blocks: FxHashMap, + used_unsafe_blocks: FxHashSet, } impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> { @@ -130,10 +129,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> { &AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => { let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } = self.tcx.unsafety_check_result(def_id); - self.register_violations( - violations, - used_unsafe_blocks.iter().map(|(&h, &d)| (h, d)), - ); + self.register_violations(violations, used_unsafe_blocks.iter().copied()); } }, _ => {} @@ -257,22 +253,8 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { fn register_violations<'a>( &mut self, violations: impl IntoIterator, - new_used_unsafe_blocks: impl IntoIterator, + new_used_unsafe_blocks: impl IntoIterator, ) { - use UsedUnsafeBlockData::*; - - let update_entry = |this: &mut Self, hir_id, new_usage| { - match this.used_unsafe_blocks.entry(hir_id) { - hash_map::Entry::Occupied(mut entry) => { - if new_usage == SomeDisallowedInUnsafeFn { - *entry.get_mut() = SomeDisallowedInUnsafeFn; - } - } - hash_map::Entry::Vacant(entry) => { - entry.insert(new_usage); - } - }; - }; let safety = self.body.source_scopes[self.source_info.scope] .local_data .as_ref() @@ -300,17 +282,13 @@ impl<'tcx> UnsafetyChecker<'_, 'tcx> { }), Safety::BuiltinUnsafe => {} Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|_violation| { - update_entry( - self, - hir_id, - SomeDisallowedInUnsafeFn, - ) + self.used_unsafe_blocks.insert(hir_id); }), }; - new_used_unsafe_blocks - .into_iter() - .for_each(|(hir_id, usage_data)| update_entry(self, hir_id, usage_data)); + new_used_unsafe_blocks.into_iter().for_each(|hir_id| { + self.used_unsafe_blocks.insert(hir_id); + }); } fn check_mut_borrowing_layout_constrained_field( &mut self, @@ -407,34 +385,28 @@ enum Context { struct UnusedUnsafeVisitor<'a, 'tcx> { tcx: TyCtxt<'tcx>, - used_unsafe_blocks: &'a FxHashMap, + used_unsafe_blocks: &'a FxHashSet, context: Context, unused_unsafes: &'a mut Vec<(HirId, UnusedUnsafe)>, } impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> { fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) { - use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn}; - if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules { let used = match self.tcx.lint_level_at_node(UNUSED_UNSAFE, block.hir_id) { - (Level::Allow, _) => Some(SomeDisallowedInUnsafeFn), - _ => self.used_unsafe_blocks.get(&block.hir_id).copied(), + (Level::Allow, _) => true, + _ => self.used_unsafe_blocks.contains(&block.hir_id), }; let unused_unsafe = match (self.context, used) { - (_, None) => UnusedUnsafe::Unused, - (Context::Safe, Some(_)) - | (Context::UnsafeFn(_), Some(SomeDisallowedInUnsafeFn)) => { + (_, false) => UnusedUnsafe::Unused, + (Context::Safe, true) | (Context::UnsafeFn(_), true) => { let previous_context = self.context; self.context = Context::UnsafeBlock(block.hir_id); intravisit::walk_block(self, block); self.context = previous_context; return; } - (Context::UnsafeFn(hir_id), Some(AllAllowedInUnsafeFn(lint_root))) => { - UnusedUnsafe::InUnsafeFn(hir_id, lint_root) - } - (Context::UnsafeBlock(hir_id), Some(_)) => UnusedUnsafe::InUnsafeBlock(hir_id), + (Context::UnsafeBlock(hir_id), true) => UnusedUnsafe::InUnsafeBlock(hir_id), }; self.unused_unsafes.push((block.hir_id, unused_unsafe)); } @@ -458,7 +430,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> { fn check_unused_unsafe( tcx: TyCtxt<'_>, def_id: LocalDefId, - used_unsafe_blocks: &FxHashMap, + used_unsafe_blocks: &FxHashSet, ) -> Vec<(HirId, UnusedUnsafe)> { let body_id = tcx.hir().maybe_body_owned_by(def_id); @@ -518,11 +490,6 @@ fn unsafety_check_result<'tcx>( } fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { - if matches!(kind, UnusedUnsafe::InUnsafeFn(..)) { - // We do *not* warn here, these unsafe blocks are actually required when - // `unsafe_op_in_unsafe_fn` is warn or higher. - return; - } let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id)); tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| { let msg = "unnecessary `unsafe` block"; @@ -536,7 +503,6 @@ fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) { "because it's nested under this `unsafe` block", ); } - UnusedUnsafe::InUnsafeFn(_id, _usage_lint_root) => unreachable!(), } db.emit(); From 35a35d86fdca99b22b07ed7d998ce05032e800d4 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 18:55:43 -0400 Subject: [PATCH 03/15] update comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Léo Lanteri Thauvin --- compiler/rustc_mir_transform/src/check_unsafety.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index a9eb60c7a15ad..0f5fd77f7ab16 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -22,9 +22,6 @@ pub struct UnsafetyChecker<'a, 'tcx> { param_env: ty::ParamEnv<'tcx>, /// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint. - /// - /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether - /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`. used_unsafe_blocks: FxHashSet, } From 86e2ca30880a06cb9dbcef1db4a20266e54cf862 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 2 Aug 2022 18:57:22 -0400 Subject: [PATCH 04/15] add link to discussion --- compiler/rustc_mir_build/src/check_unsafety.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/check_unsafety.rs b/compiler/rustc_mir_build/src/check_unsafety.rs index e3157a4300195..f0b0456c4b961 100644 --- a/compiler/rustc_mir_build/src/check_unsafety.rs +++ b/compiler/rustc_mir_build/src/check_unsafety.rs @@ -77,7 +77,8 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> { SafetyContext::UnsafeBlock { ref mut used, .. } => { // Mark this block as useful (even inside `unsafe fn`, where it is technically // redundant -- but we want to eventually enable `unsafe_op_in_unsafe_fn` by - // default which will require those blocks). + // default which will require those blocks: + // https://github.com/rust-lang/rust/issues/71668#issuecomment-1203075594). *used = true; } SafetyContext::UnsafeFn if unsafe_op_in_unsafe_fn_allowed => {} From 3c8563abcf91eda340aa77d238954dd00fa0cb34 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 6 Aug 2022 17:18:59 -0400 Subject: [PATCH 05/15] make NOP dyn casts not require anything about the vtable --- .../rustc_codegen_cranelift/src/unsize.rs | 1 + compiler/rustc_codegen_ssa/src/base.rs | 1 + .../rustc_const_eval/src/interpret/cast.rs | 7 ++++- .../const-eval/ub-wide-ptr.32bit.stderr | 26 +++++++++++++------ .../const-eval/ub-wide-ptr.64bit.stderr | 26 +++++++++++++------ src/test/ui/consts/const-eval/ub-wide-ptr.rs | 6 ++--- 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/unsize.rs b/compiler/rustc_codegen_cranelift/src/unsize.rs index 052ca0a082b3c..dd9d891ddbdee 100644 --- a/compiler/rustc_codegen_cranelift/src/unsize.rs +++ b/compiler/rustc_codegen_cranelift/src/unsize.rs @@ -29,6 +29,7 @@ pub(crate) fn unsized_info<'tcx>( let old_info = old_info.expect("unsized_info: missing old info for trait upcasting coercion"); if data_a.principal_def_id() == data_b.principal_def_id() { + // A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables. return old_info; } diff --git a/compiler/rustc_codegen_ssa/src/base.rs b/compiler/rustc_codegen_ssa/src/base.rs index a840b27097492..4c6be3f910827 100644 --- a/compiler/rustc_codegen_ssa/src/base.rs +++ b/compiler/rustc_codegen_ssa/src/base.rs @@ -151,6 +151,7 @@ pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>( let old_info = old_info.expect("unsized_info: missing old info for trait upcasting coercion"); if data_a.principal_def_id() == data_b.principal_def_id() { + // A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables. return old_info; } diff --git a/compiler/rustc_const_eval/src/interpret/cast.rs b/compiler/rustc_const_eval/src/interpret/cast.rs index c97c31eb9dadf..14eb2a1537b1f 100644 --- a/compiler/rustc_const_eval/src/interpret/cast.rs +++ b/compiler/rustc_const_eval/src/interpret/cast.rs @@ -298,7 +298,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { self.write_immediate(val, dest) } (&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => { - let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?; + let val = self.read_immediate(src)?; + if data_a.principal() == data_b.principal() { + // A NOP cast that doesn't actually change anything, should be allowed even with mismatching vtables. + return self.write_immediate(*val, dest); + } + let (old_data, old_vptr) = val.to_scalar_pair()?; let old_vptr = old_vptr.to_pointer(self)?; let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?; if old_trait != data_a.principal() { diff --git a/src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr b/src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr index 345ead48151df..9431fb33c5329 100644 --- a/src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr +++ b/src/test/ui/consts/const-eval/ub-wide-ptr.32bit.stderr @@ -278,26 +278,36 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, ╾allocN─╼ ╾allocN─╼ │ ╾──╼╾──╼ } -error[E0080]: evaluation of constant value failed - --> $DIR/ub-wide-ptr.rs:147:62 +error[E0080]: it is undefined behavior to use this value + --> $DIR/ub-wide-ptr.rs:147:1 | LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾allocN─╼ 00 00 00 00 │ ╾──╼.... + } -error[E0080]: evaluation of constant value failed - --> $DIR/ub-wide-ptr.rs:150:65 +error[E0080]: it is undefined behavior to use this value + --> $DIR/ub-wide-ptr.rs:149:1 | LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered allocN, but expected a vtable pointer + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 8, align: 4) { + ╾allocN─╼ ╾allocN─╼ │ ╾──╼╾──╼ + } error[E0080]: could not evaluate static initializer - --> $DIR/ub-wide-ptr.rs:157:5 + --> $DIR/ub-wide-ptr.rs:155:5 | LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance) error[E0080]: could not evaluate static initializer - --> $DIR/ub-wide-ptr.rs:161:5 + --> $DIR/ub-wide-ptr.rs:159:5 | LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable diff --git a/src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr b/src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr index 501932cb95c63..15ef703024aff 100644 --- a/src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr +++ b/src/test/ui/consts/const-eval/ub-wide-ptr.64bit.stderr @@ -278,26 +278,36 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, ╾──────allocN───────╼ ╾──────allocN───────╼ │ ╾──────╼╾──────╼ } -error[E0080]: evaluation of constant value failed - --> $DIR/ub-wide-ptr.rs:147:62 +error[E0080]: it is undefined behavior to use this value + --> $DIR/ub-wide-ptr.rs:147:1 | LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾──────allocN───────╼ 00 00 00 00 00 00 00 00 │ ╾──────╼........ + } -error[E0080]: evaluation of constant value failed - --> $DIR/ub-wide-ptr.rs:150:65 +error[E0080]: it is undefined behavior to use this value + --> $DIR/ub-wide-ptr.rs:149:1 | LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered allocN, but expected a vtable pointer + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: 16, align: 8) { + ╾──────allocN───────╼ ╾──────allocN───────╼ │ ╾──────╼╾──────╼ + } error[E0080]: could not evaluate static initializer - --> $DIR/ub-wide-ptr.rs:157:5 + --> $DIR/ub-wide-ptr.rs:155:5 | LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance) error[E0080]: could not evaluate static initializer - --> $DIR/ub-wide-ptr.rs:161:5 + --> $DIR/ub-wide-ptr.rs:159:5 | LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable diff --git a/src/test/ui/consts/const-eval/ub-wide-ptr.rs b/src/test/ui/consts/const-eval/ub-wide-ptr.rs index a0377ab1efd23..2da694a8bc43c 100644 --- a/src/test/ui/consts/const-eval/ub-wide-ptr.rs +++ b/src/test/ui/consts/const-eval/ub-wide-ptr.rs @@ -145,11 +145,9 @@ const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool // # raw trait object const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) }; -//~^ ERROR evaluation of constant value failed -//~| null pointer +//~^ ERROR it is undefined behavior to use this value const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) }; -//~^ ERROR evaluation of constant value failed -//~| does not point to a vtable +//~^ ERROR it is undefined behavior to use this value const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw // Const eval fails for these, so they need to be statics to error. From 4a5c46fb02b9124dc0bd5bafaccbd5a41121437e Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 15 Aug 2022 18:29:43 +0200 Subject: [PATCH 06/15] Manually implement Debug for ImportKind. --- compiler/rustc_resolve/src/imports.rs | 40 ++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index b89273990d8e5..9c2539d08d807 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -31,7 +31,7 @@ use std::{mem, ptr}; type Res = def::Res; /// Contains data for specific kinds of imports. -#[derive(Clone, Debug)] +#[derive(Clone)] pub enum ImportKind<'a> { Single { /// `source` in `use prefix::source as target`. @@ -62,6 +62,44 @@ pub enum ImportKind<'a> { MacroUse, } +/// Manually implement `Debug` for `ImportKind` because the `source/target_bindings` +/// contain `Cell`s which can introduce infinite loops while printing. +impl<'a> std::fmt::Debug for ImportKind<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use ImportKind::*; + match self { + Single { + ref source, + ref target, + ref type_ns_only, + ref nested, + ref additional_ids, + // Ignore the following to avoid an infinite loop while printing. + source_bindings: _, + target_bindings: _, + } => f + .debug_struct("Single") + .field("source", source) + .field("target", target) + .field("type_ns_only", type_ns_only) + .field("nested", nested) + .field("additional_ids", additional_ids) + .finish(), + Glob { ref is_prelude, ref max_vis } => f + .debug_struct("Glob") + .field("is_prelude", is_prelude) + .field("max_vis", max_vis) + .finish(), + ExternCrate { ref source, ref target } => f + .debug_struct("ExternCrate") + .field("source", source) + .field("target", target) + .finish(), + MacroUse => f.debug_struct("MacroUse").finish(), + } + } +} + /// One import. #[derive(Debug, Clone)] pub(crate) struct Import<'a> { From 611221d8aee134eb99749045e05896352550639c Mon Sep 17 00:00:00 2001 From: Camille Gillot Date: Mon, 15 Aug 2022 20:51:32 +0200 Subject: [PATCH 07/15] Update compiler/rustc_resolve/src/imports.rs --- compiler/rustc_resolve/src/imports.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 9c2539d08d807..e0d57ded5bf24 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -84,7 +84,7 @@ impl<'a> std::fmt::Debug for ImportKind<'a> { .field("type_ns_only", type_ns_only) .field("nested", nested) .field("additional_ids", additional_ids) - .finish(), + .finish_non_exhaustive(), Glob { ref is_prelude, ref max_vis } => f .debug_struct("Glob") .field("is_prelude", is_prelude) From fd934c99bcf1a930ef44a27129ef24b323d3c54f Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 21 Jul 2022 22:00:15 +0000 Subject: [PATCH 08/15] Do not allow Drop impl on foreign fundamental types --- .../locales/en-US/typeck.ftl | 4 ++-- .../rustc_typeck/src/coherence/builtin.rs | 8 ++++--- src/test/ui/drop/drop-foreign-fundamental.rs | 23 +++++++++++++++++++ .../ui/drop/drop-foreign-fundamental.stderr | 9 ++++++++ src/test/ui/dropck/drop-on-non-struct.rs | 7 +++--- src/test/ui/dropck/drop-on-non-struct.stderr | 4 ++-- src/test/ui/error-codes/E0117.rs | 2 +- src/test/ui/error-codes/E0117.stderr | 4 ++-- src/test/ui/error-codes/E0120.stderr | 4 ++-- src/test/ui/issues/issue-41974.stderr | 4 ++-- 10 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 src/test/ui/drop/drop-foreign-fundamental.rs create mode 100644 src/test/ui/drop/drop-foreign-fundamental.stderr diff --git a/compiler/rustc_error_messages/locales/en-US/typeck.ftl b/compiler/rustc_error_messages/locales/en-US/typeck.ftl index 494b8f913934f..2a8488cc5487e 100644 --- a/compiler/rustc_error_messages/locales/en-US/typeck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/typeck.ftl @@ -24,8 +24,8 @@ typeck_lifetimes_or_bounds_mismatch_on_trait = .generics_label = lifetimes in impl do not match this {$item_kind} in trait typeck_drop_impl_on_wrong_item = - the `Drop` trait may only be implemented for structs, enums, and unions - .label = must be a struct, enum, or union + the `Drop` trait may only be implemented for local structs, enums, and unions + .label = must be a struct, enum, or union in the current crate typeck_field_already_declared = field `{$field_name}` is already declared diff --git a/compiler/rustc_typeck/src/coherence/builtin.rs b/compiler/rustc_typeck/src/coherence/builtin.rs index 043e21fc1e32b..2467a81638f75 100644 --- a/compiler/rustc_typeck/src/coherence/builtin.rs +++ b/compiler/rustc_typeck/src/coherence/builtin.rs @@ -47,9 +47,11 @@ impl<'tcx> Checker<'tcx> { } fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) { - // Destructors only work on nominal types. - if let ty::Adt(..) | ty::Error(_) = tcx.type_of(impl_did).kind() { - return; + // Destructors only work on local ADT types. + match tcx.type_of(impl_did).kind() { + ty::Adt(def, _) if def.did().is_local() => return, + ty::Error(_) => return, + _ => {} } let sp = match tcx.hir().expect_item(impl_did).kind { diff --git a/src/test/ui/drop/drop-foreign-fundamental.rs b/src/test/ui/drop/drop-foreign-fundamental.rs new file mode 100644 index 0000000000000..c43df40d6c26a --- /dev/null +++ b/src/test/ui/drop/drop-foreign-fundamental.rs @@ -0,0 +1,23 @@ +use std::ops::Deref; +use std::pin::Pin; + +struct Whatever(T); + +impl Deref for Whatever { + type Target = T; + + fn deref(&self) -> &T { + &self.0 + } +} + +struct A; + +impl Drop for Pin> { + //~^ ERROR the `Drop` trait may only be implemented for local structs, enums, and unions + fn drop(&mut self) {} +} + +fn main() { + let x = Pin::new(Whatever(1.0f32)); +} diff --git a/src/test/ui/drop/drop-foreign-fundamental.stderr b/src/test/ui/drop/drop-foreign-fundamental.stderr new file mode 100644 index 0000000000000..fbd1ba0859134 --- /dev/null +++ b/src/test/ui/drop/drop-foreign-fundamental.stderr @@ -0,0 +1,9 @@ +error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions + --> $DIR/drop-foreign-fundamental.rs:16:15 + | +LL | impl Drop for Pin> { + | ^^^^^^^^^^^^^^^^ must be a struct, enum, or union in the current crate + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0120`. diff --git a/src/test/ui/dropck/drop-on-non-struct.rs b/src/test/ui/dropck/drop-on-non-struct.rs index ef5e18126dc68..145eab126c28d 100644 --- a/src/test/ui/dropck/drop-on-non-struct.rs +++ b/src/test/ui/dropck/drop-on-non-struct.rs @@ -1,5 +1,5 @@ impl<'a> Drop for &'a mut isize { - //~^ ERROR the `Drop` trait may only be implemented for structs, enums, and unions + //~^ ERROR the `Drop` trait may only be implemented for local structs, enums, and unions //~^^ ERROR E0117 fn drop(&mut self) { println!("kaboom"); @@ -8,8 +8,7 @@ impl<'a> Drop for &'a mut isize { impl Drop for Nonexistent { //~^ ERROR cannot find type `Nonexistent` - fn drop(&mut self) { } + fn drop(&mut self) {} } -fn main() { -} +fn main() {} diff --git a/src/test/ui/dropck/drop-on-non-struct.stderr b/src/test/ui/dropck/drop-on-non-struct.stderr index e52728f3781f8..e8fbe5e972642 100644 --- a/src/test/ui/dropck/drop-on-non-struct.stderr +++ b/src/test/ui/dropck/drop-on-non-struct.stderr @@ -15,11 +15,11 @@ LL | impl<'a> Drop for &'a mut isize { | = note: define and implement a trait or new type instead -error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions +error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions --> $DIR/drop-on-non-struct.rs:1:19 | LL | impl<'a> Drop for &'a mut isize { - | ^^^^^^^^^^^^^ must be a struct, enum, or union + | ^^^^^^^^^^^^^ must be a struct, enum, or union in the current crate error: aborting due to 3 previous errors diff --git a/src/test/ui/error-codes/E0117.rs b/src/test/ui/error-codes/E0117.rs index 22b48657385c6..406d24e366614 100644 --- a/src/test/ui/error-codes/E0117.rs +++ b/src/test/ui/error-codes/E0117.rs @@ -1,4 +1,4 @@ impl Drop for u32 {} //~ ERROR E0117 -//~| ERROR the `Drop` trait may only be implemented for structs, enums, and unions +//~| ERROR the `Drop` trait may only be implemented for local structs, enums, and unions fn main() {} diff --git a/src/test/ui/error-codes/E0117.stderr b/src/test/ui/error-codes/E0117.stderr index 76d9f5cc0e52a..f144aa9f72c13 100644 --- a/src/test/ui/error-codes/E0117.stderr +++ b/src/test/ui/error-codes/E0117.stderr @@ -9,11 +9,11 @@ LL | impl Drop for u32 {} | = note: define and implement a trait or new type instead -error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions +error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions --> $DIR/E0117.rs:1:15 | LL | impl Drop for u32 {} - | ^^^ must be a struct, enum, or union + | ^^^ must be a struct, enum, or union in the current crate error: aborting due to 2 previous errors diff --git a/src/test/ui/error-codes/E0120.stderr b/src/test/ui/error-codes/E0120.stderr index 6c306455e4254..75778f1f94a16 100644 --- a/src/test/ui/error-codes/E0120.stderr +++ b/src/test/ui/error-codes/E0120.stderr @@ -1,8 +1,8 @@ -error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions +error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions --> $DIR/E0120.rs:3:15 | LL | impl Drop for dyn MyTrait { - | ^^^^^^^^^^^ must be a struct, enum, or union + | ^^^^^^^^^^^ must be a struct, enum, or union in the current crate error: aborting due to previous error diff --git a/src/test/ui/issues/issue-41974.stderr b/src/test/ui/issues/issue-41974.stderr index fcbb4014025f1..e249db9df5324 100644 --- a/src/test/ui/issues/issue-41974.stderr +++ b/src/test/ui/issues/issue-41974.stderr @@ -7,11 +7,11 @@ LL | impl Drop for T where T: A { = note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local = note: only traits defined in the current crate can be implemented for a type parameter -error[E0120]: the `Drop` trait may only be implemented for structs, enums, and unions +error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions --> $DIR/issue-41974.rs:7:18 | LL | impl Drop for T where T: A { - | ^ must be a struct, enum, or union + | ^ must be a struct, enum, or union in the current crate error: aborting due to 2 previous errors From 7b457184849d343d50a3eb56c28396189c696a42 Mon Sep 17 00:00:00 2001 From: ouz-a Date: Sat, 13 Aug 2022 18:13:20 +0300 Subject: [PATCH 09/15] pass when where clause found --- compiler/rustc_typeck/src/check/writeback.rs | 26 ++++++++- src/test/mir-opt/issue-91633.rs | 31 ++++++++++ .../mir-opt/issue_91633.bar.mir_map.0.mir | 39 +++++++++++++ .../mir-opt/issue_91633.foo.mir_map.0.mir | 57 +++++++++++++++++++ .../mir-opt/issue_91633.fun.mir_map.0.mir | 35 ++++++++++++ .../mir-opt/issue_91633.hey.mir_map.0.mir | 35 ++++++++++++ src/test/ui/typeck/issue-91633.rs | 8 +++ 7 files changed, 229 insertions(+), 2 deletions(-) create mode 100644 src/test/mir-opt/issue-91633.rs create mode 100644 src/test/mir-opt/issue_91633.bar.mir_map.0.mir create mode 100644 src/test/mir-opt/issue_91633.foo.mir_map.0.mir create mode 100644 src/test/mir-opt/issue_91633.fun.mir_map.0.mir create mode 100644 src/test/mir-opt/issue_91633.hey.mir_map.0.mir create mode 100644 src/test/ui/typeck/issue-91633.rs diff --git a/compiler/rustc_typeck/src/check/writeback.rs b/compiler/rustc_typeck/src/check/writeback.rs index f549807c39c91..adcf8ff946d51 100644 --- a/compiler/rustc_typeck/src/check/writeback.rs +++ b/compiler/rustc_typeck/src/check/writeback.rs @@ -3,7 +3,6 @@ // substitutions. use crate::check::FnCtxt; - use hir::def_id::LocalDefId; use rustc_data_structures::fx::FxHashMap; use rustc_errors::ErrorGuaranteed; @@ -16,6 +15,7 @@ use rustc_middle::mir::FakeReadCause; use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast}; use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable}; use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable}; +use rustc_middle::ty::TypeckResults; use rustc_middle::ty::{self, ClosureSizeProfileData, Ty, TyCtxt}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -192,6 +192,27 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { } } + // (ouz-a 1005988): Normally `[T] : std::ops::Index` should be normalized + // into [T] but currently `Where` clause stops the normalization process for it, + // here we compare types of expr and base in a code without `Where` clause they would be equal + // if they are not we don't modify the expr, hence we bypass the ICE + fn is_builtin_index( + &mut self, + typeck_results: &TypeckResults<'tcx>, + e: &hir::Expr<'_>, + base_ty: Ty<'tcx>, + index_ty: Ty<'tcx>, + ) -> bool { + if let Some(elem_ty) = base_ty.builtin_index() { + let Some(exp_ty) = typeck_results.expr_ty_opt(e) else {return false;}; + let resolved_exp_ty = self.resolve(exp_ty, &e.span); + + elem_ty == resolved_exp_ty && index_ty == self.fcx.tcx.types.usize + } else { + false + } + } + // Similar to operators, indexing is always assumed to be overloaded // Here, correct cases where an indexing expression can be simplified // to use builtin indexing because the index type is known to be @@ -222,8 +243,9 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> { ) }); let index_ty = self.fcx.resolve_vars_if_possible(index_ty); + let resolved_base_ty = self.resolve(*base_ty, &base.span); - if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize { + if self.is_builtin_index(&typeck_results, e, resolved_base_ty, index_ty) { // Remove the method call record typeck_results.type_dependent_defs_mut().remove(e.hir_id); typeck_results.node_substs_mut().remove(e.hir_id); diff --git a/src/test/mir-opt/issue-91633.rs b/src/test/mir-opt/issue-91633.rs new file mode 100644 index 0000000000000..8f66019857fbb --- /dev/null +++ b/src/test/mir-opt/issue-91633.rs @@ -0,0 +1,31 @@ +// compile-flags: -Z mir-opt-level=0 +// EMIT_MIR issue_91633.hey.mir_map.0.mir +fn hey (it: &[T]) + where + [T] : std::ops::Index, + { + let _ = &it[0]; + } + +// EMIT_MIR issue_91633.bar.mir_map.0.mir +fn bar (it: Box<[T]>) + where + [T] : std::ops::Index, + { + let _ = it[0]; + } + +// EMIT_MIR issue_91633.fun.mir_map.0.mir +fn fun (it: &[T]) -> &T + { + let f = &it[0]; + f + } + +// EMIT_MIR issue_91633.foo.mir_map.0.mir +fn foo (it: Box<[T]>) -> T + { + let f = it[0].clone(); + f + } + fn main(){} diff --git a/src/test/mir-opt/issue_91633.bar.mir_map.0.mir b/src/test/mir-opt/issue_91633.bar.mir_map.0.mir new file mode 100644 index 0000000000000..f5092d2ac9238 --- /dev/null +++ b/src/test/mir-opt/issue_91633.bar.mir_map.0.mir @@ -0,0 +1,39 @@ +// MIR for `bar` 0 mir_map + +fn bar(_1: Box<[T]>) -> () { + debug it => _1; // in scope 0 at $DIR/issue-91633.rs:+0:12: +0:14 + let mut _0: (); // return place in scope 0 at $DIR/issue-91633.rs:+1:2: +1:2 + let mut _2: &<[T] as std::ops::Index>::Output; // in scope 0 at $DIR/issue-91633.rs:+4:14: +4:19 + let mut _3: &[T]; // in scope 0 at $DIR/issue-91633.rs:+4:14: +4:16 + scope 1 { + } + + bb0: { + StorageLive(_2); // scope 0 at $DIR/issue-91633.rs:+4:14: +4:19 + StorageLive(_3); // scope 0 at $DIR/issue-91633.rs:+4:14: +4:16 + _3 = &(*_1); // scope 0 at $DIR/issue-91633.rs:+4:14: +4:16 + _2 = <[T] as Index>::index(move _3, const 0_usize) -> [return: bb1, unwind: bb3]; // scope 0 at $DIR/issue-91633.rs:+4:14: +4:19 + // mir::Constant + // + span: $DIR/issue-91633.rs:15:14: 15:19 + // + literal: Const { ty: for<'r> fn(&'r [T], usize) -> &'r <[T] as Index>::Output {<[T] as Index>::index}, val: Value() } + } + + bb1: { + StorageDead(_3); // scope 0 at $DIR/issue-91633.rs:+4:18: +4:19 + StorageDead(_2); // scope 0 at $DIR/issue-91633.rs:+4:19: +4:20 + _0 = const (); // scope 0 at $DIR/issue-91633.rs:+3:2: +5:3 + drop(_1) -> [return: bb2, unwind: bb4]; // scope 0 at $DIR/issue-91633.rs:+5:2: +5:3 + } + + bb2: { + return; // scope 0 at $DIR/issue-91633.rs:+5:3: +5:3 + } + + bb3 (cleanup): { + drop(_1) -> bb4; // scope 0 at $DIR/issue-91633.rs:+5:2: +5:3 + } + + bb4 (cleanup): { + resume; // scope 0 at $DIR/issue-91633.rs:+0:1: +5:3 + } +} diff --git a/src/test/mir-opt/issue_91633.foo.mir_map.0.mir b/src/test/mir-opt/issue_91633.foo.mir_map.0.mir new file mode 100644 index 0000000000000..2e8b0feedd3f9 --- /dev/null +++ b/src/test/mir-opt/issue_91633.foo.mir_map.0.mir @@ -0,0 +1,57 @@ +// MIR for `foo` 0 mir_map + +fn foo(_1: Box<[T]>) -> T { + debug it => _1; // in scope 0 at $DIR/issue-91633.rs:+0:19: +0:21 + let mut _0: T; // return place in scope 0 at $DIR/issue-91633.rs:+0:36: +0:37 + let _2: T; // in scope 0 at $DIR/issue-91633.rs:+2:10: +2:11 + let mut _3: &T; // in scope 0 at $DIR/issue-91633.rs:+2:14: +2:27 + let _4: usize; // in scope 0 at $DIR/issue-91633.rs:+2:17: +2:18 + let mut _5: usize; // in scope 0 at $DIR/issue-91633.rs:+2:14: +2:19 + let mut _6: bool; // in scope 0 at $DIR/issue-91633.rs:+2:14: +2:19 + scope 1 { + debug f => _2; // in scope 1 at $DIR/issue-91633.rs:+2:10: +2:11 + } + + bb0: { + StorageLive(_2); // scope 0 at $DIR/issue-91633.rs:+2:10: +2:11 + StorageLive(_3); // scope 0 at $DIR/issue-91633.rs:+2:14: +2:27 + StorageLive(_4); // scope 0 at $DIR/issue-91633.rs:+2:17: +2:18 + _4 = const 0_usize; // scope 0 at $DIR/issue-91633.rs:+2:17: +2:18 + _5 = Len((*_1)); // scope 0 at $DIR/issue-91633.rs:+2:14: +2:19 + _6 = Lt(_4, _5); // scope 0 at $DIR/issue-91633.rs:+2:14: +2:19 + assert(move _6, "index out of bounds: the length is {} but the index is {}", move _5, _4) -> [success: bb1, unwind: bb5]; // scope 0 at $DIR/issue-91633.rs:+2:14: +2:19 + } + + bb1: { + _3 = &(*_1)[_4]; // scope 0 at $DIR/issue-91633.rs:+2:14: +2:27 + _2 = ::clone(move _3) -> [return: bb2, unwind: bb5]; // scope 0 at $DIR/issue-91633.rs:+2:14: +2:27 + // mir::Constant + // + span: $DIR/issue-91633.rs:28:20: 28:25 + // + literal: Const { ty: for<'r> fn(&'r T) -> T {::clone}, val: Value() } + } + + bb2: { + StorageDead(_3); // scope 0 at $DIR/issue-91633.rs:+2:26: +2:27 + FakeRead(ForLet(None), _2); // scope 0 at $DIR/issue-91633.rs:+2:10: +2:11 + StorageDead(_4); // scope 0 at $DIR/issue-91633.rs:+2:27: +2:28 + _0 = move _2; // scope 1 at $DIR/issue-91633.rs:+3:6: +3:7 + drop(_2) -> [return: bb3, unwind: bb5]; // scope 0 at $DIR/issue-91633.rs:+4:2: +4:3 + } + + bb3: { + StorageDead(_2); // scope 0 at $DIR/issue-91633.rs:+4:2: +4:3 + drop(_1) -> [return: bb4, unwind: bb6]; // scope 0 at $DIR/issue-91633.rs:+4:2: +4:3 + } + + bb4: { + return; // scope 0 at $DIR/issue-91633.rs:+4:3: +4:3 + } + + bb5 (cleanup): { + drop(_1) -> bb6; // scope 0 at $DIR/issue-91633.rs:+4:2: +4:3 + } + + bb6 (cleanup): { + resume; // scope 0 at $DIR/issue-91633.rs:+0:1: +4:3 + } +} diff --git a/src/test/mir-opt/issue_91633.fun.mir_map.0.mir b/src/test/mir-opt/issue_91633.fun.mir_map.0.mir new file mode 100644 index 0000000000000..ded9a4cf7e3f9 --- /dev/null +++ b/src/test/mir-opt/issue_91633.fun.mir_map.0.mir @@ -0,0 +1,35 @@ +// MIR for `fun` 0 mir_map + +fn fun(_1: &[T]) -> &T { + debug it => _1; // in scope 0 at $DIR/issue-91633.rs:+0:12: +0:14 + let mut _0: &T; // return place in scope 0 at $DIR/issue-91633.rs:+0:25: +0:27 + let _2: &T; // in scope 0 at $DIR/issue-91633.rs:+2:10: +2:11 + let _3: usize; // in scope 0 at $DIR/issue-91633.rs:+2:18: +2:19 + let mut _4: usize; // in scope 0 at $DIR/issue-91633.rs:+2:15: +2:20 + let mut _5: bool; // in scope 0 at $DIR/issue-91633.rs:+2:15: +2:20 + scope 1 { + debug f => _2; // in scope 1 at $DIR/issue-91633.rs:+2:10: +2:11 + } + + bb0: { + StorageLive(_2); // scope 0 at $DIR/issue-91633.rs:+2:10: +2:11 + StorageLive(_3); // scope 0 at $DIR/issue-91633.rs:+2:18: +2:19 + _3 = const 0_usize; // scope 0 at $DIR/issue-91633.rs:+2:18: +2:19 + _4 = Len((*_1)); // scope 0 at $DIR/issue-91633.rs:+2:15: +2:20 + _5 = Lt(_3, _4); // scope 0 at $DIR/issue-91633.rs:+2:15: +2:20 + assert(move _5, "index out of bounds: the length is {} but the index is {}", move _4, _3) -> [success: bb1, unwind: bb2]; // scope 0 at $DIR/issue-91633.rs:+2:15: +2:20 + } + + bb1: { + _2 = &(*_1)[_3]; // scope 0 at $DIR/issue-91633.rs:+2:14: +2:20 + FakeRead(ForLet(None), _2); // scope 0 at $DIR/issue-91633.rs:+2:10: +2:11 + _0 = &(*_2); // scope 1 at $DIR/issue-91633.rs:+3:6: +3:7 + StorageDead(_3); // scope 0 at $DIR/issue-91633.rs:+4:2: +4:3 + StorageDead(_2); // scope 0 at $DIR/issue-91633.rs:+4:2: +4:3 + return; // scope 0 at $DIR/issue-91633.rs:+4:3: +4:3 + } + + bb2 (cleanup): { + resume; // scope 0 at $DIR/issue-91633.rs:+0:1: +4:3 + } +} diff --git a/src/test/mir-opt/issue_91633.hey.mir_map.0.mir b/src/test/mir-opt/issue_91633.hey.mir_map.0.mir new file mode 100644 index 0000000000000..74f4a5a976116 --- /dev/null +++ b/src/test/mir-opt/issue_91633.hey.mir_map.0.mir @@ -0,0 +1,35 @@ +// MIR for `hey` 0 mir_map + +fn hey(_1: &[T]) -> () { + debug it => _1; // in scope 0 at $DIR/issue-91633.rs:+0:12: +0:14 + let mut _0: (); // return place in scope 0 at $DIR/issue-91633.rs:+1:2: +1:2 + let mut _2: &<[T] as std::ops::Index>::Output; // in scope 0 at $DIR/issue-91633.rs:+4:14: +4:20 + let _3: &<[T] as std::ops::Index>::Output; // in scope 0 at $DIR/issue-91633.rs:+4:15: +4:20 + let mut _4: &[T]; // in scope 0 at $DIR/issue-91633.rs:+4:15: +4:17 + scope 1 { + } + + bb0: { + StorageLive(_2); // scope 0 at $DIR/issue-91633.rs:+4:14: +4:20 + StorageLive(_3); // scope 0 at $DIR/issue-91633.rs:+4:15: +4:20 + StorageLive(_4); // scope 0 at $DIR/issue-91633.rs:+4:15: +4:17 + _4 = &(*_1); // scope 0 at $DIR/issue-91633.rs:+4:15: +4:17 + _3 = <[T] as Index>::index(move _4, const 0_usize) -> [return: bb1, unwind: bb2]; // scope 0 at $DIR/issue-91633.rs:+4:15: +4:20 + // mir::Constant + // + span: $DIR/issue-91633.rs:7:15: 7:20 + // + literal: Const { ty: for<'r> fn(&'r [T], usize) -> &'r <[T] as Index>::Output {<[T] as Index>::index}, val: Value() } + } + + bb1: { + StorageDead(_4); // scope 0 at $DIR/issue-91633.rs:+4:19: +4:20 + _2 = &(*_3); // scope 0 at $DIR/issue-91633.rs:+4:14: +4:20 + StorageDead(_2); // scope 0 at $DIR/issue-91633.rs:+4:20: +4:21 + _0 = const (); // scope 0 at $DIR/issue-91633.rs:+3:2: +5:3 + StorageDead(_3); // scope 0 at $DIR/issue-91633.rs:+5:2: +5:3 + return; // scope 0 at $DIR/issue-91633.rs:+5:3: +5:3 + } + + bb2 (cleanup): { + resume; // scope 0 at $DIR/issue-91633.rs:+0:1: +5:3 + } +} diff --git a/src/test/ui/typeck/issue-91633.rs b/src/test/ui/typeck/issue-91633.rs new file mode 100644 index 0000000000000..331a798dd7a36 --- /dev/null +++ b/src/test/ui/typeck/issue-91633.rs @@ -0,0 +1,8 @@ +// check-pass +fn f (it: &[T]) +where + [T] : std::ops::Index, +{ + let _ = &it[0]; +} +fn main(){} From 86645c9cf7e7a73e01c006c8bc39c431cae0e8df Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 14 Aug 2022 11:28:28 +0200 Subject: [PATCH 10/15] Ignore substs when checking inlining history. --- compiler/rustc_mir_transform/src/inline.rs | 13 +++++++--- .../mir-opt/inline/polymorphic-recursion.rs | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/test/mir-opt/inline/polymorphic-recursion.rs diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 1e46b0a0e8164..6704d3462f48e 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -10,6 +10,7 @@ use rustc_middle::mir::*; use rustc_middle::ty::subst::Subst; use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt}; use rustc_session::config::OptLevel; +use rustc_span::def_id::DefId; use rustc_span::{hygiene::ExpnKind, ExpnData, LocalExpnId, Span}; use rustc_target::spec::abi::Abi; @@ -103,8 +104,12 @@ struct Inliner<'tcx> { param_env: ParamEnv<'tcx>, /// Caller codegen attributes. codegen_fn_attrs: &'tcx CodegenFnAttrs, - /// Stack of inlined Instances. - history: Vec>, + /// Stack of inlined instances. + /// We only check the `DefId` and not the substs because we want to + /// avoid inlining cases of polymorphic recursion. + /// The number of `DefId`s is finite, so checking history is enough + /// to ensure that we do not loop endlessly while inlining. + history: Vec, /// Indicates that the caller body has been modified. changed: bool, } @@ -132,7 +137,7 @@ impl<'tcx> Inliner<'tcx> { Ok(new_blocks) => { debug!("inlined {}", callsite.callee); self.changed = true; - self.history.push(callsite.callee); + self.history.push(callsite.callee.def_id()); self.process_blocks(caller_body, new_blocks); self.history.pop(); } @@ -308,7 +313,7 @@ impl<'tcx> Inliner<'tcx> { return None; } - if self.history.contains(&callee) { + if self.history.contains(&callee.def_id()) { return None; } diff --git a/src/test/mir-opt/inline/polymorphic-recursion.rs b/src/test/mir-opt/inline/polymorphic-recursion.rs new file mode 100644 index 0000000000000..7388722b77633 --- /dev/null +++ b/src/test/mir-opt/inline/polymorphic-recursion.rs @@ -0,0 +1,25 @@ +// Make sure that the MIR inliner does not loop indefinitely on polymorphic recursion. +// compile-flags: --crate-type lib + +// Randomize `def_path_hash` by defining them under a module with different names +macro_rules! emit { + ($($m:ident)*) => {$( + pub mod $m { + pub trait Tr { type Next: Tr; } + + pub fn hoge() { + inner::(); + } + + #[inline(always)] + fn inner() + { + inner::(); + inner::(); + } + } + )*}; +} + +// Increase the chance of triggering the bug +emit!(m00 m01 m02 m03 m04 m05 m06 m07 m08 m09 m10 m11 m12 m13 m14 m15 m16 m17 m18 m19); From 2d968a142fb3ab2a69fa79b1e3b7af36f497a75b Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 13 Aug 2022 14:00:23 +0200 Subject: [PATCH 11/15] Simplify rustdoc themes by relying more on CSS variables --- src/librustdoc/html/static/css/rustdoc.css | 61 +++++++---------- src/librustdoc/html/static/css/themes/ayu.css | 65 +----------------- .../html/static/css/themes/dark.css | 65 +----------------- .../html/static/css/themes/light.css | 67 +------------------ 4 files changed, 30 insertions(+), 228 deletions(-) diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 710ca3ee7c7e7..ed27df8898afa 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -693,8 +693,13 @@ h2.location a { flex-grow: 1; margin: 0px; padding: 0px; + /* We use overflow-wrap: break-word for Safari, which doesn't recognize + `anywhere`: https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap */ overflow-wrap: break-word; + /* Then override it with `anywhere`, which is required to make non-Safari browsers break + more aggressively when we want them to. */ overflow-wrap: anywhere; + background-color: var(--main-background-color); } .in-band > code, .in-band > .code-header { @@ -731,13 +736,13 @@ pre, .rustdoc.source .example-wrap { .docblock table td { padding: .5em; - border: 1px dashed; + border: 1px dashed var(--border-color); } .docblock table th { padding: .5em; text-align: left; - border: 1px solid; + border: 1px solid var(--border-color); } .fields + table { @@ -840,11 +845,11 @@ nav.main { text-align: center; } nav.main .current { - border-top: 1px solid; - border-bottom: 1px solid; + border-top: 1px solid var(--border-color); + border-bottom: 1px solid var(--border-color); } nav.main .separator { - border: 1px solid; + border: 1px solid var(--border-color); display: inline-block; height: 23px; margin: 0 20px; @@ -974,7 +979,7 @@ table, max-width: 100%; /* contents can overflow because of max-width limit, then show ellipsis */ text-overflow: ellipsis; - border: 1px solid; + border: 1px solid var(--border-color); border-radius: 4px; outline: none; cursor: pointer; @@ -1027,11 +1032,12 @@ so that we can apply CSS-filters to change the arrow color in themes */ -moz-box-sizing: border-box !important; box-sizing: border-box !important; outline: none; - border: 1px solid; + border: 1px solid var(--border-color); border-radius: 2px; padding: 8px; font-size: 1rem; width: 100%; + background-color: var(--button-background-color); } .search-results { @@ -1087,7 +1093,7 @@ so that we can apply CSS-filters to change the arrow color in themes */ display: block; margin-top: 7px; border-radius: 3px; - border: 1px solid; + border: 1px solid var(--border-color); font-size: 1rem; } @@ -1096,7 +1102,7 @@ so that we can apply CSS-filters to change the arrow color in themes */ content: ''; position: absolute; right: 11px; - border: solid; + border: solid var(--border-color); border-width: 1px 1px 0 0; display: inline-block; padding: 4px; @@ -1132,13 +1138,13 @@ so that we can apply CSS-filters to change the arrow color in themes */ text-align: center; display: block; margin: 10px 0; - border-bottom: 1px solid; + border-bottom: 1px solid var(--border-color); padding-bottom: 4px; margin-bottom: 6px; } #help-button span.bottom { clear: both; - border-top: 1px solid; + border-top: 1px solid var(--border-color); } .side-by-side { text-align: initial; @@ -1331,6 +1337,7 @@ h3.variant { border-radius: 6px; margin-left: 5px; font-size: 1rem; + border: 1px solid var(--border-color); } .tooltip.ignore::after { @@ -1496,7 +1503,7 @@ pre.rust { #source-sidebar > .title { font-size: 1.5rem; text-align: center; - border-bottom: 1px solid; + border-bottom: 1px solid var(--border-color); margin-bottom: 6px; } #sidebar-toggle > button { @@ -1524,11 +1531,12 @@ pre.rust { #copy-path { height: 34px; + background-color: var(--main-background-color); } #settings-menu > a, #help-button > button, #copy-path { padding: 5px; width: 33px; - border: 1px solid; + border: 1px solid var(--border-color); border-radius: 2px; cursor: pointer; } @@ -1539,6 +1547,7 @@ pre.rust { padding: 5px; height: 100%; display: block; + background-color: var(--button-background-color); } @keyframes rotating { @@ -1588,37 +1597,13 @@ input:checked + .slider { border: 0; } -#theme-choices { - display: none; - position: absolute; - left: 0; - top: 28px; - border: 1px solid; - border-radius: 3px; - z-index: 1; - cursor: pointer; -} - -#theme-choices > button { - border: none; - width: 100%; - padding: 4px 8px; - text-align: center; - background: rgba(0,0,0,0); - overflow-wrap: normal; -} - -#theme-choices > button:not(:first-child) { - border-top: 1px solid; -} - kbd { display: inline-block; padding: 3px 5px; font: 15px monospace; line-height: 10px; vertical-align: middle; - border: solid 1px; + border: solid 1px var(--border-color); border-radius: 3px; cursor: default; } diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css index 4dfb64abbebe8..e7ccd402dd024 100644 --- a/src/librustdoc/html/static/css/themes/ayu.css +++ b/src/librustdoc/html/static/css/themes/ayu.css @@ -14,6 +14,8 @@ Original by Dempfi (https://github.com/dempfi/ayu) --scrollbar-thumb-background-color: #5c6773; --scrollbar-color: #5c6773 #24292f; --headings-border-bottom-color: #5c6773; + --border-color: #5c6773; + --button-background-color: #141920; } .slider { @@ -36,10 +38,6 @@ h4 { border: none; } -.in-band { - background-color: #0f1419; -} - .docblock code { color: #ffb454; } @@ -84,10 +82,6 @@ pre, .rustdoc.source .example-wrap { border-right: 1px solid #ffb44c; } -.docblock table td, .docblock table th { - border-color: #5c6773; -} - .search-results a:hover { background-color: #777; } @@ -151,13 +145,6 @@ pre, .rustdoc.source .example-wrap { pre.rust .comment { color: #788797; } pre.rust .doccomment { color: #a1ac88; } -nav.main .current { - border-top-color: #5c6773; - border-bottom-color: #5c6773; -} -nav.main .separator { - border: 1px solid #5c6773; -} a { color: #39AFD7; } @@ -182,17 +169,6 @@ details.rustdoc-toggle > summary::before { filter: invert(100%); } -.search-input { - background-color: #141920; - border-color: #424c57; -} - -#crate-search { - /* Without the `!important`, the border-color is ignored for ``... - It cannot be in the group above because `.search-input` has a different border color on - hover. */ - border-color: #d2d2d2 !important; -} #crate-search-div::after { /* match border-color; uses https://codepen.io/sosuke/pen/Pjoqqp */ filter: invert(94%) sepia(0%) saturate(721%) hue-rotate(255deg) brightness(90%) contrast(90%); @@ -175,10 +154,6 @@ details.rustdoc-toggle > summary::before { filter: invert(69%) sepia(60%) saturate(6613%) hue-rotate(184deg) brightness(100%) contrast(91%); } -.search-input { - border-color: #e0e0e0; -} - .search-input:focus { border-color: #008dfd; } @@ -296,11 +271,6 @@ pre.ignore:hover, .information:hover + pre.ignore { .notable-traits-tooltiptext { background-color: #111; - border-color: #777; -} - -.notable-traits-tooltiptext .notable { - border-bottom-color: #d2d2d2; } #titles > button:not(.selected) { @@ -317,23 +287,13 @@ pre.ignore:hover, .information:hover + pre.ignore { color: #888; } -@media (max-width: 700px) { - .sidebar-elems { - border-right-color: #000; - } -} - kbd { color: #000; background-color: #fafbfc; - border-color: #d1d5da; - border-bottom-color: #c6cbd1; box-shadow: inset 0 -1px 0 #c6cbd1; } #settings-menu > a, #help-button > button { - border-color: #e0e0e0; - background: #f0f0f0; color: #000; } @@ -342,11 +302,6 @@ kbd { border-color: #ffb900; } -.popover, .popover::before, -#help-button span.top, #help-button span.bottom { - border-color: #d2d2d2; -} - #copy-path { color: #999; } @@ -357,19 +312,6 @@ kbd { filter: invert(65%); } -#theme-choices { - border-color: #e0e0e0; - background-color: #353535; -} - -#theme-choices > button:not(:first-child) { - border-top-color: #e0e0e0; -} - -#theme-choices > button:hover, #theme-choices > button:focus { - background-color: #4e4e4e; -} - .search-results .result-name span.alias { color: #fff; } @@ -377,9 +319,6 @@ kbd { color: #ccc; } -#source-sidebar > .title { - border-bottom-color: #ccc; -} #source-sidebar div.files > a:hover, details.dir-entry summary:hover, #source-sidebar div.files > a:focus, details.dir-entry summary:focus { background-color: #444; diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css index 5698088c790bb..7139c199729ae 100644 --- a/src/librustdoc/html/static/css/themes/light.css +++ b/src/librustdoc/html/static/css/themes/light.css @@ -9,6 +9,8 @@ --scrollbar-thumb-background-color: rgba(36, 37, 39, 0.6); --scrollbar-color: rgba(36, 37, 39, 0.6) #d9d9d9; --headings-border-bottom-color: #ddd; + --border-color: #e0e0e0; + --button-background-color: #fff; } .slider { @@ -21,10 +23,6 @@ input:focus + .slider { box-shadow: 0 0 0 2px #0a84ff, 0 0 0 6px rgba(10, 132, 255, 0.3); } -.in-band { - background-color: white; -} - .rust-logo { /* This rule exists to force other themes to explicitly style the logo. * Rustdoc has a custom linter for this purpose. @@ -41,10 +39,6 @@ input:focus + .slider { background-color: #FDFFD3 !important; } -.docblock table td, .docblock table th { - border-color: #ddd; -} - .search-results a:hover { background-color: #ddd; } @@ -123,14 +117,6 @@ a.result-keyword:focus { background-color: #afc6e4; } .sidebar a.current.tymethod { color: #a67736; } .sidebar a.current.keyword { color: #356da4; } -nav.main .current { - border-top-color: #000; - border-bottom-color: #000; -} -nav.main .separator { - border: 1px solid #000; -} - a { color: #3873AD; } @@ -144,16 +130,6 @@ details.rustdoc-toggle > summary::before { color: #999; } -.search-input { - background-color: white; - border-color: #e0e0e0; -} -#crate-search { - /* Without the `!important`, the border-color is ignored for `