Skip to content

Commit 8b7684d

Browse files
committed
Auto merge of #51857 - pietroalbini:beta-backports, r=pietroalbini
[beta] Rollup backports Merged and approved: * #51725: Do not build LLVM tools for any of the tools * #51852: Don't use `ParamEnv::reveal_all()` if there is a real one available * #51686: yet another "old borrowck" bug around match default bindings * #51868: Remove process::id from 'Stabilized APIs' in 1.27.0 release notes * #51335: Prohibit `global_allocator` in submodules r? @ghost
2 parents 14a78df + 6164c69 commit 8b7684d

File tree

16 files changed

+220
-42
lines changed

16 files changed

+220
-42
lines changed

RELEASES.md

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ Stabilized APIs
5656
- [`Take::set_limit`]
5757
- [`hint::unreachable_unchecked`]
5858
- [`os::unix::process::parent_id`]
59-
- [`process::id`]
6059
- [`ptr::swap_nonoverlapping`]
6160
- [`slice::rsplit_mut`]
6261
- [`slice::rsplit`]

src/Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
20352035
name = "rustc_allocator"
20362036
version = "0.0.0"
20372037
dependencies = [
2038+
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
20382039
"rustc 0.0.0",
20392040
"rustc_errors 0.0.0",
20402041
"rustc_target 0.0.0",

src/bootstrap/tool.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ macro_rules! tool {
273273
/// Whether this tool requires LLVM to run
274274
pub fn uses_llvm_tools(&self) -> bool {
275275
match self {
276-
$(Tool::$name => true $(&& $llvm)*,)+
276+
$(Tool::$name => false $(|| $llvm)*,)+
277277
}
278278
}
279279
}
@@ -340,20 +340,17 @@ macro_rules! tool {
340340
}
341341
}
342342

343-
// FIXME(#51459): We have only checked that RustInstaller does not require
344-
// the LLVM binaries when running. We should go through all tools to determine
345-
// if they really need LLVM binaries, and make `llvm_tools` a required argument.
346343
tool!(
347344
Rustbook, "src/tools/rustbook", "rustbook", Mode::ToolRustc;
348345
ErrorIndex, "src/tools/error_index_generator", "error_index_generator", Mode::ToolRustc;
349346
UnstableBookGen, "src/tools/unstable-book-gen", "unstable-book-gen", Mode::ToolStd;
350347
Tidy, "src/tools/tidy", "tidy", Mode::ToolStd;
351348
Linkchecker, "src/tools/linkchecker", "linkchecker", Mode::ToolStd;
352349
CargoTest, "src/tools/cargotest", "cargotest", Mode::ToolStd;
353-
Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolTest;
350+
Compiletest, "src/tools/compiletest", "compiletest", Mode::ToolTest, llvm_tools = true;
354351
BuildManifest, "src/tools/build-manifest", "build-manifest", Mode::ToolStd;
355352
RemoteTestClient, "src/tools/remote-test-client", "remote-test-client", Mode::ToolStd;
356-
RustInstaller, "src/tools/rust-installer", "fabricate", Mode::ToolStd, llvm_tools = false;
353+
RustInstaller, "src/tools/rust-installer", "fabricate", Mode::ToolStd;
357354
RustdocTheme, "src/tools/rustdoc-themes", "rustdoc-themes", Mode::ToolStd;
358355
);
359356

src/librustc/middle/expr_use_visitor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
313313
debug!("consume_body(body={:?})", body);
314314

315315
for arg in &body.arguments {
316-
let arg_ty = return_if_err!(self.mc.node_ty(arg.pat.hir_id));
316+
let arg_ty = return_if_err!(self.mc.pat_ty_adjusted(&arg.pat));
317+
debug!("consume_body: arg_ty = {:?}", arg_ty);
317318

318319
let fn_body_scope_r =
319320
self.tcx().mk_region(ty::ReScope(region::Scope::Node(body.value.hir_id.local_id)));

src/librustc/middle/mem_categorization.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
517517
/// implicit deref patterns attached (e.g., it is really
518518
/// `&Some(x)`). In that case, we return the "outermost" type
519519
/// (e.g., `&Option<T>).
520-
fn pat_ty(&self, pat: &hir::Pat) -> McResult<Ty<'tcx>> {
520+
pub fn pat_ty_adjusted(&self, pat: &hir::Pat) -> McResult<Ty<'tcx>> {
521521
// Check for implicit `&` types wrapping the pattern; note
522522
// that these are never attached to binding patterns, so
523523
// actually this is somewhat "disjoint" from the code below
@@ -1300,7 +1300,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13001300
};
13011301

13021302
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
1303-
let subpat_ty = self.pat_ty(&subpat)?; // see (*2)
1303+
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
13041304
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
13051305
let subcmt = Rc::new(self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13061306
self.cat_pattern_(subcmt, &subpat, op)?;
@@ -1323,7 +1323,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13231323
};
13241324

13251325
for fp in field_pats {
1326-
let field_ty = self.pat_ty(&fp.node.pat)?; // see (*2)
1326+
let field_ty = self.pat_ty_adjusted(&fp.node.pat)?; // see (*2)
13271327
let f_index = self.tcx.field_index(fp.node.id, self.tables);
13281328
let cmt_field = Rc::new(self.cat_field(pat, cmt.clone(), f_index,
13291329
fp.node.ident, field_ty));
@@ -1342,7 +1342,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13421342
ref ty => span_bug!(pat.span, "tuple pattern unexpected type {:?}", ty),
13431343
};
13441344
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
1345-
let subpat_ty = self.pat_ty(&subpat)?; // see (*2)
1345+
let subpat_ty = self.pat_ty_unadjusted(&subpat)?; // see (*2)
13461346
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
13471347
let subcmt = Rc::new(self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13481348
self.cat_pattern_(subcmt, &subpat, op)?;

src/librustc_allocator/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ rustc_errors = { path = "../librustc_errors" }
1414
rustc_target = { path = "../librustc_target" }
1515
syntax = { path = "../libsyntax" }
1616
syntax_pos = { path = "../libsyntax_pos" }
17+
log = "0.4"

src/librustc_allocator/expand.rs

+79-28
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,45 @@
1111
use rustc::middle::allocator::AllocatorKind;
1212
use rustc_errors;
1313
use rustc_target::spec::abi::Abi;
14-
use syntax::ast::{Attribute, Crate, LitKind, StrStyle};
15-
use syntax::ast::{Arg, Constness, Generics, Mac, Mutability, Ty, Unsafety};
16-
use syntax::ast::{self, Expr, Ident, Item, ItemKind, TyKind, VisibilityKind};
17-
use syntax::attr;
18-
use syntax::codemap::{dummy_spanned, respan};
19-
use syntax::codemap::{ExpnInfo, MacroAttribute, NameAndSpan};
20-
use syntax::ext::base::ExtCtxt;
21-
use syntax::ext::base::Resolver;
22-
use syntax::ext::build::AstBuilder;
23-
use syntax::ext::expand::ExpansionConfig;
24-
use syntax::ext::hygiene::{self, Mark, SyntaxContext};
25-
use syntax::fold::{self, Folder};
26-
use syntax::parse::ParseSess;
27-
use syntax::ptr::P;
28-
use syntax::symbol::Symbol;
29-
use syntax::util::small_vector::SmallVector;
30-
use syntax_pos::{Span, DUMMY_SP};
14+
use syntax::{
15+
ast::{
16+
self, Arg, Attribute, Constness, Crate, Expr, Generics, Ident, Item, ItemKind,
17+
LitKind, Mac, Mod, Mutability, StrStyle, Ty, TyKind, Unsafety, VisibilityKind,
18+
},
19+
attr,
20+
codemap::{
21+
dummy_spanned, respan, ExpnInfo, MacroAttribute, NameAndSpan,
22+
},
23+
ext::{
24+
base::{ExtCtxt, Resolver},
25+
build::AstBuilder,
26+
expand::ExpansionConfig,
27+
hygiene::{self, Mark, SyntaxContext},
28+
},
29+
fold::{self, Folder},
30+
parse::ParseSess,
31+
ptr::P,
32+
symbol::Symbol,
33+
util::small_vector::SmallVector,
34+
};
35+
use syntax_pos::Span;
3136

3237
use {AllocatorMethod, AllocatorTy, ALLOCATOR_METHODS};
3338

3439
pub fn modify(
3540
sess: &ParseSess,
3641
resolver: &mut Resolver,
3742
krate: Crate,
43+
crate_name: String,
3844
handler: &rustc_errors::Handler,
3945
) -> ast::Crate {
4046
ExpandAllocatorDirectives {
4147
handler,
4248
sess,
4349
resolver,
4450
found: false,
51+
crate_name: Some(crate_name),
52+
in_submod: -1, // -1 to account for the "root" module
4553
}.fold_crate(krate)
4654
}
4755

@@ -50,10 +58,17 @@ struct ExpandAllocatorDirectives<'a> {
5058
handler: &'a rustc_errors::Handler,
5159
sess: &'a ParseSess,
5260
resolver: &'a mut Resolver,
61+
crate_name: Option<String>,
62+
63+
// For now, we disallow `global_allocator` in submodules because hygiene is hard. Keep track of
64+
// whether we are in a submodule or not. If `in_submod > 0` we are in a submodule.
65+
in_submod: isize,
5366
}
5467

5568
impl<'a> Folder for ExpandAllocatorDirectives<'a> {
5669
fn fold_item(&mut self, item: P<Item>) -> SmallVector<P<Item>> {
70+
debug!("in submodule {}", self.in_submod);
71+
5772
let name = if attr::contains_name(&item.attrs, "global_allocator") {
5873
"global_allocator"
5974
} else {
@@ -68,19 +83,23 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
6883
}
6984
}
7085

86+
if self.in_submod > 0 {
87+
self.handler
88+
.span_err(item.span, "`global_allocator` cannot be used in submodules");
89+
return SmallVector::one(item);
90+
}
91+
7192
if self.found {
72-
self.handler.span_err(
73-
item.span,
74-
"cannot define more than one \
75-
#[global_allocator]",
76-
);
93+
self.handler
94+
.span_err(item.span, "cannot define more than one #[global_allocator]");
7795
return SmallVector::one(item);
7896
}
7997
self.found = true;
8098

99+
// Create a fresh Mark for the new macro expansion we are about to do
81100
let mark = Mark::fresh(Mark::root());
82101
mark.set_expn_info(ExpnInfo {
83-
call_site: DUMMY_SP,
102+
call_site: item.span, // use the call site of the static
84103
callee: NameAndSpan {
85104
format: MacroAttribute(Symbol::intern(name)),
86105
span: None,
@@ -89,38 +108,70 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
89108
edition: hygiene::default_edition(),
90109
},
91110
});
111+
112+
// Tie the span to the macro expansion info we just created
92113
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));
93-
let ecfg = ExpansionConfig::default(name.to_string());
114+
115+
// Create an expansion config
116+
let ecfg = ExpansionConfig::default(self.crate_name.take().unwrap());
117+
118+
// Generate a bunch of new items using the AllocFnFactory
94119
let mut f = AllocFnFactory {
95120
span,
96121
kind: AllocatorKind::Global,
97122
global: item.ident,
98123
core: Ident::from_str("core"),
99124
cx: ExtCtxt::new(self.sess, ecfg, self.resolver),
100125
};
126+
127+
// We will generate a new submodule. To `use` the static from that module, we need to get
128+
// the `super::...` path.
101129
let super_path = f.cx.path(f.span, vec![Ident::from_str("super"), f.global]);
130+
131+
// Generate the items in the submodule
102132
let mut items = vec![
133+
// import `core` to use allocators
103134
f.cx.item_extern_crate(f.span, f.core),
135+
// `use` the `global_allocator` in `super`
104136
f.cx.item_use_simple(
105137
f.span,
106138
respan(f.span.shrink_to_lo(), VisibilityKind::Inherited),
107139
super_path,
108140
),
109141
];
110-
for method in ALLOCATOR_METHODS {
111-
items.push(f.allocator_fn(method));
112-
}
142+
143+
// Add the allocator methods to the submodule
144+
items.extend(
145+
ALLOCATOR_METHODS
146+
.iter()
147+
.map(|method| f.allocator_fn(method)),
148+
);
149+
150+
// Generate the submodule itself
113151
let name = f.kind.fn_name("allocator_abi");
114152
let allocator_abi = Ident::with_empty_ctxt(Symbol::gensym(&name));
115153
let module = f.cx.item_mod(span, span, allocator_abi, Vec::new(), items);
116154
let module = f.cx.monotonic_expander().fold_item(module).pop().unwrap();
117155

118-
let mut ret = SmallVector::new();
156+
// Return the item and new submodule
157+
let mut ret = SmallVector::with_capacity(2);
119158
ret.push(item);
120159
ret.push(module);
160+
121161
return ret;
122162
}
123163

164+
// If we enter a submodule, take note.
165+
fn fold_mod(&mut self, m: Mod) -> Mod {
166+
debug!("enter submodule");
167+
self.in_submod += 1;
168+
let ret = fold::noop_fold_mod(m, self);
169+
self.in_submod -= 1;
170+
debug!("exit submodule");
171+
ret
172+
}
173+
174+
// `fold_mac` is disabled by default. Enable it here.
124175
fn fold_mac(&mut self, mac: Mac) -> Mac {
125176
fold::noop_fold_mac(mac, self)
126177
}

src/librustc_allocator/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#![feature(rustc_private)]
1212

13+
#[macro_use] extern crate log;
1314
extern crate rustc;
1415
extern crate rustc_errors;
1516
extern crate rustc_target;

src/librustc_driver/driver.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,19 @@ where
10511051
});
10521052
}
10531053

1054+
// Expand global allocators, which are treated as an in-tree proc macro
10541055
krate = time(sess, "creating allocators", || {
1055-
allocator::expand::modify(&sess.parse_sess, &mut resolver, krate, sess.diagnostic())
1056+
allocator::expand::modify(
1057+
&sess.parse_sess,
1058+
&mut resolver,
1059+
krate,
1060+
crate_name.to_string(),
1061+
sess.diagnostic(),
1062+
)
10561063
});
10571064

1065+
// Done with macro expansion!
1066+
10581067
after_expand(&krate)?;
10591068

10601069
if sess.opts.debugging_opts.input_stats {

src/librustc_mir/interpret/const_eval.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub fn value_to_const_value<'tcx>(
7676
val: Value,
7777
ty: Ty<'tcx>,
7878
) -> &'tcx ty::Const<'tcx> {
79-
let layout = ecx.tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap();
79+
let layout = ecx.layout_of(ty).unwrap();
8080
match (val, &layout.abi) {
8181
(Value::Scalar(Scalar::Bits { defined: 0, ..}), _) if layout.is_zst() => {},
8282
(Value::ByRef(..), _) |

src/test/ui/allocator-submodule.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Tests that it is possible to create a global allocator in a submodule, rather than in the crate
12+
// root.
13+
14+
#![feature(alloc, allocator_api, global_allocator)]
15+
16+
extern crate alloc;
17+
18+
use std::{
19+
alloc::{GlobalAlloc, Layout},
20+
ptr,
21+
};
22+
23+
struct MyAlloc;
24+
25+
unsafe impl GlobalAlloc for MyAlloc {
26+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
27+
ptr::null_mut()
28+
}
29+
30+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {}
31+
}
32+
33+
mod submod {
34+
use super::MyAlloc;
35+
36+
#[global_allocator]
37+
static MY_HEAP: MyAlloc = MyAlloc; //~ ERROR global_allocator
38+
}
39+
40+
fn main() {}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `global_allocator` cannot be used in submodules
2+
--> $DIR/allocator-submodule.rs:37:5
3+
|
4+
LL | static MY_HEAP: MyAlloc = MyAlloc; //~ ERROR global_allocator
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0507]: cannot move out of borrowed content
2+
--> $DIR/issue-51415.rs:16:47
3+
|
4+
LL | let opt = a.iter().enumerate().find(|(_, &s)| {
5+
| ^ cannot move out of borrowed content
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)