Skip to content

Commit 6580010

Browse files
committed
Auto merge of rust-lang#102234 - matthiaskrgr:rollup-5cb20l1, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#100823 (Refactor some `std` code that works with pointer offstes) - rust-lang#102088 (Fix wrongly refactored Lift impl) - rust-lang#102109 (resolve: Set effective visibilities for imports more precisely) - rust-lang#102186 (Add const_closure, Constify Try trait) - rust-lang#102203 (rustdoc: remove no-op CSS `#source-sidebar { z-index }`) - rust-lang#102204 (Make `ManuallyDrop` satisfy `~const Destruct`) - rust-lang#102210 (diagnostics: avoid syntactically invalid suggestion in if conditionals) - rust-lang#102226 (bootstrap/miri: switch to non-deprecated env var for setting the sysroot folder) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents cdb76db + 6900638 commit 6580010

File tree

24 files changed

+310
-102
lines changed

24 files changed

+310
-102
lines changed

compiler/rustc_middle/src/ty/structural_impls.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ impl<'tcx, A: Lift<'tcx>, B: Lift<'tcx>, C: Lift<'tcx>> Lift<'tcx> for (A, B, C)
272272
impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Option<T> {
273273
type Lifted = Option<T::Lifted>;
274274
fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
275-
tcx.lift(self?).map(Some)
275+
Some(match self {
276+
Some(x) => Some(tcx.lift(x)?),
277+
None => None,
278+
})
276279
}
277280
}
278281

compiler/rustc_resolve/src/access_levels.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::imports::ImportKind;
21
use crate::NameBinding;
32
use crate::NameBindingKind;
43
use crate::Resolver;
@@ -54,15 +53,11 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
5453
// sets the rest of the `use` chain to `AccessLevel::Exported` until
5554
// we hit the actual exported item.
5655
let set_import_binding_access_level =
57-
|this: &mut Self, mut binding: &NameBinding<'a>, mut access_level| {
56+
|this: &mut Self, mut binding: &NameBinding<'a>, mut access_level, ns| {
5857
while let NameBindingKind::Import { binding: nested_binding, import, .. } =
5958
binding.kind
6059
{
61-
this.set_access_level(import.id, access_level);
62-
if let ImportKind::Single { additional_ids, .. } = import.kind {
63-
this.set_access_level(additional_ids.0, access_level);
64-
this.set_access_level(additional_ids.1, access_level);
65-
}
60+
this.set_access_level(this.r.import_id_for_ns(import, ns), access_level);
6661

6762
access_level = Some(AccessLevel::Exported);
6863
binding = nested_binding;
@@ -72,11 +67,11 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
7267
let module = self.r.get_module(module_id.to_def_id()).unwrap();
7368
let resolutions = self.r.resolutions(module);
7469

75-
for (.., name_resolution) in resolutions.borrow().iter() {
70+
for (key, name_resolution) in resolutions.borrow().iter() {
7671
if let Some(binding) = name_resolution.borrow().binding() && binding.vis.is_public() && !binding.is_ambiguity() {
7772
let access_level = match binding.is_import() {
7873
true => {
79-
set_import_binding_access_level(self, binding, module_level);
74+
set_import_binding_access_level(self, binding, module_level, key.ns);
8075
Some(AccessLevel::Exported)
8176
},
8277
false => module_level,

compiler/rustc_resolve/src/imports.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::diagnostics::Suggestion;
44
use crate::Determinacy::{self, *};
5-
use crate::Namespace::{MacroNS, TypeNS};
5+
use crate::Namespace::{self, *};
66
use crate::{module_to_string, names_to_string};
77
use crate::{AmbiguityKind, BindingKey, ModuleKind, ResolutionError, Resolver, Segment};
88
use crate::{Finalize, Module, ModuleOrUniformRoot, ParentScope, PerNS, ScopeSet};
@@ -371,6 +371,31 @@ impl<'a> Resolver<'a> {
371371
self.used_imports.insert(import.id);
372372
}
373373
}
374+
375+
/// Take primary and additional node IDs from an import and select one that corresponds to the
376+
/// given namespace. The logic must match the corresponding logic from `fn lower_use_tree` that
377+
/// assigns resolutons to IDs.
378+
pub(crate) fn import_id_for_ns(&self, import: &Import<'_>, ns: Namespace) -> NodeId {
379+
if let ImportKind::Single { additional_ids: (id1, id2), .. } = import.kind {
380+
if let Some(resolutions) = self.import_res_map.get(&import.id) {
381+
assert!(resolutions[ns].is_some(), "incorrectly finalized import");
382+
return match ns {
383+
TypeNS => import.id,
384+
ValueNS => match resolutions.type_ns {
385+
Some(_) => id1,
386+
None => import.id,
387+
},
388+
MacroNS => match (resolutions.type_ns, resolutions.value_ns) {
389+
(Some(_), Some(_)) => id2,
390+
(Some(_), None) | (None, Some(_)) => id1,
391+
(None, None) => import.id,
392+
},
393+
};
394+
}
395+
}
396+
397+
import.id
398+
}
374399
}
375400

376401
/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
12241224
| ty::Never
12251225
| ty::Foreign(_) => {}
12261226

1227+
// `ManuallyDrop` is trivially drop
1228+
ty::Adt(def, _) if Some(def.did()) == tcx.lang_items().manually_drop() => {}
1229+
12271230
// These types are built-in, so we can fast-track by registering
12281231
// nested predicates for their constituent type(s)
12291232
ty::Array(ty, _) | ty::Slice(ty) => {

compiler/rustc_typeck/src/check/demand.rs

+10
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
417417
hir::def::CtorKind::Const => unreachable!(),
418418
};
419419

420+
// Suggest constructor as deep into the block tree as possible.
421+
// This fixes https://github.com/rust-lang/rust/issues/101065,
422+
// and also just helps make the most minimal suggestions.
423+
let mut expr = expr;
424+
while let hir::ExprKind::Block(block, _) = &expr.kind
425+
&& let Some(expr_) = &block.expr
426+
{
427+
expr = expr_
428+
}
429+
420430
vec![
421431
(expr.span.shrink_to_lo(), format!("{prefix}{variant}{open}")),
422432
(expr.span.shrink_to_hi(), close.to_owned()),

library/core/src/const_closure.rs

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::marker::Destruct;
2+
3+
/// Struct representing a closure with mutably borrowed data.
4+
///
5+
/// Example:
6+
/// ```no_build
7+
/// #![feature(const_mut_refs)]
8+
/// use crate::const_closure::ConstFnMutClosure;
9+
/// const fn imp(state: &mut i32, (arg,): (i32,)) -> i32 {
10+
/// *state += arg;
11+
/// *state
12+
/// }
13+
/// let mut i = 5;
14+
/// let mut cl = ConstFnMutClosure::new(&mut i, imp);
15+
///
16+
/// assert!(7 == cl(2));
17+
/// assert!(8 == cl(1));
18+
/// ```
19+
pub(crate) struct ConstFnMutClosure<'a, CapturedData: ?Sized, Function> {
20+
data: &'a mut CapturedData,
21+
func: Function,
22+
}
23+
24+
impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<'a, CapturedData, Function> {
25+
/// Function for creating a new closure.
26+
///
27+
/// `data` is the a mutable borrow of data that is captured from the environment.
28+
///
29+
/// `func` is the function of the closure, it gets the data and a tuple of the arguments closure
30+
/// and return the return value of the closure.
31+
pub(crate) const fn new<ClosureArguments, ClosureReturnValue>(
32+
data: &'a mut CapturedData,
33+
func: Function,
34+
) -> Self
35+
where
36+
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
37+
{
38+
Self { data, func }
39+
}
40+
}
41+
42+
impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const
43+
FnOnce<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function>
44+
where
45+
Function:
46+
~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue + ~const Destruct,
47+
{
48+
type Output = ClosureReturnValue;
49+
50+
extern "rust-call" fn call_once(mut self, args: ClosureArguments) -> Self::Output {
51+
self.call_mut(args)
52+
}
53+
}
54+
55+
impl<'a, CapturedData: ?Sized, ClosureArguments, Function, ClosureReturnValue> const
56+
FnMut<ClosureArguments> for ConstFnMutClosure<'a, CapturedData, Function>
57+
where
58+
Function: ~const Fn(&mut CapturedData, ClosureArguments) -> ClosureReturnValue,
59+
{
60+
extern "rust-call" fn call_mut(&mut self, args: ClosureArguments) -> Self::Output {
61+
(self.func)(self.data, args)
62+
}
63+
}

0 commit comments

Comments
 (0)