Skip to content

Commit 073744f

Browse files
committed
Auto merge of rust-lang#71367 - Dylan-DPC:rollup-ysj4olr, r=Dylan-DPC
Rollup of 4 pull requests Successful merges: - rust-lang#69362 (Stabilize most common subset of alloc_layout_extras) - rust-lang#71174 (Check that main/start is not async) - rust-lang#71285 (MIR: use HirId instead of NodeId to avoid cycles while inlining) - rust-lang#71346 (Do not build tools if user do not want them) Failed merges: r? @ghost
2 parents 20fc02f + 9a0e702 commit 073744f

File tree

12 files changed

+145
-35
lines changed

12 files changed

+145
-35
lines changed

src/bootstrap/tool.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,15 @@ macro_rules! tool_extended {
607607

608608
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
609609
let builder = run.builder;
610-
run.path($path).default_condition(builder.config.extended)
610+
run.path($path).default_condition(
611+
builder.config.extended
612+
&& builder.config.tools.as_ref().map_or(true, |tools| {
613+
tools.iter().any(|tool| match tool.as_ref() {
614+
"clippy" => $tool_name == "clippy-driver",
615+
x => $tool_name == x,
616+
})
617+
}),
618+
)
611619
}
612620

613621
fn make_run(run: RunConfig<'_>) {

src/libcore/alloc/layout.rs

+43-13
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Layout {
162162
/// Returns an error if the combination of `self.size()` and the given
163163
/// `align` violates the conditions listed in
164164
/// [`Layout::from_size_align`](#method.from_size_align).
165-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
165+
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
166166
#[inline]
167167
pub fn align_to(&self, align: usize) -> Result<Self, LayoutErr> {
168168
Layout::from_size_align(self.size(), cmp::max(self.align(), align))
@@ -218,7 +218,7 @@ impl Layout {
218218
///
219219
/// This is equivalent to adding the result of `padding_needed_for`
220220
/// to the layout's current size.
221-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
221+
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
222222
#[inline]
223223
pub fn pad_to_align(&self) -> Layout {
224224
let pad = self.padding_needed_for(self.align());
@@ -258,19 +258,50 @@ impl Layout {
258258

259259
/// Creates a layout describing the record for `self` followed by
260260
/// `next`, including any necessary padding to ensure that `next`
261-
/// will be properly aligned. Note that the resulting layout will
262-
/// satisfy the alignment properties of both `self` and `next`.
261+
/// will be properly aligned, but *no trailing padding*.
263262
///
264-
/// The resulting layout will be the same as that of a C struct containing
265-
/// two fields with the layouts of `self` and `next`, in that order.
263+
/// In order to match C representation layout `repr(C)`, you should
264+
/// call `pad_to_align` after extending the layout with all fields.
265+
/// (There is no way to match the default Rust representation
266+
/// layout `repr(Rust)`, as it is unspecified.)
266267
///
267-
/// Returns `Some((k, offset))`, where `k` is layout of the concatenated
268+
/// Note that the alignment of the resulting layout will be the maximum of
269+
/// those of `self` and `next`, in order to ensure alignment of both parts.
270+
///
271+
/// Returns `Ok((k, offset))`, where `k` is layout of the concatenated
268272
/// record and `offset` is the relative location, in bytes, of the
269273
/// start of the `next` embedded within the concatenated record
270274
/// (assuming that the record itself starts at offset 0).
271275
///
272276
/// On arithmetic overflow, returns `LayoutErr`.
273-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
277+
///
278+
/// # Examples
279+
///
280+
/// To calculate the layout of a `#[repr(C)]` structure and the offsets of
281+
/// the fields from its fields' layouts:
282+
///
283+
/// ```rust
284+
/// # use std::alloc::{Layout, LayoutErr};
285+
/// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec<usize>), LayoutErr> {
286+
/// let mut offsets = Vec::new();
287+
/// let mut layout = Layout::from_size_align(0, 1)?;
288+
/// for &field in fields {
289+
/// let (new_layout, offset) = layout.extend(field)?;
290+
/// layout = new_layout;
291+
/// offsets.push(offset);
292+
/// }
293+
/// // Remember to finalize with `pad_to_align`!
294+
/// Ok((layout.pad_to_align(), offsets))
295+
/// }
296+
/// # // test that it works
297+
/// # #[repr(C)] struct S { a: u64, b: u32, c: u16, d: u32 }
298+
/// # let s = Layout::new::<S>();
299+
/// # let u16 = Layout::new::<u16>();
300+
/// # let u32 = Layout::new::<u32>();
301+
/// # let u64 = Layout::new::<u64>();
302+
/// # assert_eq!(repr_c(&[u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16])));
303+
/// ```
304+
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
274305
#[inline]
275306
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
276307
let new_align = cmp::max(self.align(), next.align());
@@ -318,13 +349,12 @@ impl Layout {
318349
/// Creates a layout describing the record for a `[T; n]`.
319350
///
320351
/// On arithmetic overflow, returns `LayoutErr`.
321-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
352+
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
322353
#[inline]
323354
pub fn array<T>(n: usize) -> Result<Self, LayoutErr> {
324-
Layout::new::<T>().repeat(n).map(|(k, offs)| {
325-
debug_assert!(offs == mem::size_of::<T>());
326-
k
327-
})
355+
let (layout, offset) = Layout::new::<T>().repeat(n)?;
356+
debug_assert_eq!(offset, mem::size_of::<T>());
357+
Ok(layout.pad_to_align())
328358
}
329359
}
330360

src/librustc_error_codes/error_codes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ E0748: include_str!("./error_codes/E0748.md"),
431431
E0749: include_str!("./error_codes/E0749.md"),
432432
E0750: include_str!("./error_codes/E0750.md"),
433433
E0751: include_str!("./error_codes/E0751.md"),
434+
E0752: include_str!("./error_codes/E0752.md"),
434435
;
435436
// E0006, // merged with E0005
436437
// E0008, // cannot bind by-move into a pattern guard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
`fn main()` or the specified start function is not allowed to be
2+
async. You might be seeing this error because your async runtime
3+
library is not set up correctly.
4+
5+
Erroneous code example:
6+
7+
```compile_fail,E0752
8+
async fn main() -> Result<i32, ()> {
9+
Ok(1)
10+
}
11+
```

src/librustc_mir/transform/inline.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,15 @@ impl Inliner<'tcx> {
9494
continue;
9595
}
9696

97-
let self_node_id = self.tcx.hir().as_local_node_id(self.source.def_id()).unwrap();
98-
let callee_node_id = self.tcx.hir().as_local_node_id(callsite.callee);
97+
let callee_hir_id = self.tcx.hir().as_local_hir_id(callsite.callee);
9998

100-
let callee_body = if let Some(callee_node_id) = callee_node_id {
99+
let callee_body = if let Some(callee_hir_id) = callee_hir_id {
100+
let self_hir_id = self.tcx.hir().as_local_hir_id(self.source.def_id()).unwrap();
101101
// Avoid a cycle here by only using `optimized_mir` only if we have
102-
// a lower node id than the callee. This ensures that the callee will
102+
// a lower `HirId` than the callee. This ensures that the callee will
103103
// not inline us. This trick only works without incremental compilation.
104104
// So don't do it if that is enabled.
105-
if !self.tcx.dep_graph.is_fully_enabled()
106-
&& self_node_id.as_u32() < callee_node_id.as_u32()
107-
{
105+
if !self.tcx.dep_graph.is_fully_enabled() && self_hir_id < callee_hir_id {
108106
self.tcx.optimized_mir(callsite.callee)
109107
} else {
110108
continue;

src/librustc_trait_selection/traits/error_reporting/on_unimplemented.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
8282
match &node {
8383
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. }) => {
8484
self.describe_generator(*body_id).or_else(|| {
85-
Some(if let hir::FnHeader { asyncness: hir::IsAsync::Async, .. } = sig.header {
86-
"an async function"
87-
} else {
88-
"a function"
85+
Some(match sig.header {
86+
hir::FnHeader { asyncness: hir::IsAsync::Async, .. } => "an async function",
87+
_ => "a function",
8988
})
9089
})
9190
}
@@ -97,10 +96,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
9796
kind: hir::ImplItemKind::Fn(sig, body_id),
9897
..
9998
}) => self.describe_generator(*body_id).or_else(|| {
100-
Some(if let hir::FnHeader { asyncness: hir::IsAsync::Async, .. } = sig.header {
101-
"an async method"
102-
} else {
103-
"a method"
99+
Some(match sig.header {
100+
hir::FnHeader { asyncness: hir::IsAsync::Async, .. } => "an async method",
101+
_ => "a method",
104102
})
105103
}),
106104
hir::Node::Expr(hir::Expr {

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1318,10 +1318,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
13181318

13191319
let is_async = inner_generator_body
13201320
.and_then(|body| body.generator_kind())
1321-
.map(|generator_kind| match generator_kind {
1322-
hir::GeneratorKind::Async(..) => true,
1323-
_ => false,
1324-
})
1321+
.map(|generator_kind| matches!(generator_kind, hir::GeneratorKind::Async(..)))
13251322
.unwrap_or(false);
13261323
let (await_or_yield, an_await_or_yield) =
13271324
if is_async { ("await", "an await") } else { ("yield", "a yield") };

src/librustc_typeck/lib.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
159159
match main_t.kind {
160160
ty::FnDef(..) => {
161161
if let Some(Node::Item(it)) = tcx.hir().find(main_id) {
162-
if let hir::ItemKind::Fn(.., ref generics, _) = it.kind {
162+
if let hir::ItemKind::Fn(ref sig, ref generics, _) = it.kind {
163163
let mut error = false;
164164
if !generics.params.is_empty() {
165165
let msg = "`main` function is not allowed to have generic \
@@ -182,6 +182,18 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
182182
.emit();
183183
error = true;
184184
}
185+
if let hir::IsAsync::Async = sig.header.asyncness {
186+
let span = tcx.sess.source_map().guess_head_span(it.span);
187+
struct_span_err!(
188+
tcx.sess,
189+
span,
190+
E0752,
191+
"`main` function is not allowed to be `async`"
192+
)
193+
.span_label(span, "`main` function is not allowed to be `async`")
194+
.emit();
195+
error = true;
196+
}
185197
if error {
186198
return;
187199
}
@@ -226,7 +238,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
226238
match start_t.kind {
227239
ty::FnDef(..) => {
228240
if let Some(Node::Item(it)) = tcx.hir().find(start_id) {
229-
if let hir::ItemKind::Fn(.., ref generics, _) = it.kind {
241+
if let hir::ItemKind::Fn(ref sig, ref generics, _) = it.kind {
230242
let mut error = false;
231243
if !generics.params.is_empty() {
232244
struct_span_err!(
@@ -250,6 +262,18 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
250262
.emit();
251263
error = true;
252264
}
265+
if let hir::IsAsync::Async = sig.header.asyncness {
266+
let span = tcx.sess.source_map().guess_head_span(it.span);
267+
struct_span_err!(
268+
tcx.sess,
269+
span,
270+
E0752,
271+
"start is not allowed to be `async`"
272+
)
273+
.span_label(span, "start is not allowed to be `async`")
274+
.emit();
275+
error = true;
276+
}
253277
if error {
254278
return;
255279
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
3+
#![feature(start)]
4+
5+
#[start]
6+
pub async fn start(_: isize, _: *const *const u8) -> isize {
7+
//~^ ERROR start is not allowed to be `async`
8+
0
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0752]: start is not allowed to be `async`
2+
--> $DIR/issue-68523-start.rs:6:1
3+
|
4+
LL | pub async fn start(_: isize, _: *const *const u8) -> isize {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ start is not allowed to be `async`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0752`.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// edition:2018
2+
3+
async fn main() -> Result<i32, ()> {
4+
//~^ ERROR `main` function is not allowed to be `async`
5+
//~^^ ERROR `main` has invalid return type `impl std::future::Future`
6+
Ok(1)
7+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: `main` has invalid return type `impl std::future::Future`
2+
--> $DIR/issue-68523.rs:3:20
3+
|
4+
LL | async fn main() -> Result<i32, ()> {
5+
| ^^^^^^^^^^^^^^^ `main` can only return types that implement `std::process::Termination`
6+
|
7+
= help: consider using `()`, or a `Result`
8+
9+
error[E0752]: `main` function is not allowed to be `async`
10+
--> $DIR/issue-68523.rs:3:1
11+
|
12+
LL | async fn main() -> Result<i32, ()> {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`
14+
15+
error: aborting due to 2 previous errors
16+
17+
Some errors have detailed explanations: E0277, E0752.
18+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)