Skip to content

Commit

Permalink
Auto merge of #89572 - Manishearth:rollup-obz5ycp, r=Manishearth
Browse files Browse the repository at this point in the history
Rollup of 10 pull requests

Successful merges:

 - #88706 (Normalize associated type projections when checking return type of main)
 - #88828 (Use `libc::sigaction()` instead of `sys::signal()` to prevent a deadlock)
 - #88871 (Fix suggestion for nested struct patterns)
 - #89317 (Move generic error message to separate branches)
 - #89351 (for signed wrapping remainder, do not compare lhs with MIN)
 - #89442 (Add check for duplicated doc aliases)
 - #89502 (Fix Lower/UpperExp formatting for integers and precision zero)
 - #89523 (Make `proc_macro_derive_resolution_fallback` a future-breakage lint)
 - #89532 (Document behavior of  `MaybeLiveLocals` regarding enums and field-senstivity)
 - #89546 (Make an initial guess for metadata size to reduce buffer resizes)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Oct 5, 2021
2 parents 25ec827 + 5f8b161 commit 98a5a98
Show file tree
Hide file tree
Showing 27 changed files with 455 additions and 80 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ declare_lint! {
"detects proc macro derives using inaccessible names from parent modules",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #83583 <https://github.com/rust-lang/rust/issues/83583>",
reason: FutureIncompatibilityReason::FutureReleaseErrorReportNow,
};
}

Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,9 @@ fn get_metadata_section(
// Header is okay -> inflate the actual metadata
let compressed_bytes = &buf[header_len..];
debug!("inflating {} bytes of compressed metadata", compressed_bytes.len());
let mut inflated = Vec::new();
// Assume the decompressed data will be at least the size of the compressed data, so we
// don't have to grow the buffer as much.
let mut inflated = Vec::with_capacity(compressed_bytes.len());
match FrameDecoder::new(compressed_bytes).read_to_end(&mut inflated) {
Ok(_) => rustc_erase_owner!(OwningRef::new(inflated).map_owner_box()),
Err(_) => {
Expand Down
31 changes: 31 additions & 0 deletions compiler/rustc_mir_dataflow/src/impls/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@ use crate::{AnalysisDomain, Backward, GenKill, GenKillAnalysis};
/// exist. See [this `mir-dataflow` test][flow-test] for an example. You almost never want to use
/// this analysis without also looking at the results of [`MaybeBorrowedLocals`].
///
/// ## Field-(in)sensitivity
///
/// As the name suggests, this analysis is field insensitive. If a projection of a variable `x` is
/// assigned to (e.g. `x.0 = 42`), it does not "define" `x` as far as liveness is concerned. In fact,
/// such an assignment is currently marked as a "use" of `x` in an attempt to be maximally
/// conservative.
///
/// ## Enums and `SetDiscriminant`
///
/// Assigning a literal value to an `enum` (e.g. `Option<i32>`), does not result in a simple
/// assignment of the form `_1 = /*...*/` in the MIR. For example, the following assignment to `x`:
///
/// ```
/// x = Some(4);
/// ```
///
/// compiles to this MIR
///
/// ```
/// ((_1 as Some).0: i32) = const 4_i32;
/// discriminant(_1) = 1;
/// ```
///
/// However, `MaybeLiveLocals` **does** mark `x` (`_1`) as "killed" after a statement like this.
/// That's because it treats the `SetDiscriminant` operation as a definition of `x`, even though
/// the writes that actually initialized the locals happened earlier.
///
/// This makes `MaybeLiveLocals` unsuitable for certain classes of optimization normally associated
/// with a live variables analysis, notably dead-store elimination. It's a dirty hack, but it works
/// okay for the generator state transform (currently the main consumuer of this analysis).
///
/// [`MaybeBorrowedLocals`]: super::MaybeBorrowedLocals
/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
Expand Down
42 changes: 36 additions & 6 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;

use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, NestedMetaItem};
use rustc_data_structures::stable_set::FxHashSet;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::{pluralize, struct_span_err, Applicability};
use rustc_feature::{AttributeType, BUILTIN_ATTRIBUTE_MAP};
use rustc_hir as hir;
Expand Down Expand Up @@ -66,6 +66,7 @@ impl CheckAttrVisitor<'tcx> {
target: Target,
item: Option<ItemLike<'_>>,
) {
let mut doc_aliases = FxHashMap::default();
let mut is_valid = true;
let mut specified_inline = None;
let mut seen = FxHashSet::default();
Expand All @@ -79,7 +80,13 @@ impl CheckAttrVisitor<'tcx> {
sym::track_caller => {
self.check_track_caller(hir_id, &attr.span, attrs, span, target)
}
sym::doc => self.check_doc_attrs(attr, hir_id, target, &mut specified_inline),
sym::doc => self.check_doc_attrs(
attr,
hir_id,
target,
&mut specified_inline,
&mut doc_aliases,
),
sym::no_link => self.check_no_link(hir_id, &attr, span, target),
sym::export_name => self.check_export_name(hir_id, &attr, span, target),
sym::rustc_layout_scalar_valid_range_start
Expand Down Expand Up @@ -512,6 +519,7 @@ impl CheckAttrVisitor<'tcx> {
hir_id: HirId,
target: Target,
is_list: bool,
aliases: &mut FxHashMap<String, Span>,
) -> bool {
let tcx = self.tcx;
let err_fn = move |span: Span, msg: &str| {
Expand Down Expand Up @@ -582,17 +590,38 @@ impl CheckAttrVisitor<'tcx> {
if &*item_name.as_str() == doc_alias {
return err_fn(meta.span(), "is the same as the item's name");
}
let span = meta.span();
if let Err(entry) = aliases.try_insert(doc_alias.to_owned(), span) {
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, span, |lint| {
lint.build("doc alias is duplicated")
.span_label(*entry.entry.get(), "first defined here")
.emit();
});
}
true
}

fn check_doc_alias(&self, meta: &NestedMetaItem, hir_id: HirId, target: Target) -> bool {
fn check_doc_alias(
&self,
meta: &NestedMetaItem,
hir_id: HirId,
target: Target,
aliases: &mut FxHashMap<String, Span>,
) -> bool {
if let Some(values) = meta.meta_item_list() {
let mut errors = 0;
for v in values {
match v.literal() {
Some(l) => match l.kind {
LitKind::Str(s, _) => {
if !self.check_doc_alias_value(v, &s.as_str(), hir_id, target, true) {
if !self.check_doc_alias_value(
v,
&s.as_str(),
hir_id,
target,
true,
aliases,
) {
errors += 1;
}
}
Expand Down Expand Up @@ -621,7 +650,7 @@ impl CheckAttrVisitor<'tcx> {
}
errors == 0
} else if let Some(doc_alias) = meta.value_str().map(|s| s.to_string()) {
self.check_doc_alias_value(meta, &doc_alias, hir_id, target, false)
self.check_doc_alias_value(meta, &doc_alias, hir_id, target, false, aliases)
} else {
self.tcx
.sess
Expand Down Expand Up @@ -858,6 +887,7 @@ impl CheckAttrVisitor<'tcx> {
hir_id: HirId,
target: Target,
specified_inline: &mut Option<(bool, Span)>,
aliases: &mut FxHashMap<String, Span>,
) -> bool {
let mut is_valid = true;

Expand All @@ -867,7 +897,7 @@ impl CheckAttrVisitor<'tcx> {
match i_meta.name_or_empty() {
sym::alias
if !self.check_attr_not_crate_level(&meta, hir_id, "alias")
|| !self.check_doc_alias(&meta, hir_id, target) =>
|| !self.check_doc_alias(&meta, hir_id, target, aliases) =>
{
is_valid = false
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_passes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
#![feature(in_band_lifetimes)]
#![feature(format_args_capture)]
#![feature(iter_zip)]
#![feature(nll)]
#![feature(map_try_insert)]
#![feature(min_specialization)]
#![feature(nll)]
#![feature(try_blocks)]
#![recursion_limit = "256"]

Expand Down
23 changes: 15 additions & 8 deletions compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,24 @@ impl IrMaps<'tcx> {
self.capture_info_map.insert(hir_id, Rc::new(cs));
}

fn add_from_pat(&mut self, pat: &hir::Pat<'tcx>) {
fn collect_shorthand_field_ids(&self, pat: &hir::Pat<'tcx>) -> HirIdSet {
// For struct patterns, take note of which fields used shorthand
// (`x` rather than `x: x`).
let mut shorthand_field_ids = HirIdSet::default();
let mut pats = VecDeque::new();
pats.push_back(pat);

while let Some(pat) = pats.pop_front() {
use rustc_hir::PatKind::*;
match &pat.kind {
Binding(.., inner_pat) => {
pats.extend(inner_pat.iter());
}
Struct(_, fields, _) => {
let ids = fields.iter().filter(|f| f.is_shorthand).map(|f| f.pat.hir_id);
shorthand_field_ids.extend(ids);
let (short, not_short): (Vec<&_>, Vec<&_>) =
fields.iter().partition(|f| f.is_shorthand);
shorthand_field_ids.extend(short.iter().map(|f| f.pat.hir_id));
pats.extend(not_short.iter().map(|f| f.pat));
}
Ref(inner_pat, _) | Box(inner_pat) => {
pats.push_back(inner_pat);
Expand All @@ -296,6 +299,12 @@ impl IrMaps<'tcx> {
}
}

return shorthand_field_ids;
}

fn add_from_pat(&mut self, pat: &hir::Pat<'tcx>) {
let shorthand_field_ids = self.collect_shorthand_field_ids(pat);

pat.each_binding(|_, hir_id, _, ident| {
self.add_live_node_for_node(hir_id, VarDefNode(ident.span, hir_id));
self.add_variable(Local(LocalInfo {
Expand Down Expand Up @@ -373,15 +382,13 @@ impl<'tcx> Visitor<'tcx> for IrMaps<'tcx> {
}

fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
let shorthand_field_ids = self.collect_shorthand_field_ids(param.pat);
param.pat.each_binding(|_bm, hir_id, _x, ident| {
let var = match param.pat.kind {
rustc_hir::PatKind::Struct(_, fields, _) => Local(LocalInfo {
rustc_hir::PatKind::Struct(..) => Local(LocalInfo {
id: hir_id,
name: ident.name,
is_shorthand: fields
.iter()
.find(|f| f.ident == ident)
.map_or(false, |f| f.is_shorthand),
is_shorthand: shorthand_field_ids.contains(&hir_id),
}),
_ => Param(hir_id, ident.name),
};
Expand Down
Loading

0 comments on commit 98a5a98

Please sign in to comment.