Skip to content

Commit 82af160

Browse files
committed
Auto merge of #90769 - matthiaskrgr:rollup-266apqm, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - #88447 (Use computed visibility in rustdoc) - #88868 (Allow simd_bitmask to return byte arrays) - #90727 (Remove potential useless data for search index) - #90742 (Use AddAssign impl) - #90758 (Fix collections entry API documentation.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 68ca579 + 5f24975 commit 82af160

File tree

25 files changed

+234
-139
lines changed

25 files changed

+234
-139
lines changed

compiler/rustc_ast/src/util/comments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
169169
if let Some(mut idx) = token_text.find('\n') {
170170
code_to_the_left = false;
171171
while let Some(next_newline) = &token_text[idx + 1..].find('\n') {
172-
idx = idx + 1 + next_newline;
172+
idx += 1 + next_newline;
173173
comments.push(Comment {
174174
style: CommentStyle::BlankLine,
175175
lines: vec![],

compiler/rustc_codegen_llvm/src/intrinsic.rs

+69-32
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf};
1919
use rustc_middle::ty::{self, Ty};
2020
use rustc_middle::{bug, span_bug};
2121
use rustc_span::{sym, symbol::kw, Span, Symbol};
22-
use rustc_target::abi::{self, HasDataLayout, Primitive};
22+
use rustc_target::abi::{self, Align, HasDataLayout, Primitive};
2323
use rustc_target::spec::{HasTargetSpec, PanicStrategy};
2424

2525
use std::cmp::Ordering;
@@ -857,28 +857,39 @@ fn generic_simd_intrinsic(
857857
let arg_tys = sig.inputs();
858858

859859
if name == sym::simd_select_bitmask {
860-
let in_ty = arg_tys[0];
861-
let m_len = match in_ty.kind() {
862-
// Note that this `.unwrap()` crashes for isize/usize, that's sort
863-
// of intentional as there's not currently a use case for that.
864-
ty::Int(i) => i.bit_width().unwrap(),
865-
ty::Uint(i) => i.bit_width().unwrap(),
866-
_ => return_error!("`{}` is not an integral type", in_ty),
867-
};
868860
require_simd!(arg_tys[1], "argument");
869-
let (v_len, _) = arg_tys[1].simd_size_and_type(bx.tcx());
870-
require!(
871-
// Allow masks for vectors with fewer than 8 elements to be
872-
// represented with a u8 or i8.
873-
m_len == v_len || (m_len == 8 && v_len < 8),
874-
"mismatched lengths: mask length `{}` != other vector length `{}`",
875-
m_len,
876-
v_len
877-
);
861+
let (len, _) = arg_tys[1].simd_size_and_type(bx.tcx());
862+
863+
let expected_int_bits = (len.max(8) - 1).next_power_of_two();
864+
let expected_bytes = len / 8 + ((len % 8 > 0) as u64);
865+
866+
let mask_ty = arg_tys[0];
867+
let mask = match mask_ty.kind() {
868+
ty::Int(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
869+
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => args[0].immediate(),
870+
ty::Array(elem, len)
871+
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
872+
&& len.try_eval_usize(bx.tcx, ty::ParamEnv::reveal_all())
873+
== Some(expected_bytes) =>
874+
{
875+
let place = PlaceRef::alloca(bx, args[0].layout);
876+
args[0].val.store(bx, place);
877+
let int_ty = bx.type_ix(expected_bytes * 8);
878+
let ptr = bx.pointercast(place.llval, bx.cx.type_ptr_to(int_ty));
879+
bx.load(int_ty, ptr, Align::ONE)
880+
}
881+
_ => return_error!(
882+
"invalid bitmask `{}`, expected `u{}` or `[u8; {}]`",
883+
mask_ty,
884+
expected_int_bits,
885+
expected_bytes
886+
),
887+
};
888+
878889
let i1 = bx.type_i1();
879-
let im = bx.type_ix(v_len);
880-
let i1xn = bx.type_vector(i1, v_len);
881-
let m_im = bx.trunc(args[0].immediate(), im);
890+
let im = bx.type_ix(len);
891+
let i1xn = bx.type_vector(i1, len);
892+
let m_im = bx.trunc(mask, im);
882893
let m_i1s = bx.bitcast(m_im, i1xn);
883894
return Ok(bx.select(m_i1s, args[1].immediate(), args[2].immediate()));
884895
}
@@ -1056,16 +1067,16 @@ fn generic_simd_intrinsic(
10561067

10571068
if name == sym::simd_bitmask {
10581069
// The `fn simd_bitmask(vector) -> unsigned integer` intrinsic takes a
1059-
// vector mask and returns an unsigned integer containing the most
1060-
// significant bit (MSB) of each lane.
1061-
1062-
// If the vector has less than 8 lanes, a u8 is returned with zeroed
1063-
// trailing bits.
1070+
// vector mask and returns the most significant bit (MSB) of each lane in the form
1071+
// of either:
1072+
// * an unsigned integer
1073+
// * an array of `u8`
1074+
// If the vector has less than 8 lanes, a u8 is returned with zeroed trailing bits.
1075+
//
1076+
// The bit order of the result depends on the byte endianness, LSB-first for little
1077+
// endian and MSB-first for big endian.
10641078
let expected_int_bits = in_len.max(8);
1065-
match ret_ty.kind() {
1066-
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => (),
1067-
_ => return_error!("bitmask `{}`, expected `u{}`", ret_ty, expected_int_bits),
1068-
}
1079+
let expected_bytes = expected_int_bits / 8 + ((expected_int_bits % 8 > 0) as u64);
10691080

10701081
// Integer vector <i{in_bitwidth} x in_len>:
10711082
let (i_xn, in_elem_bitwidth) = match in_elem.kind() {
@@ -1095,8 +1106,34 @@ fn generic_simd_intrinsic(
10951106
let i1xn = bx.trunc(i_xn_msb, bx.type_vector(bx.type_i1(), in_len));
10961107
// Bitcast <i1 x N> to iN:
10971108
let i_ = bx.bitcast(i1xn, bx.type_ix(in_len));
1098-
// Zero-extend iN to the bitmask type:
1099-
return Ok(bx.zext(i_, bx.type_ix(expected_int_bits)));
1109+
1110+
match ret_ty.kind() {
1111+
ty::Uint(i) if i.bit_width() == Some(expected_int_bits) => {
1112+
// Zero-extend iN to the bitmask type:
1113+
return Ok(bx.zext(i_, bx.type_ix(expected_int_bits)));
1114+
}
1115+
ty::Array(elem, len)
1116+
if matches!(elem.kind(), ty::Uint(ty::UintTy::U8))
1117+
&& len.try_eval_usize(bx.tcx, ty::ParamEnv::reveal_all())
1118+
== Some(expected_bytes) =>
1119+
{
1120+
// Zero-extend iN to the array lengh:
1121+
let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));
1122+
1123+
// Convert the integer to a byte array
1124+
let ptr = bx.alloca(bx.type_ix(expected_bytes * 8), Align::ONE);
1125+
bx.store(ze, ptr, Align::ONE);
1126+
let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
1127+
let ptr = bx.pointercast(ptr, bx.cx.type_ptr_to(array_ty));
1128+
return Ok(bx.load(array_ty, ptr, Align::ONE));
1129+
}
1130+
_ => return_error!(
1131+
"cannot return `{}`, expected `u{}` or `[u8; {}]`",
1132+
ret_ty,
1133+
expected_int_bits,
1134+
expected_bytes
1135+
),
1136+
}
11001137
}
11011138

11021139
fn simd_simple_float_intrinsic(

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11791179
let ctor_res =
11801180
Res::Def(DefKind::Ctor(CtorOf::Variant, ctor_kind), ctor_def_id);
11811181
let mut vis = self.get_visibility(ctor_def_id.index);
1182-
if ctor_def_id == def_id && vis == ty::Visibility::Public {
1182+
if ctor_def_id == def_id && vis.is_public() {
11831183
// For non-exhaustive variants lower the constructor visibility to
11841184
// within the crate. We only need this for fictive constructors,
11851185
// for other constructors correct visibilities

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ pub fn provide(providers: &mut Providers) {
318318
}
319319

320320
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &Export, parent: DefId| {
321-
if child.vis != ty::Visibility::Public {
321+
if !child.vis.is_public() {
322322
return;
323323
}
324324

compiler/rustc_middle/src/ty/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ impl Visibility {
333333
Visibility::Invisible => false,
334334
}
335335
}
336+
337+
pub fn is_public(self) -> bool {
338+
matches!(self, Visibility::Public)
339+
}
336340
}
337341

338342
/// The crate variances map is computed during typeck and contains the

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
24042404
// Iterate external crate defs but be mindful about visibility
24052405
while let Some(def) = queue.pop() {
24062406
for child in tcx.item_children(def).iter() {
2407-
if child.vis != ty::Visibility::Public {
2407+
if !child.vis.is_public() {
24082408
continue;
24092409
}
24102410

compiler/rustc_middle/src/util/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
let rv = f();
3535
let duration = start.elapsed();
3636
let mut accu = accu.lock();
37-
*accu = *accu + duration;
37+
*accu += duration;
3838
rv
3939
}
4040

compiler/rustc_privacy/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl EmbargoVisitor<'tcx> {
543543
module: LocalDefId,
544544
) {
545545
let level = Some(AccessLevel::Reachable);
546-
if let ty::Visibility::Public = vis {
546+
if vis.is_public() {
547547
self.update(def_id, level);
548548
}
549549
match def_kind {
@@ -580,7 +580,7 @@ impl EmbargoVisitor<'tcx> {
580580

581581
DefKind::Struct | DefKind::Union => {
582582
// While structs and unions have type privacy, their fields do not.
583-
if let ty::Visibility::Public = vis {
583+
if vis.is_public() {
584584
let item =
585585
self.tcx.hir().expect_item(self.tcx.hir().local_def_id_to_hir_id(def_id));
586586
if let hir::ItemKind::Struct(ref struct_def, _)
@@ -933,7 +933,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
933933
let def_id = self.tcx.hir().local_def_id(id);
934934
if let Some(exports) = self.tcx.module_exports(def_id) {
935935
for export in exports.iter() {
936-
if export.vis == ty::Visibility::Public {
936+
if export.vis.is_public() {
937937
if let Some(def_id) = export.res.opt_def_id() {
938938
if let Some(def_id) = def_id.as_local() {
939939
self.update(def_id, Some(AccessLevel::Exported));
@@ -1918,8 +1918,7 @@ impl SearchInterfaceForPrivateItemsVisitor<'tcx> {
19181918
/// 1. It's contained within a public type
19191919
/// 2. It comes from a private crate
19201920
fn leaks_private_dep(&self, item_id: DefId) -> bool {
1921-
let ret = self.required_visibility == ty::Visibility::Public
1922-
&& self.tcx.is_private_dep(item_id.krate);
1921+
let ret = self.required_visibility.is_public() && self.tcx.is_private_dep(item_id.krate);
19231922

19241923
tracing::debug!("leaks_private_dep(item_id={:?})={}", item_id, ret);
19251924
ret

compiler/rustc_resolve/src/check_unused.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc_ast::visit::{self, Visitor};
3232
use rustc_ast_lowering::ResolverAstLowering;
3333
use rustc_data_structures::fx::FxHashSet;
3434
use rustc_errors::pluralize;
35-
use rustc_middle::ty;
3635
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_IMPORTS};
3736
use rustc_session::lint::BuiltinLintDiagnostics;
3837
use rustc_span::{MultiSpan, Span, DUMMY_SP};
@@ -228,7 +227,7 @@ impl Resolver<'_> {
228227
for import in self.potentially_unused_imports.iter() {
229228
match import.kind {
230229
_ if import.used.get()
231-
|| import.vis.get() == ty::Visibility::Public
230+
|| import.vis.get().is_public()
232231
|| import.span.is_dummy() =>
233232
{
234233
if let ImportKind::MacroUse = import.kind {

compiler/rustc_resolve/src/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
1111
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
1212
use rustc_hir::PrimTy;
1313
use rustc_middle::bug;
14-
use rustc_middle::ty::{self, DefIdTree};
14+
use rustc_middle::ty::DefIdTree;
1515
use rustc_session::Session;
1616
use rustc_span::hygiene::MacroKind;
1717
use rustc_span::lev_distance::find_best_match_for_name;
@@ -1308,7 +1308,7 @@ impl<'a> Resolver<'a> {
13081308
);
13091309
let def_span = self.session.source_map().guess_head_span(binding.span);
13101310
let mut note_span = MultiSpan::from_span(def_span);
1311-
if !first && binding.vis == ty::Visibility::Public {
1311+
if !first && binding.vis.is_public() {
13121312
note_span.push_span_label(def_span, "consider importing it directly".into());
13131313
}
13141314
err.span_note(note_span, &msg);

compiler/rustc_resolve/src/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBindi
164164
import: Import { kind: ImportKind::ExternCrate { .. }, .. },
165165
..
166166
},
167-
) => import.vis.get() == ty::Visibility::Public,
167+
) => import.vis.get().is_public(),
168168
_ => false,
169169
}
170170
}

compiler/rustc_typeck/src/check/expr.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,11 @@ use rustc_hir::{ExprKind, QPath};
3535
use rustc_infer::infer;
3636
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
3737
use rustc_infer::infer::InferOk;
38-
use rustc_middle::ty;
3938
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
4039
use rustc_middle::ty::error::TypeError::{FieldMisMatch, Sorts};
4140
use rustc_middle::ty::relate::expected_found_bool;
4241
use rustc_middle::ty::subst::SubstsRef;
43-
use rustc_middle::ty::Ty;
44-
use rustc_middle::ty::TypeFoldable;
45-
use rustc_middle::ty::{AdtKind, Visibility};
42+
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
4643
use rustc_session::parse::feature_err;
4744
use rustc_span::edition::LATEST_STABLE_EDITION;
4845
use rustc_span::hygiene::DesugaringKind;
@@ -1732,7 +1729,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17321729
.filter_map(|field| {
17331730
// ignore already set fields and private fields from non-local crates
17341731
if skip.iter().any(|&x| x == field.ident.name)
1735-
|| (!variant.def_id.is_local() && field.vis != Visibility::Public)
1732+
|| (!variant.def_id.is_local() && !field.vis.is_public())
17361733
{
17371734
None
17381735
} else {

compiler/rustc_typeck/src/check/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14101410
}
14111411
}
14121412
// We only want to suggest public or local traits (#45781).
1413-
item.vis == ty::Visibility::Public || info.def_id.is_local()
1413+
item.vis.is_public() || info.def_id.is_local()
14141414
})
14151415
.is_some()
14161416
})

library/std/src/collections/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@
268268
//! not. Normally, this would require a `find` followed by an `insert`,
269269
//! effectively duplicating the search effort on each insertion.
270270
//!
271-
//! When a user calls `map.entry(&key)`, the map will search for the key and
271+
//! When a user calls `map.entry(key)`, the map will search for the key and
272272
//! then yield a variant of the `Entry` enum.
273273
//!
274274
//! If a `Vacant(entry)` is yielded, then the key *was not* found. In this case

src/librustdoc/clean/inline.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ crate fn build_impl(
435435
tcx.associated_items(did)
436436
.in_definition_order()
437437
.filter_map(|item| {
438-
if associated_trait.is_some() || item.vis == ty::Visibility::Public {
438+
if associated_trait.is_some() || item.vis.is_public() {
439439
Some(item.clean(cx))
440440
} else {
441441
None
@@ -515,7 +515,7 @@ fn build_module(
515515
// two namespaces, so the target may be listed twice. Make sure we only
516516
// visit each node at most once.
517517
for &item in cx.tcx.item_children(did).iter() {
518-
if item.vis == ty::Visibility::Public {
518+
if item.vis.is_public() {
519519
let res = item.res.expect_non_local();
520520
if let Some(def_id) = res.mod_def_id() {
521521
if did == def_id || !visited.insert(def_id) {

src/librustdoc/clean/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ fn clean_extern_crate(
18811881
// this is the ID of the crate itself
18821882
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
18831883
let attrs = cx.tcx.hir().attrs(krate.hir_id());
1884-
let please_inline = krate.vis.node.is_pub()
1884+
let please_inline = cx.tcx.visibility(krate.def_id).is_public()
18851885
&& attrs.iter().any(|a| {
18861886
a.has_name(sym::doc)
18871887
&& match a.meta_item_list() {
@@ -1933,9 +1933,12 @@ fn clean_use_statement(
19331933
return Vec::new();
19341934
}
19351935

1936+
let visibility = cx.tcx.visibility(import.def_id);
19361937
let attrs = cx.tcx.hir().attrs(import.hir_id());
19371938
let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline);
1938-
let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore;
1939+
let pub_underscore = visibility.is_public() && name == kw::Underscore;
1940+
let current_mod = cx.tcx.parent_module_from_def_id(import.def_id);
1941+
let parent_mod = cx.tcx.parent_module_from_def_id(current_mod);
19391942

19401943
if pub_underscore {
19411944
if let Some(ref inline) = inline_attr {
@@ -1954,8 +1957,9 @@ fn clean_use_statement(
19541957
// forcefully don't inline if this is not public or if the
19551958
// #[doc(no_inline)] attribute is present.
19561959
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
1957-
let mut denied = !(import.vis.node.is_pub()
1958-
|| (cx.render_options.document_private && import.vis.node.is_pub_restricted()))
1960+
let mut denied = !(visibility.is_public()
1961+
|| (cx.render_options.document_private
1962+
&& visibility.is_accessible_from(parent_mod.to_def_id(), cx.tcx)))
19591963
|| pub_underscore
19601964
|| attrs.iter().any(|a| {
19611965
a.has_name(sym::doc)

src/librustdoc/clean/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl ExternalCrate {
254254
as_keyword(Res::Def(DefKind::Mod, id.def_id.to_def_id()))
255255
}
256256
hir::ItemKind::Use(path, hir::UseKind::Single)
257-
if item.vis.node.is_pub() =>
257+
if tcx.visibility(id.def_id).is_public() =>
258258
{
259259
as_keyword(path.res.expect_non_local())
260260
.map(|(_, prim)| (id.def_id.to_def_id(), prim))
@@ -320,7 +320,7 @@ impl ExternalCrate {
320320
as_primitive(Res::Def(DefKind::Mod, id.def_id.to_def_id()))
321321
}
322322
hir::ItemKind::Use(path, hir::UseKind::Single)
323-
if item.vis.node.is_pub() =>
323+
if tcx.visibility(id.def_id).is_public() =>
324324
{
325325
as_primitive(path.res.expect_non_local()).map(|(_, prim)| {
326326
// Pretend the primitive is local.

0 commit comments

Comments
 (0)