Skip to content

Commit 50f2bf6

Browse files
committed
Auto merge of rust-lang#85335 - GuillaumeGomez:rollup-0tvc14g, r=GuillaumeGomez
Rollup of 4 pull requests Successful merges: - rust-lang#84751 (str::is_char_boundary - slight optimization) - rust-lang#85185 (Generate not more docs than necessary) - rust-lang#85324 (Warn about unused `pub` fields in non-`pub` structs) - rust-lang#85329 (fix version_str comment) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents eac3c7c + 8ea8252 commit 50f2bf6

File tree

10 files changed

+126
-36
lines changed

10 files changed

+126
-36
lines changed

compiler/rustc_interface/src/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
922922
}
923923
}
924924

925-
/// Returns a version string such as "rustc 1.46.0 (04488afe3 2020-08-24)"
925+
/// Returns a version string such as "1.46.0 (04488afe3 2020-08-24)"
926926
pub fn version_str() -> Option<&'static str> {
927927
option_env!("CFG_VERSION")
928928
}

compiler/rustc_passes/src/dead.rs

+29-20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct MarkSymbolVisitor<'tcx> {
4444
repr_has_repr_c: bool,
4545
in_pat: bool,
4646
inherited_pub_visibility: bool,
47+
pub_visibility: bool,
4748
ignore_variant_stack: Vec<DefId>,
4849
// maps from tuple struct constructors to tuple struct items
4950
struct_constructors: FxHashMap<hir::HirId, hir::HirId>,
@@ -188,27 +189,33 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
188189

189190
fn visit_node(&mut self, node: Node<'tcx>) {
190191
let had_repr_c = self.repr_has_repr_c;
191-
self.repr_has_repr_c = false;
192192
let had_inherited_pub_visibility = self.inherited_pub_visibility;
193+
let had_pub_visibility = self.pub_visibility;
194+
self.repr_has_repr_c = false;
193195
self.inherited_pub_visibility = false;
196+
self.pub_visibility = false;
194197
match node {
195-
Node::Item(item) => match item.kind {
196-
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
197-
let def = self.tcx.adt_def(item.def_id);
198-
self.repr_has_repr_c = def.repr.c();
198+
Node::Item(item) => {
199+
self.pub_visibility = item.vis.node.is_pub();
199200

200-
intravisit::walk_item(self, &item);
201-
}
202-
hir::ItemKind::Enum(..) => {
203-
self.inherited_pub_visibility = item.vis.node.is_pub();
201+
match item.kind {
202+
hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
203+
let def = self.tcx.adt_def(item.def_id);
204+
self.repr_has_repr_c = def.repr.c();
204205

205-
intravisit::walk_item(self, &item);
206-
}
207-
hir::ItemKind::ForeignMod { .. } => {}
208-
_ => {
209-
intravisit::walk_item(self, &item);
206+
intravisit::walk_item(self, &item);
207+
}
208+
hir::ItemKind::Enum(..) => {
209+
self.inherited_pub_visibility = self.pub_visibility;
210+
211+
intravisit::walk_item(self, &item);
212+
}
213+
hir::ItemKind::ForeignMod { .. } => {}
214+
_ => {
215+
intravisit::walk_item(self, &item);
216+
}
210217
}
211-
},
218+
}
212219
Node::TraitItem(trait_item) => {
213220
intravisit::walk_trait_item(self, trait_item);
214221
}
@@ -220,8 +227,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
220227
}
221228
_ => {}
222229
}
223-
self.repr_has_repr_c = had_repr_c;
230+
self.pub_visibility = had_pub_visibility;
224231
self.inherited_pub_visibility = had_inherited_pub_visibility;
232+
self.repr_has_repr_c = had_repr_c;
225233
}
226234

227235
fn mark_as_used_if_union(&mut self, adt: &ty::AdtDef, fields: &[hir::ExprField<'_>]) {
@@ -259,10 +267,10 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
259267
) {
260268
let has_repr_c = self.repr_has_repr_c;
261269
let inherited_pub_visibility = self.inherited_pub_visibility;
262-
let live_fields = def
263-
.fields()
264-
.iter()
265-
.filter(|f| has_repr_c || inherited_pub_visibility || f.vis.node.is_pub());
270+
let pub_visibility = self.pub_visibility;
271+
let live_fields = def.fields().iter().filter(|f| {
272+
has_repr_c || (pub_visibility && (inherited_pub_visibility || f.vis.node.is_pub()))
273+
});
266274
self.live_symbols.extend(live_fields.map(|f| f.hir_id));
267275

268276
intravisit::walk_struct_def(self, def);
@@ -500,6 +508,7 @@ fn find_live<'tcx>(
500508
repr_has_repr_c: false,
501509
in_pat: false,
502510
inherited_pub_visibility: false,
511+
pub_visibility: false,
503512
ignore_variant_stack: vec![],
504513
struct_constructors,
505514
};

library/core/src/str/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,26 @@ impl str {
192192
#[stable(feature = "is_char_boundary", since = "1.9.0")]
193193
#[inline]
194194
pub fn is_char_boundary(&self, index: usize) -> bool {
195-
// 0 and len are always ok.
195+
// 0 is always ok.
196196
// Test for 0 explicitly so that it can optimize out the check
197197
// easily and skip reading string data for that case.
198-
if index == 0 || index == self.len() {
198+
// Note that optimizing `self.get(..index)` relies on this.
199+
if index == 0 {
199200
return true;
200201
}
202+
201203
match self.as_bytes().get(index) {
202-
None => false,
204+
// For `None` we have two options:
205+
//
206+
// - index == self.len()
207+
// Empty strings are valid, so return true
208+
// - index > self.len()
209+
// In this case return false
210+
//
211+
// The check is placed exactly here, because it improves generated
212+
// code on higher opt-levels. See PR #84751 for more details.
213+
None => index == self.len(),
214+
203215
// This is bit magic equivalent to: b < 128 || b >= 192
204216
Some(&b) => (b as i8) >= -0x40,
205217
}

src/bootstrap/doc.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,22 @@ impl Step for Std {
451451

452452
builder.run(&mut cargo.into());
453453
};
454+
455+
let paths = builder
456+
.paths
457+
.iter()
458+
.map(components_simplified)
459+
.filter_map(|path| {
460+
if path.get(0) == Some(&"library") {
461+
Some(path[1].to_owned())
462+
} else if !path.is_empty() {
463+
Some(path[0].to_owned())
464+
} else {
465+
None
466+
}
467+
})
468+
.collect::<Vec<_>>();
469+
454470
// Only build the following crates. While we could just iterate over the
455471
// folder structure, that would also build internal crates that we do
456472
// not want to show in documentation. These crates will later be visited
@@ -464,20 +480,17 @@ impl Step for Std {
464480
let krates = ["core", "alloc", "std", "proc_macro", "test"];
465481
for krate in &krates {
466482
run_cargo_rustdoc_for(krate);
483+
if paths.iter().any(|p| p == krate) {
484+
// No need to document more of the libraries if we have the one we want.
485+
break;
486+
}
467487
}
468488
builder.cp_r(&out_dir, &out);
469489

470490
// Look for library/std, library/core etc in the `x.py doc` arguments and
471491
// open the corresponding rendered docs.
472-
for path in builder.paths.iter().map(components_simplified) {
473-
let requested_crate = if path.get(0) == Some(&"library") {
474-
&path[1]
475-
} else if !path.is_empty() {
476-
&path[0]
477-
} else {
478-
continue;
479-
};
480-
if krates.contains(&requested_crate) {
492+
for requested_crate in paths {
493+
if krates.iter().any(|k| *k == requested_crate.as_str()) {
481494
let index = out.join(requested_crate).join("index.html");
482495
open(builder, &index);
483496
}

src/ci/docker/host-x86_64/mingw-check/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ENV SCRIPT python3 ../x.py --stage 2 test src/tools/expand-yaml-anchors && \
3434
python3 ../x.py build --stage 0 src/tools/build-manifest && \
3535
python3 ../x.py test --stage 0 src/tools/compiletest && \
3636
python3 ../x.py test --stage 2 src/tools/tidy && \
37-
python3 ../x.py doc --stage 0 library/std && \
37+
python3 ../x.py doc --stage 0 library/test && \
3838
/scripts/validate-toolstate.sh && \
3939
# Runs checks to ensure that there are no ES5 issues in our JS code.
4040
es-check es5 ../src/librustdoc/html/static/*.js

src/test/ui/cast/issue-84213.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct Something {
66

77
fn main() {
88
let mut something = Something { field: 1337 };
9+
let _ = something.field;
910

1011
let _pointer_to_something = &something as *const Something;
1112
//~^ ERROR: non-primitive cast

src/test/ui/cast/issue-84213.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct Something {
66

77
fn main() {
88
let mut something = Something { field: 1337 };
9+
let _ = something.field;
910

1011
let _pointer_to_something = something as *const Something;
1112
//~^ ERROR: non-primitive cast

src/test/ui/cast/issue-84213.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0605]: non-primitive cast: `Something` as `*const Something`
2-
--> $DIR/issue-84213.rs:10:33
2+
--> $DIR/issue-84213.rs:11:33
33
|
44
LL | let _pointer_to_something = something as *const Something;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
@@ -10,7 +10,7 @@ LL | let _pointer_to_something = &something as *const Something;
1010
| ^
1111

1212
error[E0605]: non-primitive cast: `Something` as `*mut Something`
13-
--> $DIR/issue-84213.rs:13:37
13+
--> $DIR/issue-84213.rs:14:37
1414
|
1515
LL | let _mut_pointer_to_something = something as *mut Something;
1616
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Unused `pub` fields in non-`pub` structs should also trigger dead code warnings.
2+
// check-pass
3+
4+
#![warn(dead_code)]
5+
6+
struct Foo {
7+
a: i32, //~ WARNING: field is never read
8+
pub b: i32, //~ WARNING: field is never read
9+
}
10+
11+
struct Bar;
12+
13+
impl Bar {
14+
fn a(&self) -> i32 { 5 } //~ WARNING: associated function is never used
15+
pub fn b(&self) -> i32 { 6 } //~ WARNING: associated function is never used
16+
}
17+
18+
19+
fn main() {
20+
let _ = Foo { a: 1, b: 2 };
21+
let _ = Bar;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
warning: field is never read: `a`
2+
--> $DIR/issue-85255.rs:7:5
3+
|
4+
LL | a: i32,
5+
| ^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-85255.rs:4:9
9+
|
10+
LL | #![warn(dead_code)]
11+
| ^^^^^^^^^
12+
13+
warning: field is never read: `b`
14+
--> $DIR/issue-85255.rs:8:5
15+
|
16+
LL | pub b: i32,
17+
| ^^^^^^^^^^
18+
19+
warning: associated function is never used: `a`
20+
--> $DIR/issue-85255.rs:14:8
21+
|
22+
LL | fn a(&self) -> i32 { 5 }
23+
| ^
24+
25+
warning: associated function is never used: `b`
26+
--> $DIR/issue-85255.rs:15:12
27+
|
28+
LL | pub fn b(&self) -> i32 { 6 }
29+
| ^
30+
31+
warning: 4 warnings emitted
32+

0 commit comments

Comments
 (0)