Skip to content

Commit

Permalink
Auto merge of rust-lang#85560 - GuillaumeGomez:rollup-8k90rc7, r=Guil…
Browse files Browse the repository at this point in the history
…laumeGomez

Rollup of 4 pull requests

Successful merges:

 - rust-lang#85506 (Reset "focusedByTab" field when doing another search)
 - rust-lang#85548 (Remove dead toggle JS code)
 - rust-lang#85550 (facepalm: operator precedence fail on my part.)
 - rust-lang#85555 (Check for more things in THIR unsafeck)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed May 21, 2021
2 parents 9c0379c + 0f48e63 commit 5dc8789
Show file tree
Hide file tree
Showing 70 changed files with 651 additions and 229 deletions.
77 changes: 68 additions & 9 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
use rustc_session::lint::Level;
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::symbol::Symbol;
use rustc_span::Span;

use std::ops::Bound;

struct UnsafetyVisitor<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
thir: &'a Thir<'tcx>,
Expand All @@ -19,6 +22,10 @@ struct UnsafetyVisitor<'a, 'tcx> {
/// `unsafe` block, and whether it has been used.
safety_context: SafetyContext,
body_unsafety: BodyUnsafety,
/// The `#[target_feature]` attributes of the body. Used for checking
/// calls to functions with `#[target_feature]` (RFC 2396).
body_target_features: &'tcx Vec<Symbol>,
is_const: bool,
}

impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
Expand Down Expand Up @@ -148,11 +155,55 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
ExprKind::Call { fun, ty: _, args: _, from_hir_call: _, fn_span: _ } => {
if self.thir[fun].ty.fn_sig(self.tcx).unsafety() == hir::Unsafety::Unsafe {
self.requires_unsafe(expr.span, CallToUnsafeFunction);
} else if let &ty::FnDef(func_did, _) = self.thir[fun].ty.kind() {
// If the called function has target features the calling function hasn't,
// the call requires `unsafe`.
if !self
.tcx
.codegen_fn_attrs(func_did)
.target_features
.iter()
.all(|feature| self.body_target_features.contains(feature))
{
self.requires_unsafe(expr.span, CallToFunctionWith);
}
}
}
ExprKind::Deref { arg } => {
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
if self.tcx.is_mutable_static(def_id) {
self.requires_unsafe(expr.span, UseOfMutableStatic);
} else if self.tcx.is_foreign_item(def_id) {
self.requires_unsafe(expr.span, UseOfExternStatic);
}
} else if self.thir[arg].ty.is_unsafe_ptr() {
self.requires_unsafe(expr.span, DerefOfRawPointer);
}
}
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
self.requires_unsafe(expr.span, UseOfInlineAssembly);
}
ExprKind::Adt {
adt_def,
variant_index: _,
substs: _,
user_ty: _,
fields: _,
base: _,
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
(Bound::Unbounded, Bound::Unbounded) => {}
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
},
ExprKind::Cast { source } => {
let source = &self.thir[source];
if self.tcx.features().const_raw_ptr_to_usize_cast
&& self.is_const
&& (source.ty.is_unsafe_ptr() || source.ty.is_fn_ptr())
&& expr.ty.is_integral()
{
self.requires_unsafe(expr.span, CastOfPointerToInt);
}
}
_ => {}
}

Expand Down Expand Up @@ -195,15 +246,10 @@ impl BodyUnsafety {
enum UnsafeOpKind {
CallToUnsafeFunction,
UseOfInlineAssembly,
#[allow(dead_code)] // FIXME
InitializingTypeWith,
#[allow(dead_code)] // FIXME
CastOfPointerToInt,
#[allow(dead_code)] // FIXME
UseOfMutableStatic,
#[allow(dead_code)] // FIXME
UseOfExternStatic,
#[allow(dead_code)] // FIXME
DerefOfRawPointer,
#[allow(dead_code)] // FIXME
AssignToDroppingUnionField,
Expand All @@ -213,7 +259,6 @@ enum UnsafeOpKind {
MutationOfLayoutConstrainedField,
#[allow(dead_code)] // FIXME
BorrowOfLayoutConstrainedField,
#[allow(dead_code)] // FIXME
CallToFunctionWith,
}

Expand Down Expand Up @@ -287,6 +332,7 @@ pub fn check_unsafety<'tcx>(
tcx: TyCtxt<'tcx>,
thir: &Thir<'tcx>,
expr: ExprId,
def_id: LocalDefId,
hir_id: hir::HirId,
) {
let body_unsafety = tcx.hir().fn_sig_by_hir_id(hir_id).map_or(BodyUnsafety::Safe, |fn_sig| {
Expand All @@ -296,10 +342,23 @@ pub fn check_unsafety<'tcx>(
BodyUnsafety::Safe
}
});
let body_target_features = &tcx.codegen_fn_attrs(def_id).target_features;
let safety_context =
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
let mut visitor =
UnsafetyVisitor { tcx, thir, safety_context, hir_context: hir_id, body_unsafety };
let is_const = match tcx.hir().body_owner_kind(hir_id) {
hir::BodyOwnerKind::Closure => false,
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def_id.to_def_id()),
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
};
let mut visitor = UnsafetyVisitor {
tcx,
thir,
safety_context,
hir_context: hir_id,
body_unsafety,
body_target_features,
is_const,
};
visitor.visit_expr(&thir[expr]);
}

Expand All @@ -311,7 +370,7 @@ crate fn thir_check_unsafety_inner<'tcx>(
let body_id = tcx.hir().body_owned_by(hir_id);
let body = tcx.hir().body(body_id);
let (thir, expr) = cx::build_thir(tcx, def, &body.value);
check_unsafety(tcx, &thir, expr, hir_id);
check_unsafety(tcx, &thir, expr, def.did, hir_id);
}

crate fn thir_check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/bin/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ fn format_rusage_data(_child: Child) -> Option<String> {
};
// Mac OS X reports the maxrss in bytes, not kb.
let divisor = if env::consts::OS == "macos" { 1024 } else { 1 };
let maxrss = rusage.ru_maxrss + (divisor - 1) / divisor;
let maxrss = (rusage.ru_maxrss + (divisor - 1)) / divisor;

let mut init_str = format!(
"user: {USER_SEC}.{USER_USEC:03} \
Expand Down
158 changes: 0 additions & 158 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,138 +764,6 @@ function hideThemeButtonState() {
innerToggle.children[0].innerText = labelForToggleButton(sectionIsCollapsed);
}

function collapseDocs(toggle, mode) {
if (!toggle || !toggle.parentNode) {
return;
}

function adjustToggle(arg) {
return function(e) {
if (hasClass(e, "toggle-label")) {
if (arg) {
e.style.display = "inline-block";
} else {
e.style.display = "none";
}
}
if (hasClass(e, "inner")) {
e.innerHTML = labelForToggleButton(arg);
}
};
}

function implHider(addOrRemove, fullHide) {
return function(n) {
var shouldHide =
fullHide ||
hasClass(n, "method") ||
hasClass(n, "associatedconstant");
if (shouldHide || hasClass(n, "type")) {
if (shouldHide) {
if (addOrRemove) {
addClass(n, "hidden-by-impl-hider");
} else {
removeClass(n, "hidden-by-impl-hider");
}
}
var ns = n.nextElementSibling;
while (ns && (hasClass(ns, "docblock") || hasClass(ns, "item-info"))) {
if (addOrRemove) {
addClass(ns, "hidden-by-impl-hider");
} else {
removeClass(ns, "hidden-by-impl-hider");
}
ns = ns.nextElementSibling;
}
}
};
}

var relatedDoc;
var action = mode;
if (!hasClass(toggle.parentNode, "impl")) {
relatedDoc = toggle.parentNode.nextElementSibling;
if (hasClass(relatedDoc, "item-info")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if (hasClass(relatedDoc, "docblock")) {
if (mode === "toggle") {
if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
action = "show";
} else {
action = "hide";
}
}
if (action === "hide") {
addClass(relatedDoc, "hidden-by-usual-hider");
onEachLazy(toggle.childNodes, adjustToggle(true));
addClass(toggle.parentNode, "collapsed");
} else if (action === "show") {
removeClass(relatedDoc, "hidden-by-usual-hider");
removeClass(toggle.parentNode, "collapsed");
onEachLazy(toggle.childNodes, adjustToggle(false));
}
}
} else {
// we are collapsing the impl block(s).

var parentElem = toggle.parentNode;
relatedDoc = parentElem;
var docblock = relatedDoc.nextElementSibling;

while (!hasClass(relatedDoc, "impl-items")) {
relatedDoc = relatedDoc.nextElementSibling;
}

if (!relatedDoc && !hasClass(docblock, "docblock")) {
return;
}

// Hide all functions, but not associated types/consts.

if (mode === "toggle") {
if (hasClass(relatedDoc, "fns-now-collapsed") ||
hasClass(docblock, "hidden-by-impl-hider")) {
action = "show";
} else {
action = "hide";
}
}

var dontApplyBlockRule = toggle.parentNode.parentNode.id !== "main";
if (action === "show") {
removeClass(relatedDoc, "fns-now-collapsed");
// Stability/deprecation/portability information is never hidden.
if (!hasClass(docblock, "item-info")) {
removeClass(docblock, "hidden-by-usual-hider");
}
onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
onEachLazy(relatedDoc.childNodes, implHider(false, dontApplyBlockRule));
} else if (action === "hide") {
addClass(relatedDoc, "fns-now-collapsed");
// Stability/deprecation/portability information should be shown even when detailed
// info is hidden.
if (!hasClass(docblock, "item-info")) {
addClass(docblock, "hidden-by-usual-hider");
}
onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
onEachLazy(relatedDoc.childNodes, implHider(true, dontApplyBlockRule));
}
}
}

function collapseNonInherent(e) {
// inherent impl ids are like "impl" or impl-<number>'.
// they will never be hidden by default.
var n = e.parentElement;
if (n.id.match(/^impl(?:-\d+)?$/) === null) {
// Automatically minimize all non-inherent impls
if (hasClass(n, "impl")) {
collapseDocs(e, "hide");
}
}
}

function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
Expand All @@ -910,20 +778,6 @@ function hideThemeButtonState() {
var hideImplementors = getSettingValue("auto-collapse-implementors") !== "false";
var hideLargeItemContents = getSettingValue("auto-hide-large-items") !== "false";

var impl_list = document.getElementById("trait-implementations-list");
if (impl_list !== null) {
onEachLazy(impl_list.getElementsByClassName("rustdoc-toggle"), function(e) {
collapseNonInherent(e);
});
}

var blanket_list = document.getElementById("blanket-implementations-list");
if (blanket_list !== null) {
onEachLazy(blanket_list.getElementsByClassName("rustdoc-toggle"), function(e) {
collapseNonInherent(e);
});
}

onEachLazy(document.getElementsByTagName("details"), function (e) {
var showLargeItem = !hideLargeItemContents && hasClass(e, "type-contents-toggle");
var showImplementor = !hideImplementors && hasClass(e, "implementors-toggle");
Expand All @@ -936,18 +790,6 @@ function hideThemeButtonState() {

});

var currentType = document.getElementsByClassName("type-decl")[0];
if (currentType) {
currentType = currentType.getElementsByClassName("rust")[0];
if (currentType) {
onEachLazy(currentType.classList, function(item) {
if (item !== "main") {
return true;
}
});
}
}

var pageId = getPageId();
if (pageId !== null) {
expandSection(pageId);
Expand Down
9 changes: 5 additions & 4 deletions src/librustdoc/html/static/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,12 +885,12 @@ window.initSearch = function(rawSearchIndex) {
focusSearchResult();
}

// focus the first search result on the active tab, or the result that
// Focus the first search result on the active tab, or the result that
// was focused last time this tab was active.
function focusSearchResult() {
var target = searchState.focusedByTab[searchState.currentTab] ||
document.querySelectorAll(".search-results.active a").item(0) ||
document.querySelectorAll("#titles > button").item(searchState.currentTab);
document.querySelectorAll(".search-results.active a").item(0) ||
document.querySelectorAll("#titles > button").item(searchState.currentTab);
if (target) {
target.focus();
}
Expand Down Expand Up @@ -1076,6 +1076,8 @@ window.initSearch = function(rawSearchIndex) {
ret_others[0] + ret_in_args[0] + ret_returned[0] + "</div>";

search.innerHTML = output;
// Reset focused elements.
searchState.focusedByTab = [null, null, null];
searchState.showResults(search);
var elems = document.getElementById("titles").childNodes;
elems[0].onclick = function() { printTab(0); };
Expand Down Expand Up @@ -1365,7 +1367,6 @@ window.initSearch = function(rawSearchIndex) {
if (e.which === 38) { // up
var previous = document.activeElement.previousElementSibling;
if (previous) {
console.log("previousElementSibling", previous);
previous.focus();
} else {
searchState.focus();
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/cast/cast-ptr-to-int-const.mir.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
--> $DIR/cast-ptr-to-int-const.rs:10:9
|
LL | &Y as *const u32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
|
= note: casting pointers to integers in constants

error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
--> $DIR/cast-ptr-to-int-const.rs:17:5
|
LL | &0 as *const i32 as usize
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
|
= note: casting pointers to integers in constants

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0133`.
Loading

0 comments on commit 5dc8789

Please sign in to comment.