Skip to content

Commit

Permalink
Auto merge of rust-lang#72627 - Dylan-DPC:rollup-bavnoq5, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Rollup of 6 pull requests

Successful merges:

 - rust-lang#72270 (add a lint against references to packed fields)
 - rust-lang#72294 (JS cleanup)
 - rust-lang#72342 (Warn about unused crate deps)
 - rust-lang#72401 (Use correct function for detecting `const fn` in unsafety checking)
 - rust-lang#72581 (Allow unlabeled breaks from desugared `?` in labeled blocks)
 - rust-lang#72592 (Update books)

Failed merges:

r? @ghost
  • Loading branch information
bors committed May 26, 2020
2 parents 5239f5c + e061c40 commit e533559
Show file tree
Hide file tree
Showing 39 changed files with 682 additions and 327 deletions.
2 changes: 1 addition & 1 deletion src/doc/embedded-book
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
UNUSED_ALLOCATION,
UNUSED_DOC_COMMENTS,
UNUSED_EXTERN_CRATES,
UNUSED_CRATE_DEPENDENCIES,
UNUSED_FEATURES,
UNUSED_LABELS,
UNUSED_PARENS,
Expand Down
29 changes: 29 additions & 0 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob

use rustc_ast::expand::allocator::{global_allocator_spans, AllocatorKind};
use rustc_ast::{ast, attr};
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc;
use rustc_errors::struct_span_err;
Expand All @@ -18,6 +19,7 @@ use rustc_middle::middle::cstore::{
};
use rustc_middle::ty::TyCtxt;
use rustc_session::config::{self, CrateType};
use rustc_session::lint;
use rustc_session::output::validate_crate_name;
use rustc_session::search_paths::PathKind;
use rustc_session::{CrateDisambiguator, Session};
Expand Down Expand Up @@ -49,6 +51,7 @@ pub struct CrateLoader<'a> {
local_crate_name: Symbol,
// Mutable output.
cstore: CStore,
used_extern_options: FxHashSet<Symbol>,
}

pub enum LoadedMacro {
Expand Down Expand Up @@ -205,6 +208,7 @@ impl<'a> CrateLoader<'a> {
allocator_kind: None,
has_global_allocator: false,
},
used_extern_options: Default::default(),
}
}

Expand Down Expand Up @@ -445,6 +449,9 @@ impl<'a> CrateLoader<'a> {
dep_kind: DepKind,
dep: Option<(&'b CratePaths, &'b CrateDep)>,
) -> CrateNum {
if dep.is_none() {
self.used_extern_options.insert(name);
}
self.maybe_resolve_crate(name, span, dep_kind, dep).unwrap_or_else(|err| err.report())
}

Expand Down Expand Up @@ -839,6 +846,26 @@ impl<'a> CrateLoader<'a> {
});
}

fn report_unused_deps(&mut self, krate: &ast::Crate) {
// Make a point span rather than covering the whole file
let span = krate.span.shrink_to_lo();
// Complain about anything left over
for (name, _) in self.sess.opts.externs.iter() {
if !self.used_extern_options.contains(&Symbol::intern(name)) {
self.sess.parse_sess.buffer_lint(
lint::builtin::UNUSED_CRATE_DEPENDENCIES,
span,
ast::CRATE_NODE_ID,
&format!(
"external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`",
name,
self.local_crate_name,
name),
);
}
}
}

pub fn postprocess(&mut self, krate: &ast::Crate) {
self.inject_profiler_runtime();
self.inject_allocator_crate(krate);
Expand All @@ -847,6 +874,8 @@ impl<'a> CrateLoader<'a> {
if log_enabled!(log::Level::Info) {
dump_crates(&self.cstore);
}

self.report_unused_deps(krate);
}

pub fn process_extern_crate(
Expand Down
66 changes: 66 additions & 0 deletions src/librustc_mir/transform/check_packed_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::builtin::UNALIGNED_REFERENCES;

use crate::transform::{MirPass, MirSource};
use crate::util;

pub struct CheckPackedRef;

impl<'tcx> MirPass<'tcx> for CheckPackedRef {
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env(src.instance.def_id());
let source_info = SourceInfo::outermost(body.span);
let mut checker = PackedRefChecker { body, tcx, param_env, source_info };
checker.visit_body(&body);
}
}

struct PackedRefChecker<'a, 'tcx> {
body: &'a Body<'tcx>,
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
source_info: SourceInfo,
}

impl<'a, 'tcx> Visitor<'tcx> for PackedRefChecker<'a, 'tcx> {
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
// Make sure we know where in the MIR we are.
self.source_info = terminator.source_info;
self.super_terminator(terminator, location);
}

fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
// Make sure we know where in the MIR we are.
self.source_info = statement.source_info;
self.super_statement(statement, location);
}

fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
if context.is_borrow() {
if util::is_disaligned(self.tcx, self.body, self.param_env, *place) {
let source_info = self.source_info;
let lint_root = self.body.source_scopes[source_info.scope]
.local_data
.as_ref()
.assert_crate_local()
.lint_root;
self.tcx.struct_span_lint_hir(
UNALIGNED_REFERENCES,
lint_root,
source_info.span,
|lint| {
lint.build(&format!("reference to packed field is unaligned",))
.note(
"fields of packed structs are not properly aligned, and creating \
a misaligned reference is undefined behavior (even if that \
reference is never dereferenced)",
)
.emit()
},
);
}
}
}
}
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_span::symbol::{sym, Symbol};

use std::ops::Bound;

use crate::const_eval::{is_const_fn, is_min_const_fn};
use crate::const_eval::is_min_const_fn;
use crate::util;

pub struct UnsafetyChecker<'a, 'tcx> {
Expand Down Expand Up @@ -527,7 +527,7 @@ fn unsafety_check_result(tcx: TyCtxt<'_>, def_id: LocalDefId) -> UnsafetyCheckRe
let (const_context, min_const_fn) = match tcx.hir().body_owner_kind(id) {
hir::BodyOwnerKind::Closure => (false, false),
hir::BodyOwnerKind::Fn => {
(is_const_fn(tcx, def_id.to_def_id()), is_min_const_fn(tcx, def_id.to_def_id()))
(tcx.is_const_fn_raw(def_id.to_def_id()), is_min_const_fn(tcx, def_id.to_def_id()))
}
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => (true, false),
};
Expand Down
6 changes: 5 additions & 1 deletion src/librustc_mir/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod add_call_guards;
pub mod add_moves_for_packed_drops;
pub mod add_retag;
pub mod check_consts;
pub mod check_packed_ref;
pub mod check_unsafety;
pub mod cleanup_post_borrowck;
pub mod const_prop;
Expand Down Expand Up @@ -228,10 +229,11 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs {
validator.qualifs_in_return_place()
}

/// Make MIR ready for const evaluation. This is run on all MIR, not just on consts!
fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> Steal<Body<'_>> {
let def_id = def_id.expect_local();

// Unsafety check uses the raw mir, so make sure it is run
// Unsafety check uses the raw mir, so make sure it is run.
let _ = tcx.unsafety_check_result(def_id);

let mut body = tcx.mir_built(def_id).steal();
Expand All @@ -247,6 +249,8 @@ fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> Steal<Body<'_>> {
None,
MirPhase::Const,
&[&[
// MIR-level lints.
&check_packed_ref::CheckPackedRef,
// What we need to do constant evaluation.
&simplify::SimplifyCfg::new("initial"),
&rustc_peek::SanityCheck,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_passes/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_middle::hir::map::Map;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::Span;

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -203,7 +204,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
label: &Destination,
cf_type: &str,
) -> bool {
if self.cx == LabeledBlock {
if !span.is_desugaring(DesugaringKind::QuestionMark) && self.cx == LabeledBlock {
if label.label.is_none() {
struct_span_err!(
self.sess,
Expand Down
16 changes: 15 additions & 1 deletion src/librustc_session/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ declare_lint! {
"extern crates that are never used"
}

declare_lint! {
pub UNUSED_CRATE_DEPENDENCIES,
Allow,
"crate dependencies that are never used"
}

declare_lint! {
pub UNUSED_QUALIFICATIONS,
Allow,
Expand Down Expand Up @@ -216,10 +222,16 @@ declare_lint! {
"lints that have been renamed or removed"
}

declare_lint! {
pub UNALIGNED_REFERENCES,
Allow,
"detects unaligned references to fields of packed structs",
}

declare_lint! {
pub SAFE_PACKED_BORROWS,
Warn,
"safe borrows of fields of packed structs were was erroneously allowed",
"safe borrows of fields of packed structs were erroneously allowed",
@future_incompatible = FutureIncompatibleInfo {
reference: "issue #46043 <https://github.com/rust-lang/rust/issues/46043>",
edition: None,
Expand Down Expand Up @@ -523,6 +535,7 @@ declare_lint_pass! {
UNCONDITIONAL_PANIC,
UNUSED_IMPORTS,
UNUSED_EXTERN_CRATES,
UNUSED_CRATE_DEPENDENCIES,
UNUSED_QUALIFICATIONS,
UNKNOWN_LINTS,
UNUSED_VARIABLES,
Expand All @@ -545,6 +558,7 @@ declare_lint_pass! {
INVALID_TYPE_PARAM_DEFAULT,
CONST_ERR,
RENAMED_AND_REMOVED_LINTS,
UNALIGNED_REFERENCES,
SAFE_PACKED_BORROWS,
PATTERNS_IN_FNS_WITHOUT_BODY,
MISSING_FRAGMENT_SPECIFIER,
Expand Down
Loading

0 comments on commit e533559

Please sign in to comment.