Skip to content

Commit 5dc8789

Browse files
committed
Auto merge of #85560 - GuillaumeGomez:rollup-8k90rc7, r=GuillaumeGomez
Rollup of 4 pull requests Successful merges: - #85506 (Reset "focusedByTab" field when doing another search) - #85548 (Remove dead toggle JS code) - #85550 (facepalm: operator precedence fail on my part.) - #85555 (Check for more things in THIR unsafeck) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 9c0379c + 0f48e63 commit 5dc8789

File tree

70 files changed

+651
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+651
-229
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+68-9
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ use rustc_middle::ty::{self, TyCtxt};
77
use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
88
use rustc_session::lint::Level;
99
use rustc_span::def_id::{DefId, LocalDefId};
10+
use rustc_span::symbol::Symbol;
1011
use rustc_span::Span;
1112

13+
use std::ops::Bound;
14+
1215
struct UnsafetyVisitor<'a, 'tcx> {
1316
tcx: TyCtxt<'tcx>,
1417
thir: &'a Thir<'tcx>,
@@ -19,6 +22,10 @@ struct UnsafetyVisitor<'a, 'tcx> {
1922
/// `unsafe` block, and whether it has been used.
2023
safety_context: SafetyContext,
2124
body_unsafety: BodyUnsafety,
25+
/// The `#[target_feature]` attributes of the body. Used for checking
26+
/// calls to functions with `#[target_feature]` (RFC 2396).
27+
body_target_features: &'tcx Vec<Symbol>,
28+
is_const: bool,
2229
}
2330

2431
impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
@@ -148,11 +155,55 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
148155
ExprKind::Call { fun, ty: _, args: _, from_hir_call: _, fn_span: _ } => {
149156
if self.thir[fun].ty.fn_sig(self.tcx).unsafety() == hir::Unsafety::Unsafe {
150157
self.requires_unsafe(expr.span, CallToUnsafeFunction);
158+
} else if let &ty::FnDef(func_did, _) = self.thir[fun].ty.kind() {
159+
// If the called function has target features the calling function hasn't,
160+
// the call requires `unsafe`.
161+
if !self
162+
.tcx
163+
.codegen_fn_attrs(func_did)
164+
.target_features
165+
.iter()
166+
.all(|feature| self.body_target_features.contains(feature))
167+
{
168+
self.requires_unsafe(expr.span, CallToFunctionWith);
169+
}
170+
}
171+
}
172+
ExprKind::Deref { arg } => {
173+
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
174+
if self.tcx.is_mutable_static(def_id) {
175+
self.requires_unsafe(expr.span, UseOfMutableStatic);
176+
} else if self.tcx.is_foreign_item(def_id) {
177+
self.requires_unsafe(expr.span, UseOfExternStatic);
178+
}
179+
} else if self.thir[arg].ty.is_unsafe_ptr() {
180+
self.requires_unsafe(expr.span, DerefOfRawPointer);
151181
}
152182
}
153183
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
154184
self.requires_unsafe(expr.span, UseOfInlineAssembly);
155185
}
186+
ExprKind::Adt {
187+
adt_def,
188+
variant_index: _,
189+
substs: _,
190+
user_ty: _,
191+
fields: _,
192+
base: _,
193+
} => match self.tcx.layout_scalar_valid_range(adt_def.did) {
194+
(Bound::Unbounded, Bound::Unbounded) => {}
195+
_ => self.requires_unsafe(expr.span, InitializingTypeWith),
196+
},
197+
ExprKind::Cast { source } => {
198+
let source = &self.thir[source];
199+
if self.tcx.features().const_raw_ptr_to_usize_cast
200+
&& self.is_const
201+
&& (source.ty.is_unsafe_ptr() || source.ty.is_fn_ptr())
202+
&& expr.ty.is_integral()
203+
{
204+
self.requires_unsafe(expr.span, CastOfPointerToInt);
205+
}
206+
}
156207
_ => {}
157208
}
158209

@@ -195,15 +246,10 @@ impl BodyUnsafety {
195246
enum UnsafeOpKind {
196247
CallToUnsafeFunction,
197248
UseOfInlineAssembly,
198-
#[allow(dead_code)] // FIXME
199249
InitializingTypeWith,
200-
#[allow(dead_code)] // FIXME
201250
CastOfPointerToInt,
202-
#[allow(dead_code)] // FIXME
203251
UseOfMutableStatic,
204-
#[allow(dead_code)] // FIXME
205252
UseOfExternStatic,
206-
#[allow(dead_code)] // FIXME
207253
DerefOfRawPointer,
208254
#[allow(dead_code)] // FIXME
209255
AssignToDroppingUnionField,
@@ -213,7 +259,6 @@ enum UnsafeOpKind {
213259
MutationOfLayoutConstrainedField,
214260
#[allow(dead_code)] // FIXME
215261
BorrowOfLayoutConstrainedField,
216-
#[allow(dead_code)] // FIXME
217262
CallToFunctionWith,
218263
}
219264

@@ -287,6 +332,7 @@ pub fn check_unsafety<'tcx>(
287332
tcx: TyCtxt<'tcx>,
288333
thir: &Thir<'tcx>,
289334
expr: ExprId,
335+
def_id: LocalDefId,
290336
hir_id: hir::HirId,
291337
) {
292338
let body_unsafety = tcx.hir().fn_sig_by_hir_id(hir_id).map_or(BodyUnsafety::Safe, |fn_sig| {
@@ -296,10 +342,23 @@ pub fn check_unsafety<'tcx>(
296342
BodyUnsafety::Safe
297343
}
298344
});
345+
let body_target_features = &tcx.codegen_fn_attrs(def_id).target_features;
299346
let safety_context =
300347
if body_unsafety.is_unsafe() { SafetyContext::UnsafeFn } else { SafetyContext::Safe };
301-
let mut visitor =
302-
UnsafetyVisitor { tcx, thir, safety_context, hir_context: hir_id, body_unsafety };
348+
let is_const = match tcx.hir().body_owner_kind(hir_id) {
349+
hir::BodyOwnerKind::Closure => false,
350+
hir::BodyOwnerKind::Fn => tcx.is_const_fn_raw(def_id.to_def_id()),
351+
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => true,
352+
};
353+
let mut visitor = UnsafetyVisitor {
354+
tcx,
355+
thir,
356+
safety_context,
357+
hir_context: hir_id,
358+
body_unsafety,
359+
body_target_features,
360+
is_const,
361+
};
303362
visitor.visit_expr(&thir[expr]);
304363
}
305364

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

317376
crate fn thir_check_unsafety<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {

src/bootstrap/bin/rustc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn format_rusage_data(_child: Child) -> Option<String> {
305305
};
306306
// Mac OS X reports the maxrss in bytes, not kb.
307307
let divisor = if env::consts::OS == "macos" { 1024 } else { 1 };
308-
let maxrss = rusage.ru_maxrss + (divisor - 1) / divisor;
308+
let maxrss = (rusage.ru_maxrss + (divisor - 1)) / divisor;
309309

310310
let mut init_str = format!(
311311
"user: {USER_SEC}.{USER_USEC:03} \

src/librustdoc/html/static/main.js

-158
Original file line numberDiff line numberDiff line change
@@ -764,138 +764,6 @@ function hideThemeButtonState() {
764764
innerToggle.children[0].innerText = labelForToggleButton(sectionIsCollapsed);
765765
}
766766

767-
function collapseDocs(toggle, mode) {
768-
if (!toggle || !toggle.parentNode) {
769-
return;
770-
}
771-
772-
function adjustToggle(arg) {
773-
return function(e) {
774-
if (hasClass(e, "toggle-label")) {
775-
if (arg) {
776-
e.style.display = "inline-block";
777-
} else {
778-
e.style.display = "none";
779-
}
780-
}
781-
if (hasClass(e, "inner")) {
782-
e.innerHTML = labelForToggleButton(arg);
783-
}
784-
};
785-
}
786-
787-
function implHider(addOrRemove, fullHide) {
788-
return function(n) {
789-
var shouldHide =
790-
fullHide ||
791-
hasClass(n, "method") ||
792-
hasClass(n, "associatedconstant");
793-
if (shouldHide || hasClass(n, "type")) {
794-
if (shouldHide) {
795-
if (addOrRemove) {
796-
addClass(n, "hidden-by-impl-hider");
797-
} else {
798-
removeClass(n, "hidden-by-impl-hider");
799-
}
800-
}
801-
var ns = n.nextElementSibling;
802-
while (ns && (hasClass(ns, "docblock") || hasClass(ns, "item-info"))) {
803-
if (addOrRemove) {
804-
addClass(ns, "hidden-by-impl-hider");
805-
} else {
806-
removeClass(ns, "hidden-by-impl-hider");
807-
}
808-
ns = ns.nextElementSibling;
809-
}
810-
}
811-
};
812-
}
813-
814-
var relatedDoc;
815-
var action = mode;
816-
if (!hasClass(toggle.parentNode, "impl")) {
817-
relatedDoc = toggle.parentNode.nextElementSibling;
818-
if (hasClass(relatedDoc, "item-info")) {
819-
relatedDoc = relatedDoc.nextElementSibling;
820-
}
821-
if (hasClass(relatedDoc, "docblock")) {
822-
if (mode === "toggle") {
823-
if (hasClass(relatedDoc, "hidden-by-usual-hider")) {
824-
action = "show";
825-
} else {
826-
action = "hide";
827-
}
828-
}
829-
if (action === "hide") {
830-
addClass(relatedDoc, "hidden-by-usual-hider");
831-
onEachLazy(toggle.childNodes, adjustToggle(true));
832-
addClass(toggle.parentNode, "collapsed");
833-
} else if (action === "show") {
834-
removeClass(relatedDoc, "hidden-by-usual-hider");
835-
removeClass(toggle.parentNode, "collapsed");
836-
onEachLazy(toggle.childNodes, adjustToggle(false));
837-
}
838-
}
839-
} else {
840-
// we are collapsing the impl block(s).
841-
842-
var parentElem = toggle.parentNode;
843-
relatedDoc = parentElem;
844-
var docblock = relatedDoc.nextElementSibling;
845-
846-
while (!hasClass(relatedDoc, "impl-items")) {
847-
relatedDoc = relatedDoc.nextElementSibling;
848-
}
849-
850-
if (!relatedDoc && !hasClass(docblock, "docblock")) {
851-
return;
852-
}
853-
854-
// Hide all functions, but not associated types/consts.
855-
856-
if (mode === "toggle") {
857-
if (hasClass(relatedDoc, "fns-now-collapsed") ||
858-
hasClass(docblock, "hidden-by-impl-hider")) {
859-
action = "show";
860-
} else {
861-
action = "hide";
862-
}
863-
}
864-
865-
var dontApplyBlockRule = toggle.parentNode.parentNode.id !== "main";
866-
if (action === "show") {
867-
removeClass(relatedDoc, "fns-now-collapsed");
868-
// Stability/deprecation/portability information is never hidden.
869-
if (!hasClass(docblock, "item-info")) {
870-
removeClass(docblock, "hidden-by-usual-hider");
871-
}
872-
onEachLazy(toggle.childNodes, adjustToggle(false, dontApplyBlockRule));
873-
onEachLazy(relatedDoc.childNodes, implHider(false, dontApplyBlockRule));
874-
} else if (action === "hide") {
875-
addClass(relatedDoc, "fns-now-collapsed");
876-
// Stability/deprecation/portability information should be shown even when detailed
877-
// info is hidden.
878-
if (!hasClass(docblock, "item-info")) {
879-
addClass(docblock, "hidden-by-usual-hider");
880-
}
881-
onEachLazy(toggle.childNodes, adjustToggle(true, dontApplyBlockRule));
882-
onEachLazy(relatedDoc.childNodes, implHider(true, dontApplyBlockRule));
883-
}
884-
}
885-
}
886-
887-
function collapseNonInherent(e) {
888-
// inherent impl ids are like "impl" or impl-<number>'.
889-
// they will never be hidden by default.
890-
var n = e.parentElement;
891-
if (n.id.match(/^impl(?:-\d+)?$/) === null) {
892-
// Automatically minimize all non-inherent impls
893-
if (hasClass(n, "impl")) {
894-
collapseDocs(e, "hide");
895-
}
896-
}
897-
}
898-
899767
function insertAfter(newNode, referenceNode) {
900768
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
901769
}
@@ -910,20 +778,6 @@ function hideThemeButtonState() {
910778
var hideImplementors = getSettingValue("auto-collapse-implementors") !== "false";
911779
var hideLargeItemContents = getSettingValue("auto-hide-large-items") !== "false";
912780

913-
var impl_list = document.getElementById("trait-implementations-list");
914-
if (impl_list !== null) {
915-
onEachLazy(impl_list.getElementsByClassName("rustdoc-toggle"), function(e) {
916-
collapseNonInherent(e);
917-
});
918-
}
919-
920-
var blanket_list = document.getElementById("blanket-implementations-list");
921-
if (blanket_list !== null) {
922-
onEachLazy(blanket_list.getElementsByClassName("rustdoc-toggle"), function(e) {
923-
collapseNonInherent(e);
924-
});
925-
}
926-
927781
onEachLazy(document.getElementsByTagName("details"), function (e) {
928782
var showLargeItem = !hideLargeItemContents && hasClass(e, "type-contents-toggle");
929783
var showImplementor = !hideImplementors && hasClass(e, "implementors-toggle");
@@ -936,18 +790,6 @@ function hideThemeButtonState() {
936790

937791
});
938792

939-
var currentType = document.getElementsByClassName("type-decl")[0];
940-
if (currentType) {
941-
currentType = currentType.getElementsByClassName("rust")[0];
942-
if (currentType) {
943-
onEachLazy(currentType.classList, function(item) {
944-
if (item !== "main") {
945-
return true;
946-
}
947-
});
948-
}
949-
}
950-
951793
var pageId = getPageId();
952794
if (pageId !== null) {
953795
expandSection(pageId);

src/librustdoc/html/static/search.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -885,12 +885,12 @@ window.initSearch = function(rawSearchIndex) {
885885
focusSearchResult();
886886
}
887887

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

10781078
search.innerHTML = output;
1079+
// Reset focused elements.
1080+
searchState.focusedByTab = [null, null, null];
10791081
searchState.showResults(search);
10801082
var elems = document.getElementById("titles").childNodes;
10811083
elems[0].onclick = function() { printTab(0); };
@@ -1365,7 +1367,6 @@ window.initSearch = function(rawSearchIndex) {
13651367
if (e.which === 38) { // up
13661368
var previous = document.activeElement.previousElementSibling;
13671369
if (previous) {
1368-
console.log("previousElementSibling", previous);
13691370
previous.focus();
13701371
} else {
13711372
searchState.focus();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
2+
--> $DIR/cast-ptr-to-int-const.rs:10:9
3+
|
4+
LL | &Y as *const u32 as usize
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
6+
|
7+
= note: casting pointers to integers in constants
8+
9+
error[E0133]: cast of pointer to int is unsafe and requires unsafe function or block
10+
--> $DIR/cast-ptr-to-int-const.rs:17:5
11+
|
12+
LL | &0 as *const i32 as usize
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cast of pointer to int
14+
|
15+
= note: casting pointers to integers in constants
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)