Skip to content

Commit 3fdd578

Browse files
committed
Auto merge of rust-lang#101115 - matthiaskrgr:rollup-iy14ztr, r=matthiaskrgr
Rollup of 13 pull requests Successful merges: - rust-lang#97015 (std::io: migrate ReadBuf to BorrowBuf/BorrowCursor) - rust-lang#98301 (Add GDB/LLDB pretty-printers for NonZero types) - rust-lang#99570 (Box::from(slice): Clarify that contents are copied) - rust-lang#100296 (Add standard C error function aliases to last_os_error) - rust-lang#100520 (Add mention of `BufReader` in `Read::bytes` docs) - rust-lang#100885 (Export Cancel from std::os::fortanix_sgx::usercalls::raw) - rust-lang#100955 (Some papercuts on error::Error) - rust-lang#101002 (Provide structured suggestion for `hashmap[idx] = val`) - rust-lang#101038 (no alignment check during interning) - rust-lang#101055 (Use smaller span for suggestions) - rust-lang#101091 (Extend attrs if local_def_id exists) - rust-lang#101098 (rustc_middle: Remove `Visibility::Invisible`) - rust-lang#101102 (unstable-book-gen: use std::fs::write) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents d5ef528 + b3a4bc5 commit 3fdd578

File tree

66 files changed

+936
-610
lines changed

Some content is hidden

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

66 files changed

+936
-610
lines changed

compiler/rustc_attr/src/session_diagnostics.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,12 @@ impl<'a> SessionDiagnostic<'a> for UnsupportedLiteral {
223223
error_code!(E0565),
224224
);
225225
if self.is_bytestr {
226-
if let Ok(lint_str) = sess.source_map().span_to_snippet(self.span) {
227-
diag.span_suggestion(
228-
self.span,
229-
fluent::attr::unsupported_literal_suggestion,
230-
&lint_str[1..],
231-
Applicability::MaybeIncorrect,
232-
);
233-
}
226+
diag.span_suggestion(
227+
sess.source_map().start_point(self.span),
228+
fluent::attr::unsupported_literal_suggestion,
229+
"",
230+
Applicability::MaybeIncorrect,
231+
);
234232
}
235233
diag
236234
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+113-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
1+
use rustc_errors::{
2+
Applicability, Diagnostic, DiagnosticBuilder, EmissionGuarantee, ErrorGuaranteed,
3+
};
24
use rustc_hir as hir;
5+
use rustc_hir::intravisit::Visitor;
36
use rustc_hir::Node;
47
use rustc_middle::hir::map::Map;
58
use rustc_middle::mir::{Mutability, Place, PlaceRef, ProjectionElem};
@@ -614,7 +617,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
614617
"trait `IndexMut` is required to modify indexed content, \
615618
but it is not implemented for `{ty}`",
616619
));
617-
self.suggest_map_index_mut_alternatives(ty, &mut err);
620+
self.suggest_map_index_mut_alternatives(ty, &mut err, span);
618621
}
619622
_ => (),
620623
}
@@ -632,13 +635,120 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
632635
&self,
633636
ty: Ty<'_>,
634637
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
638+
span: Span,
635639
) {
636640
let Some(adt) = ty.ty_adt_def() else { return };
637641
let did = adt.did();
638642
if self.infcx.tcx.is_diagnostic_item(sym::HashMap, did)
639643
|| self.infcx.tcx.is_diagnostic_item(sym::BTreeMap, did)
640644
{
641-
err.help(format!("to modify a `{ty}`, use `.get_mut()`, `.insert()` or the entry API"));
645+
struct V<'a, 'b, 'tcx, G: EmissionGuarantee> {
646+
assign_span: Span,
647+
err: &'a mut DiagnosticBuilder<'b, G>,
648+
ty: Ty<'tcx>,
649+
suggested: bool,
650+
}
651+
impl<'a, 'b: 'a, 'hir, 'tcx, G: EmissionGuarantee> Visitor<'hir> for V<'a, 'b, 'tcx, G> {
652+
fn visit_stmt(&mut self, stmt: &'hir hir::Stmt<'hir>) {
653+
hir::intravisit::walk_stmt(self, stmt);
654+
let expr = match stmt.kind {
655+
hir::StmtKind::Semi(expr) | hir::StmtKind::Expr(expr) => expr,
656+
hir::StmtKind::Local(hir::Local { init: Some(expr), .. }) => expr,
657+
_ => {
658+
return;
659+
}
660+
};
661+
if let hir::ExprKind::Assign(place, rv, _sp) = expr.kind
662+
&& let hir::ExprKind::Index(val, index) = place.kind
663+
&& (expr.span == self.assign_span || place.span == self.assign_span)
664+
{
665+
// val[index] = rv;
666+
// ---------- place
667+
self.err.multipart_suggestions(
668+
&format!(
669+
"to modify a `{}`, use `.get_mut()`, `.insert()` or the entry API",
670+
self.ty,
671+
),
672+
vec![
673+
vec![ // val.insert(index, rv);
674+
(
675+
val.span.shrink_to_hi().with_hi(index.span.lo()),
676+
".insert(".to_string(),
677+
),
678+
(
679+
index.span.shrink_to_hi().with_hi(rv.span.lo()),
680+
", ".to_string(),
681+
),
682+
(rv.span.shrink_to_hi(), ")".to_string()),
683+
],
684+
vec![ // val.get_mut(index).map(|v| { *v = rv; });
685+
(
686+
val.span.shrink_to_hi().with_hi(index.span.lo()),
687+
".get_mut(".to_string(),
688+
),
689+
(
690+
index.span.shrink_to_hi().with_hi(place.span.hi()),
691+
").map(|val| { *val".to_string(),
692+
),
693+
(
694+
rv.span.shrink_to_hi(),
695+
"; })".to_string(),
696+
),
697+
],
698+
vec![ // let x = val.entry(index).or_insert(rv);
699+
(val.span.shrink_to_lo(), "let val = ".to_string()),
700+
(
701+
val.span.shrink_to_hi().with_hi(index.span.lo()),
702+
".entry(".to_string(),
703+
),
704+
(
705+
index.span.shrink_to_hi().with_hi(rv.span.lo()),
706+
").or_insert(".to_string(),
707+
),
708+
(rv.span.shrink_to_hi(), ")".to_string()),
709+
],
710+
].into_iter(),
711+
Applicability::MachineApplicable,
712+
);
713+
self.suggested = true;
714+
} else if let hir::ExprKind::MethodCall(_path, args @ [_, ..], sp) = expr.kind
715+
&& let hir::ExprKind::Index(val, index) = args[0].kind
716+
&& expr.span == self.assign_span
717+
{
718+
// val[index].path(args..);
719+
self.err.multipart_suggestion(
720+
&format!("to modify a `{}` use `.get_mut()`", self.ty),
721+
vec![
722+
(
723+
val.span.shrink_to_hi().with_hi(index.span.lo()),
724+
".get_mut(".to_string(),
725+
),
726+
(
727+
index.span.shrink_to_hi().with_hi(args[0].span.hi()),
728+
").map(|val| val".to_string(),
729+
),
730+
(sp.shrink_to_hi(), ")".to_string()),
731+
],
732+
Applicability::MachineApplicable,
733+
);
734+
self.suggested = true;
735+
}
736+
}
737+
}
738+
let hir_map = self.infcx.tcx.hir();
739+
let def_id = self.body.source.def_id();
740+
let hir_id = hir_map.local_def_id_to_hir_id(def_id.as_local().unwrap());
741+
let node = hir_map.find(hir_id);
742+
let Some(hir::Node::Item(item)) = node else { return; };
743+
let hir::ItemKind::Fn(.., body_id) = item.kind else { return; };
744+
let body = self.infcx.tcx.hir().body(body_id);
745+
let mut v = V { assign_span: span, err, ty, suggested: false };
746+
v.visit_body(body);
747+
if !v.suggested {
748+
err.help(&format!(
749+
"to modify a `{ty}`, use `.get_mut()`, `.insert()` or the entry API",
750+
));
751+
}
642752
}
643753
}
644754

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -1500,24 +1500,18 @@ fn vcall_visibility_metadata<'ll, 'tcx>(
15001500
// If there is not LTO and the visibility in public, we have to assume that the vtable can
15011501
// be seen from anywhere. With multiple CGUs, the vtable is quasi-public.
15021502
(Lto::No | Lto::ThinLocal, Visibility::Public, _)
1503-
| (Lto::No, Visibility::Restricted(_) | Visibility::Invisible, false) => {
1504-
VCallVisibility::Public
1505-
}
1503+
| (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
15061504
// With LTO and a quasi-public visibility, the usages of the functions of the vtable are
15071505
// all known by the `LinkageUnit`.
15081506
// FIXME: LLVM only supports this optimization for `Lto::Fat` currently. Once it also
15091507
// supports `Lto::Thin` the `VCallVisibility` may have to be adjusted for those.
15101508
(Lto::Fat | Lto::Thin, Visibility::Public, _)
1511-
| (
1512-
Lto::ThinLocal | Lto::Thin | Lto::Fat,
1513-
Visibility::Restricted(_) | Visibility::Invisible,
1514-
false,
1515-
) => VCallVisibility::LinkageUnit,
1509+
| (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1510+
VCallVisibility::LinkageUnit
1511+
}
15161512
// If there is only one CGU, private vtables can only be seen by that CGU/translation unit
15171513
// and therefore we know of all usages of functions in the vtable.
1518-
(_, Visibility::Restricted(_) | Visibility::Invisible, true) => {
1519-
VCallVisibility::TranslationUnit
1520-
}
1514+
(_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
15211515
};
15221516

15231517
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ fn eval_body_using_ecx<'mir, 'tcx>(
7474
None => InternKind::Constant,
7575
}
7676
};
77+
ecx.machine.check_alignment = false; // interning doesn't need to respect alignment
7778
intern_const_alloc_recursive(ecx, intern_kind, &ret)?;
79+
// we leave alignment checks off, since this `ecx` will not be used for further evaluation anyway
7880

7981
debug!("eval_body_using_ecx done: {:?}", *ret);
8082
Ok(ret)

compiler/rustc_const_eval/src/const_eval/machine.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
8989
/// exhaustion error.
9090
///
9191
/// Setting this to `0` disables the limit and allows the interpreter to run forever.
92-
pub steps_remaining: usize,
92+
pub(super) steps_remaining: usize,
9393

9494
/// The virtual call stack.
95-
pub(crate) stack: Vec<Frame<'mir, 'tcx, AllocId, ()>>,
95+
pub(super) stack: Vec<Frame<'mir, 'tcx, AllocId, ()>>,
9696

9797
/// We need to make sure consts never point to anything mutable, even recursively. That is
9898
/// relied on for pattern matching on consts with references.
@@ -103,7 +103,7 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
103103
pub(super) can_access_statics: bool,
104104

105105
/// Whether to check alignment during evaluation.
106-
check_alignment: bool,
106+
pub(super) check_alignment: bool,
107107
}
108108

109109
impl<'mir, 'tcx> CompileTimeInterpreter<'mir, 'tcx> {

compiler/rustc_middle/src/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ fn skip_stability_check_due_to_privacy(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
293293

294294
// These are not visible outside crate; therefore
295295
// stability markers are irrelevant, if even present.
296-
ty::Visibility::Restricted(..) | ty::Visibility::Invisible => true,
296+
ty::Visibility::Restricted(..) => true,
297297
}
298298
}
299299

compiler/rustc_middle/src/ty/inhabitedness/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,10 @@ impl<'tcx> FieldDef {
169169
param_env: ty::ParamEnv<'tcx>,
170170
) -> DefIdForest<'tcx> {
171171
let data_uninhabitedness = move || self.ty(tcx, substs).uninhabited_from(tcx, param_env);
172-
// FIXME(canndrew): Currently enum fields are (incorrectly) stored with
173-
// `Visibility::Invisible` so we need to override `self.vis` if we're
174-
// dealing with an enum.
175172
if is_enum {
176173
data_uninhabitedness()
177174
} else {
178175
match self.vis {
179-
Visibility::Invisible => DefIdForest::empty(),
180176
Visibility::Restricted(from) => {
181177
let forest = DefIdForest::from_id(from);
182178
let iter = Some(forest).into_iter().chain(Some(data_uninhabitedness()));

compiler/rustc_middle/src/ty/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ pub enum Visibility {
268268
Public,
269269
/// Visible only in the given crate-local module.
270270
Restricted(DefId),
271-
/// Not visible anywhere in the local crate. This is the visibility of private external items.
272-
Invisible,
273271
}
274272

275273
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable)]
@@ -366,8 +364,6 @@ impl Visibility {
366364
let restriction = match self {
367365
// Public items are visible everywhere.
368366
Visibility::Public => return true,
369-
// Private items from other crates are visible nowhere.
370-
Visibility::Invisible => return false,
371367
// Restricted items are visible in an arbitrary local module.
372368
Visibility::Restricted(other) if other.krate != module.krate => return false,
373369
Visibility::Restricted(module) => module,
@@ -380,7 +376,6 @@ impl Visibility {
380376
pub fn is_at_least<T: DefIdTree>(self, vis: Visibility, tree: T) -> bool {
381377
let vis_restriction = match vis {
382378
Visibility::Public => return self == Visibility::Public,
383-
Visibility::Invisible => return true,
384379
Visibility::Restricted(module) => module,
385380
};
386381

@@ -392,7 +387,6 @@ impl Visibility {
392387
match self {
393388
Visibility::Public => true,
394389
Visibility::Restricted(def_id) => def_id.is_local(),
395-
Visibility::Invisible => false,
396390
}
397391
}
398392

compiler/rustc_privacy/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,6 @@ impl SearchInterfaceForPrivateItemsVisitor<'_> {
17311731
if !vis.is_at_least(self.required_visibility, self.tcx) {
17321732
let vis_descr = match vis {
17331733
ty::Visibility::Public => "public",
1734-
ty::Visibility::Invisible => "private",
17351734
ty::Visibility::Restricted(vis_def_id) => {
17361735
if vis_def_id == self.tcx.parent_module(hir_id).to_def_id() {
17371736
"private"

compiler/rustc_resolve/src/build_reduced_graph.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
380380
has_attributes: !item.attrs.is_empty(),
381381
root_span,
382382
root_id,
383-
vis: Cell::new(vis),
383+
vis: Cell::new(Some(vis)),
384384
used: Cell::new(false),
385385
});
386386

@@ -588,7 +588,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
588588
ast::UseTreeKind::Glob => {
589589
let kind = ImportKind::Glob {
590590
is_prelude: self.r.session.contains_name(&item.attrs, sym::prelude_import),
591-
max_vis: Cell::new(ty::Visibility::Invisible),
591+
max_vis: Cell::new(None),
592592
};
593593
self.r.visibilities.insert(self.r.local_def_id(id), vis);
594594
self.add_import(prefix, kind, use_tree.span, id, item, root_span, item.id, vis);
@@ -650,7 +650,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
650650
true,
651651
// The whole `use` item
652652
item,
653-
ty::Visibility::Invisible,
653+
ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()),
654654
root_span,
655655
);
656656
}
@@ -885,7 +885,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
885885
root_span: item.span,
886886
span: item.span,
887887
module_path: Vec::new(),
888-
vis: Cell::new(vis),
888+
vis: Cell::new(Some(vis)),
889889
used: Cell::new(used),
890890
});
891891
self.r.potentially_unused_imports.push(import);
@@ -1118,7 +1118,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
11181118
root_span: span,
11191119
span,
11201120
module_path: Vec::new(),
1121-
vis: Cell::new(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id())),
1121+
vis: Cell::new(Some(ty::Visibility::Restricted(CRATE_DEF_ID.to_def_id()))),
11221122
used: Cell::new(false),
11231123
})
11241124
};

compiler/rustc_resolve/src/check_unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl Resolver<'_> {
227227
for import in self.potentially_unused_imports.iter() {
228228
match import.kind {
229229
_ if import.used.get()
230-
|| import.vis.get().is_public()
230+
|| import.expect_vis().is_public()
231231
|| import.span.is_dummy() =>
232232
{
233233
if let ImportKind::MacroUse = import.kind {

compiler/rustc_resolve/src/ident.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,10 @@ impl<'a> Resolver<'a> {
953953
// Check if one of single imports can still define the name,
954954
// if it can then our result is not determined and can be invalidated.
955955
for single_import in &resolution.single_imports {
956-
if !self.is_accessible_from(single_import.vis.get(), parent_scope.module) {
956+
let Some(import_vis) = single_import.vis.get() else {
957+
continue;
958+
};
959+
if !self.is_accessible_from(import_vis, parent_scope.module) {
957960
continue;
958961
}
959962
let Some(module) = single_import.imported_module.get() else {
@@ -1018,7 +1021,10 @@ impl<'a> Resolver<'a> {
10181021
// Check if one of glob imports can still define the name,
10191022
// if it can then our "no resolution" result is not determined and can be invalidated.
10201023
for glob_import in module.globs.borrow().iter() {
1021-
if !self.is_accessible_from(glob_import.vis.get(), parent_scope.module) {
1024+
let Some(import_vis) = glob_import.vis.get() else {
1025+
continue;
1026+
};
1027+
if !self.is_accessible_from(import_vis, parent_scope.module) {
10221028
continue;
10231029
}
10241030
let module = match glob_import.imported_module.get() {

0 commit comments

Comments
 (0)