Skip to content

Commit c308726

Browse files
committed
Auto merge of #119552 - krtab:dead_code_priv_mod_pub_field, r=cjgillot,saethlin
Replace visibility test with reachability test in dead code detection Fixes #119545 Also included is a fix for an error now flagged by the lint
2 parents 0ad5e0d + 7342cc4 commit c308726

File tree

29 files changed

+129
-53
lines changed

29 files changed

+129
-53
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20652065
from_closure: constraint.from_closure,
20662066
cause: ObligationCause::new(constraint.span, CRATE_DEF_ID, cause_code.clone()),
20672067
variance_info: constraint.variance_info,
2068-
outlives_constraint: *constraint,
20692068
})
20702069
.collect();
20712070
debug!("categorized_path={:#?}", categorized_path);
@@ -2294,5 +2293,4 @@ pub struct BlameConstraint<'tcx> {
22942293
pub from_closure: bool,
22952294
pub cause: ObligationCause<'tcx>,
22962295
pub variance_info: ty::VarianceDiagInfo<'tcx>,
2297-
pub outlives_constraint: OutlivesConstraint<'tcx>,
22982296
}

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
5151

5252
let (ident, vdata, fields) = match substr.fields {
5353
Struct(vdata, fields) => (substr.type_ident, *vdata, fields),
54-
EnumMatching(_, _, v, fields) => (v.ident, &v.data, fields),
54+
EnumMatching(_, v, fields) => (v.ident, &v.data, fields),
5555
AllFieldlessEnum(enum_def) => return show_fieldless_enum(cx, span, enum_def, substr),
5656
EnumTag(..) | StaticStruct(..) | StaticEnum(..) => {
5757
cx.dcx().span_bug(span, "nonsensical .fields in `#[derive(Debug)]`")

compiler/rustc_builtin_macros/src/deriving/encodable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn encodable_substructure(
226226
BlockOrExpr::new_expr(expr)
227227
}
228228

229-
EnumMatching(idx, _, variant, fields) => {
229+
EnumMatching(idx, variant, fields) => {
230230
// We're not generating an AST that the borrow checker is expecting,
231231
// so we need to generate a unique local variable to take the
232232
// mutable loan out on, otherwise we get conflicts which don't

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,10 @@ pub enum SubstructureFields<'a> {
310310
/// variants has any fields).
311311
AllFieldlessEnum(&'a ast::EnumDef),
312312

313-
/// Matching variants of the enum: variant index, variant count, ast::Variant,
313+
/// Matching variants of the enum: variant index, ast::Variant,
314314
/// fields: the field name is only non-`None` in the case of a struct
315315
/// variant.
316-
EnumMatching(usize, usize, &'a ast::Variant, Vec<FieldInfo>),
316+
EnumMatching(usize, &'a ast::Variant, Vec<FieldInfo>),
317317

318318
/// The tag of an enum. The first field is a `FieldInfo` for the tags, as
319319
/// if they were fields. The second field is the expression to combine the
@@ -1272,7 +1272,7 @@ impl<'a> MethodDef<'a> {
12721272
trait_,
12731273
type_ident,
12741274
nonselflike_args,
1275-
&EnumMatching(0, 1, &variants[0], Vec::new()),
1275+
&EnumMatching(0, &variants[0], Vec::new()),
12761276
);
12771277
}
12781278
}
@@ -1318,7 +1318,7 @@ impl<'a> MethodDef<'a> {
13181318
// expressions for referencing every field of every
13191319
// Self arg, assuming all are instances of VariantK.
13201320
// Build up code associated with such a case.
1321-
let substructure = EnumMatching(index, variants.len(), variant, fields);
1321+
let substructure = EnumMatching(index, variant, fields);
13221322
let arm_expr = self
13231323
.call_substructure_method(
13241324
cx,
@@ -1346,7 +1346,7 @@ impl<'a> MethodDef<'a> {
13461346
trait_,
13471347
type_ident,
13481348
nonselflike_args,
1349-
&EnumMatching(0, variants.len(), v, Vec::new()),
1349+
&EnumMatching(0, v, Vec::new()),
13501350
)
13511351
.into_expr(cx, span),
13521352
)

compiler/rustc_codegen_gcc/src/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
110110
local_gen_sym_counter: Cell<usize>,
111111

112112
eh_personality: Cell<Option<RValue<'gcc>>>,
113+
#[cfg(feature="master")]
113114
pub rust_try_fn: Cell<Option<(Type<'gcc>, Function<'gcc>)>>,
114115

115116
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
@@ -121,6 +122,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
121122
/// FIXME(antoyo): fix the rustc API to avoid having this hack.
122123
pub structs_as_pointer: RefCell<FxHashSet<RValue<'gcc>>>,
123124

125+
#[cfg(feature="master")]
124126
pub cleanup_blocks: RefCell<FxHashSet<Block<'gcc>>>,
125127
}
126128

@@ -325,9 +327,11 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
325327
struct_types: Default::default(),
326328
local_gen_sym_counter: Cell::new(0),
327329
eh_personality: Cell::new(None),
330+
#[cfg(feature="master")]
328331
rust_try_fn: Cell::new(None),
329332
pointee_infos: Default::default(),
330333
structs_as_pointer: Default::default(),
334+
#[cfg(feature="master")]
331335
cleanup_blocks: Default::default(),
332336
};
333337
// TODO(antoyo): instead of doing this, add SsizeT to libgccjit.

compiler/rustc_codegen_llvm/src/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
2727
use rustc_session::Session;
2828
use rustc_span::source_map::Spanned;
2929
use rustc_span::Span;
30-
use rustc_target::abi::{
31-
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
32-
};
30+
use rustc_target::abi::{call::FnAbi, HasDataLayout, TargetDataLayout, VariantIdx};
3331
use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3432
use smallvec::SmallVec;
3533

@@ -83,7 +81,6 @@ pub struct CodegenCx<'ll, 'tcx> {
8381
/// Mapping of scalar types to llvm types.
8482
pub scalar_lltypes: RefCell<FxHashMap<Ty<'tcx>, &'ll Type>>,
8583

86-
pub pointee_infos: RefCell<FxHashMap<(Ty<'tcx>, Size), Option<PointeeInfo>>>,
8784
pub isize_ty: &'ll Type,
8885

8986
pub coverage_cx: Option<coverageinfo::CrateCoverageContext<'ll, 'tcx>>,
@@ -450,7 +447,6 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
450447
compiler_used_statics: RefCell::new(Vec::new()),
451448
type_lowering: Default::default(),
452449
scalar_lltypes: Default::default(),
453-
pointee_infos: Default::default(),
454450
isize_ty,
455451
coverage_cx,
456452
dbg_cx,

compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
131131
|local, value, location| {
132132
let value = match value {
133133
// We do not know anything of this assigned value.
134-
AssignedValue::Arg | AssignedValue::Terminator(_) => None,
134+
AssignedValue::Arg | AssignedValue::Terminator => None,
135135
// Try to get some insight.
136136
AssignedValue::Rvalue(rvalue) => {
137137
let value = state.simplify_rvalue(rvalue, location);

compiler/rustc_mir_transform/src/ssa.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct SsaLocals {
2929
pub enum AssignedValue<'a, 'tcx> {
3030
Arg,
3131
Rvalue(&'a mut Rvalue<'tcx>),
32-
Terminator(&'a mut TerminatorKind<'tcx>),
32+
Terminator,
3333
}
3434

3535
impl SsaLocals {
@@ -149,8 +149,7 @@ impl SsaLocals {
149149
Set1::One(DefLocation::CallReturn { call, .. }) => {
150150
let bb = &mut basic_blocks[call];
151151
let loc = Location { block: call, statement_index: bb.statements.len() };
152-
let term = bb.terminator_mut();
153-
f(local, AssignedValue::Terminator(&mut term.kind), loc)
152+
f(local, AssignedValue::Terminator, loc)
154153
}
155154
_ => {}
156155
}

compiler/rustc_passes/src/dead.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,16 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
525525
let tcx = self.tcx;
526526
let unconditionally_treat_fields_as_live = self.repr_unconditionally_treats_fields_as_live;
527527
let has_repr_simd = self.repr_has_repr_simd;
528+
let effective_visibilities = &tcx.effective_visibilities(());
528529
let live_fields = def.fields().iter().filter_map(|f| {
529530
let def_id = f.def_id;
530531
if unconditionally_treat_fields_as_live || (f.is_positional() && has_repr_simd) {
531532
return Some(def_id);
532533
}
533-
if !tcx.visibility(f.hir_id.owner.def_id).is_public() {
534+
if !effective_visibilities.is_reachable(f.hir_id.owner.def_id) {
534535
return None;
535536
}
536-
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
537+
if effective_visibilities.is_reachable(def_id) { Some(def_id) } else { None }
537538
});
538539
self.live_symbols.extend(live_fields);
539540

library/alloc/src/collections/btree/navigate.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ impl<BorrowType: marker::BorrowType, K, V> NodeRef<BorrowType, K, V, marker::Lea
655655
pub enum Position<BorrowType, K, V> {
656656
Leaf(NodeRef<BorrowType, K, V, marker::Leaf>),
657657
Internal(NodeRef<BorrowType, K, V, marker::Internal>),
658-
InternalKV(Handle<NodeRef<BorrowType, K, V, marker::Internal>, marker::KV>),
658+
InternalKV,
659659
}
660660

661661
impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal> {
@@ -677,7 +677,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
677677
visit(Position::Leaf(leaf));
678678
match edge.next_kv() {
679679
Ok(kv) => {
680-
visit(Position::InternalKV(kv));
680+
visit(Position::InternalKV);
681681
kv.right_edge()
682682
}
683683
Err(_) => return,
@@ -699,7 +699,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
699699
self.visit_nodes_in_order(|pos| match pos {
700700
Position::Leaf(node) => result += node.len(),
701701
Position::Internal(node) => result += node.len(),
702-
Position::InternalKV(_) => (),
702+
Position::InternalKV => (),
703703
});
704704
result
705705
}

library/alloc/src/collections/btree/node/tests.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::LeafOrInternal>
3232
result += &format!("\n{}{:?}", indent, leaf.keys());
3333
}
3434
navigate::Position::Internal(_) => {}
35-
navigate::Position::InternalKV(kv) => {
36-
let depth = self.height() - kv.into_node().height();
37-
let indent = " ".repeat(depth);
38-
result += &format!("\n{}{:?}", indent, kv.into_kv().0);
39-
}
35+
navigate::Position::InternalKV => {}
4036
});
4137
result
4238
}

library/std/src/sys/pal/sgx/net.rs

+2
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ pub mod netc {
524524

525525
#[derive(Copy, Clone)]
526526
pub struct sockaddr_in {
527+
#[allow(dead_code)]
527528
pub sin_family: sa_family_t,
528529
pub sin_port: u16,
529530
pub sin_addr: in_addr,
@@ -536,6 +537,7 @@ pub mod netc {
536537

537538
#[derive(Copy, Clone)]
538539
pub struct sockaddr_in6 {
540+
#[allow(dead_code)]
539541
pub sin6_family: sa_family_t,
540542
pub sin6_port: u16,
541543
pub sin6_addr: in6_addr,

library/std/src/sys/pal/unix/thread_local_dtor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern "C" fn(*mut u8)) {
3535
#[cfg(not(sanitizer_cfi_normalize_integers))]
3636
#[cfi_encoding = "i"]
3737
#[repr(transparent)]
38-
pub struct c_int(pub libc::c_int);
38+
pub struct c_int(#[allow(dead_code)] pub libc::c_int);
3939

4040
extern "C" {
4141
#[linkage = "extern_weak"]

library/std/src/sys/pal/unsupported/net.rs

+2
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ pub mod netc {
346346

347347
#[derive(Copy, Clone)]
348348
pub struct sockaddr_in {
349+
#[allow(dead_code)]
349350
pub sin_family: sa_family_t,
350351
pub sin_port: u16,
351352
pub sin_addr: in_addr,
@@ -358,6 +359,7 @@ pub mod netc {
358359

359360
#[derive(Copy, Clone)]
360361
pub struct sockaddr_in6 {
362+
#[allow(dead_code)]
361363
pub sin6_family: sa_family_t,
362364
pub sin6_port: u16,
363365
pub sin6_addr: in6_addr,

library/std/src/sys/pal/wasi/net.rs

+2
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ pub mod netc {
520520

521521
#[derive(Copy, Clone)]
522522
pub struct sockaddr_in {
523+
#[allow(dead_code)]
523524
pub sin_family: sa_family_t,
524525
pub sin_port: u16,
525526
pub sin_addr: in_addr,
@@ -532,6 +533,7 @@ pub mod netc {
532533

533534
#[derive(Copy, Clone)]
534535
pub struct sockaddr_in6 {
536+
#[allow(dead_code)]
535537
pub sin6_family: sa_family_t,
536538
pub sin6_port: u16,
537539
pub sin6_addr: in6_addr,

library/test/src/console.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub struct ConsoleTestDiscoveryState {
4646
pub tests: usize,
4747
pub benchmarks: usize,
4848
pub ignored: usize,
49-
pub options: Options,
5049
}
5150

5251
impl ConsoleTestDiscoveryState {
@@ -56,13 +55,7 @@ impl ConsoleTestDiscoveryState {
5655
None => None,
5756
};
5857

59-
Ok(ConsoleTestDiscoveryState {
60-
log_out,
61-
tests: 0,
62-
benchmarks: 0,
63-
ignored: 0,
64-
options: opts.options,
65-
})
58+
Ok(ConsoleTestDiscoveryState { log_out, tests: 0, benchmarks: 0, ignored: 0 })
6659
}
6760

6861
pub fn write_log<F, S>(&mut self, msg: F) -> io::Result<()>

src/bootstrap/src/core/build_steps/compile.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -2140,18 +2140,9 @@ pub struct CargoTarget<'a> {
21402140
#[derive(Deserialize)]
21412141
#[serde(tag = "reason", rename_all = "kebab-case")]
21422142
pub enum CargoMessage<'a> {
2143-
CompilerArtifact {
2144-
package_id: Cow<'a, str>,
2145-
features: Vec<Cow<'a, str>>,
2146-
filenames: Vec<Cow<'a, str>>,
2147-
target: CargoTarget<'a>,
2148-
},
2149-
BuildScriptExecuted {
2150-
package_id: Cow<'a, str>,
2151-
},
2152-
BuildFinished {
2153-
success: bool,
2154-
},
2143+
CompilerArtifact { filenames: Vec<Cow<'a, str>>, target: CargoTarget<'a> },
2144+
BuildScriptExecuted,
2145+
BuildFinished,
21552146
}
21562147

21572148
pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path) {

src/tools/clippy/clippy_lints/src/raw_strings.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ impl EarlyLintPass for RawStrings {
108108
}
109109
}
110110

111-
let req = {
111+
let mut req = {
112112
let mut following_quote = false;
113113
let mut req = 0;
114114
// `once` so a raw string ending in hashes is still checked
@@ -136,7 +136,9 @@ impl EarlyLintPass for RawStrings {
136136
ControlFlow::Continue(num) | ControlFlow::Break(num) => num,
137137
}
138138
};
139-
139+
if self.allow_one_hash_in_raw_strings {
140+
req = req.max(1);
141+
}
140142
if req < max {
141143
span_lint_and_then(
142144
cx,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
allow-one-hash-in-raw-strings = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(clippy::no_effect, unused)]
2+
#![warn(clippy::needless_raw_string_hashes)]
3+
4+
fn main() {
5+
r#"\aaa"#;
6+
r#"\aaa"#;
7+
r#"Hello "world"!"#;
8+
r####" "### "## "# "####;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![allow(clippy::no_effect, unused)]
2+
#![warn(clippy::needless_raw_string_hashes)]
3+
4+
fn main() {
5+
r#"\aaa"#;
6+
r##"\aaa"##;
7+
r##"Hello "world"!"##;
8+
r######" "### "## "# "######;
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error: unnecessary hashes around raw string literal
2+
--> tests/ui-toml/needless_raw_string_hashes_one_allowed/needless_raw_string_hashes.rs:6:5
3+
|
4+
LL | r##"\aaa"##;
5+
| ^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::needless-raw-string-hashes` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::needless_raw_string_hashes)]`
9+
help: remove one hash from both sides of the string literal
10+
|
11+
LL - r##"\aaa"##;
12+
LL + r#"\aaa"#;
13+
|
14+
15+
error: unnecessary hashes around raw string literal
16+
--> tests/ui-toml/needless_raw_string_hashes_one_allowed/needless_raw_string_hashes.rs:7:5
17+
|
18+
LL | r##"Hello "world"!"##;
19+
| ^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: remove one hash from both sides of the string literal
22+
|
23+
LL - r##"Hello "world"!"##;
24+
LL + r#"Hello "world"!"#;
25+
|
26+
27+
error: unnecessary hashes around raw string literal
28+
--> tests/ui-toml/needless_raw_string_hashes_one_allowed/needless_raw_string_hashes.rs:8:5
29+
|
30+
LL | r######" "### "## "# "######;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
help: remove 2 hashes from both sides of the string literal
34+
|
35+
LL - r######" "### "## "# "######;
36+
LL + r####" "### "## "# "####;
37+
|
38+
39+
error: aborting due to 3 previous errors
40+

0 commit comments

Comments
 (0)