Skip to content

Commit

Permalink
Auto merge of #86559 - Dylan-DPC:rollup-aixg3q5, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Rollup of 6 pull requests

Successful merges:

 - #86223 (Specify the kind of the item for E0121)
 - #86521 (Add comments around code where ordering is important due for panic-safety)
 - #86523 (Improvements to intra-doc link macro disambiguators)
 - #86542 (Line numbers aligned with content)
 - #86549 (Add destructuring example of E0508)
 - #86557 (Update books)

Failed merges:

 - #86548 (Fix crate filter search reset)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Jun 22, 2021
2 parents 6a758ea + bd04f4c commit 574c9dd
Show file tree
Hide file tree
Showing 55 changed files with 569 additions and 460 deletions.
13 changes: 13 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0508.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ fn main() {
let _value = array[0].clone();
}
```

If you really want to move the value out, you can use a destructuring array
pattern to move it:

```
struct NonCopy;
fn main() {
let array = [NonCopy; 1];
// Destructuring the array
let [_value] = array;
}
```
21 changes: 21 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,27 @@ impl ItemKind<'_> {
_ => return None,
})
}

pub fn descr(&self) -> &'static str {
match self {
ItemKind::ExternCrate(..) => "extern crate",
ItemKind::Use(..) => "`use` import",
ItemKind::Static(..) => "static item",
ItemKind::Const(..) => "constant item",
ItemKind::Fn(..) => "function",
ItemKind::Mod(..) => "module",
ItemKind::ForeignMod { .. } => "extern block",
ItemKind::GlobalAsm(..) => "global asm item",
ItemKind::TyAlias(..) => "type alias",
ItemKind::OpaqueTy(..) => "opaque type",
ItemKind::Enum(..) => "enum",
ItemKind::Struct(..) => "struct",
ItemKind::Union(..) => "union",
ItemKind::Trait(..) => "trait",
ItemKind::TraitAlias(..) => "trait alias",
ItemKind::Impl(..) => "implementation",
}
}
}

/// A reference from an trait to one of its associated items. This
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2418,6 +2418,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
visitor.0,
true,
hir_ty,
"function",
);
}

Expand Down
51 changes: 40 additions & 11 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ crate fn placeholder_type_error(
placeholder_types: Vec<Span>,
suggest: bool,
hir_ty: Option<&hir::Ty<'_>>,
kind: &'static str,
) {
if placeholder_types.is_empty() {
return;
Expand Down Expand Up @@ -174,7 +175,7 @@ crate fn placeholder_type_error(
));
}

let mut err = bad_placeholder_type(tcx, placeholder_types);
let mut err = bad_placeholder_type(tcx, placeholder_types, kind);

// Suggest, but only if it is not a function in const or static
if suggest {
Expand Down Expand Up @@ -236,7 +237,15 @@ fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_item(item);

placeholder_type_error(tcx, Some(generics.span), generics.params, visitor.0, suggest, None);
placeholder_type_error(
tcx,
Some(generics.span),
generics.params,
visitor.0,
suggest,
None,
item.kind.descr(),
);
}

impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
Expand Down Expand Up @@ -302,13 +311,17 @@ impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
fn bad_placeholder_type(
tcx: TyCtxt<'tcx>,
mut spans: Vec<Span>,
kind: &'static str,
) -> rustc_errors::DiagnosticBuilder<'tcx> {
let kind = if kind.ends_with('s') { format!("{}es", kind) } else { format!("{}s", kind) };

spans.sort();
let mut err = struct_span_err!(
tcx.sess,
spans.clone(),
E0121,
"the type placeholder `_` is not allowed within types on item signatures",
"the type placeholder `_` is not allowed within types on item signatures for {}",
kind
);
for span in spans {
err.span_label(span, "not allowed in type signatures");
Expand Down Expand Up @@ -382,7 +395,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
_: Option<&ty::GenericParamDef>,
span: Span,
) -> &'tcx Const<'tcx> {
bad_placeholder_type(self.tcx(), vec![span]).emit();
bad_placeholder_type(self.tcx(), vec![span], "generic").emit();
// Typeck doesn't expect erased regions to be returned from `type_of`.
let ty = self.tcx.fold_regions(ty, &mut false, |r, _| match r {
ty::ReErased => self.tcx.lifetimes.re_static,
Expand Down Expand Up @@ -746,7 +759,15 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
hir::ForeignItemKind::Static(..) => {
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_foreign_item(item);
placeholder_type_error(tcx, None, &[], visitor.0, false, None);
placeholder_type_error(
tcx,
None,
&[],
visitor.0,
false,
None,
"static variable",
);
}
_ => (),
}
Expand Down Expand Up @@ -818,7 +839,15 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
if let hir::TyKind::TraitObject(..) = ty.kind {
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_item(it);
placeholder_type_error(tcx, None, &[], visitor.0, false, None);
placeholder_type_error(
tcx,
None,
&[],
visitor.0,
false,
None,
it.kind.descr(),
);
}
}
_ => (),
Expand Down Expand Up @@ -846,7 +875,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
// Account for `const C: _;`.
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);
placeholder_type_error(tcx, None, &[], visitor.0, false, None);
placeholder_type_error(tcx, None, &[], visitor.0, false, None, "constant");
}

hir::TraitItemKind::Type(_, Some(_)) => {
Expand All @@ -855,7 +884,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
// Account for `type T = _;`.
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);
placeholder_type_error(tcx, None, &[], visitor.0, false, None);
placeholder_type_error(tcx, None, &[], visitor.0, false, None, "associated type");
}

hir::TraitItemKind::Type(_, None) => {
Expand All @@ -865,7 +894,7 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_trait_item(trait_item);

placeholder_type_error(tcx, None, &[], visitor.0, false, None);
placeholder_type_error(tcx, None, &[], visitor.0, false, None, "associated type");
}
};

Expand All @@ -887,7 +916,7 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_impl_item(impl_item);

placeholder_type_error(tcx, None, &[], visitor.0, false, None);
placeholder_type_error(tcx, None, &[], visitor.0, false, None, "associated type");
}
hir::ImplItemKind::Const(..) => {}
}
Expand Down Expand Up @@ -1711,7 +1740,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {

let mut visitor = PlaceholderHirTyCollector::default();
visitor.visit_ty(ty);
let mut diag = bad_placeholder_type(tcx, visitor.0);
let mut diag = bad_placeholder_type(tcx, visitor.0, "return type");
let ret_ty = fn_sig.output();
if ret_ty != tcx.ty_error() {
if !ret_ty.is_closure() {
Expand Down
37 changes: 28 additions & 9 deletions compiler/rustc_typeck/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,9 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
TraitItemKind::Const(ref ty, body_id) => body_id
.and_then(|body_id| {
if is_suggestable_infer_ty(ty) {
Some(infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident))
Some(infer_placeholder_type(
tcx, def_id, body_id, ty.span, item.ident, "constant",
))
} else {
None
}
Expand All @@ -304,7 +306,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
}
ImplItemKind::Const(ref ty, body_id) => {
if is_suggestable_infer_ty(ty) {
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident)
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant")
} else {
icx.to_ty(ty)
}
Expand All @@ -320,9 +322,25 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {

Node::Item(item) => {
match item.kind {
ItemKind::Static(ref ty, .., body_id) | ItemKind::Const(ref ty, body_id) => {
ItemKind::Static(ref ty, .., body_id) => {
if is_suggestable_infer_ty(ty) {
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident)
infer_placeholder_type(
tcx,
def_id,
body_id,
ty.span,
item.ident,
"static variable",
)
} else {
icx.to_ty(ty)
}
}
ItemKind::Const(ref ty, body_id) => {
if is_suggestable_infer_ty(ty) {
infer_placeholder_type(
tcx, def_id, body_id, ty.span, item.ident, "constant",
)
} else {
icx.to_ty(ty)
}
Expand Down Expand Up @@ -742,13 +760,14 @@ fn let_position_impl_trait_type(tcx: TyCtxt<'_>, opaque_ty_id: LocalDefId) -> Ty
concrete_ty
}

fn infer_placeholder_type(
tcx: TyCtxt<'_>,
fn infer_placeholder_type<'a>(
tcx: TyCtxt<'a>,
def_id: LocalDefId,
body_id: hir::BodyId,
span: Span,
item_ident: Ident,
) -> Ty<'_> {
kind: &'static str,
) -> Ty<'a> {
// Attempts to make the type nameable by turning FnDefs into FnPtrs.
struct MakeNameable<'tcx> {
success: bool,
Expand Down Expand Up @@ -802,7 +821,7 @@ fn infer_placeholder_type(
if let Some(sugg_ty) = sugg_ty {
err.span_suggestion(
span,
"provide a type for the item",
&format!("provide a type for the {item}", item = kind),
format!("{}: {}", item_ident, sugg_ty),
Applicability::MachineApplicable,
);
Expand All @@ -816,7 +835,7 @@ fn infer_placeholder_type(
err.emit_unless(ty.references_error());
}
None => {
let mut diag = bad_placeholder_type(tcx, vec![span]);
let mut diag = bad_placeholder_type(tcx, vec![span], kind);

if !ty.references_error() {
let mut mk_nameable = MakeNameable::new(tcx);
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,8 @@ impl<T, A: Allocator> Vec<T, A> {
}
unsafe {
ptr::write(self.as_mut_ptr().add(len), element);
// Since next() executes user code which can panic we have to bump the length
// after each step.
// NB can't overflow since we would have had to alloc the address space
self.set_len(len + 1);
}
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/vec/source_iter_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ fn write_in_place_with_drop<T>(
// all we can do is check if it's still in range
debug_assert!(sink.dst as *const _ <= src_end, "InPlaceIterable contract violation");
ptr::write(sink.dst, item);
// Since this executes user code which can panic we have to bump the pointer
// after each step.
sink.dst = sink.dst.add(1);
}
Ok(sink)
Expand Down Expand Up @@ -136,6 +138,8 @@ where
let dst = dst_buf.offset(i as isize);
debug_assert!(dst as *const _ <= end, "InPlaceIterable contract violation");
ptr::write(dst, self.__iterator_get_unchecked(i));
// Since this executes user code which can panic we have to bump the pointer
// after each step.
drop_guard.dst = dst.add(1);
}
}
Expand Down
2 changes: 2 additions & 0 deletions library/alloc/src/vec/spec_extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ where
iterator.for_each(move |element| {
ptr::write(ptr, element);
ptr = ptr.offset(1);
// Since the loop executes user code which can panic we have to bump the pointer
// after each step.
// NB can't overflow since we would have had to alloc the address space
local_len.increment_len(1);
});
Expand Down
9 changes: 9 additions & 0 deletions library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,16 @@ where
fn next(&mut self) -> Option<(A::Item, B::Item)> {
if self.index < self.len {
let i = self.index;
// since get_unchecked executes code which can panic we increment the counters beforehand
// so that the same index won't be accessed twice, as required by TrustedRandomAccess
self.index += 1;
// SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
unsafe {
Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
}
} else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a_len {
let i = self.index;
// as above, increment before executing code that may panic
self.index += 1;
self.len += 1;
// match the base implementation's potential side effects
Expand All @@ -259,6 +262,8 @@ where
let end = self.index + delta;
while self.index < end {
let i = self.index;
// since get_unchecked executes code which can panic we increment the counters beforehand
// so that the same index won't be accessed twice, as required by TrustedRandomAccess
self.index += 1;
if A::MAY_HAVE_SIDE_EFFECT {
// SAFETY: the usage of `cmp::min` to calculate `delta`
Expand Down Expand Up @@ -295,6 +300,8 @@ where
let sz_a = self.a.size();
if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
for _ in 0..sz_a - self.len {
// since next_back() may panic we increment the counters beforehand
// to keep Zip's state in sync with the underlying iterator source
self.a_len -= 1;
self.a.next_back();
}
Expand All @@ -309,6 +316,8 @@ where
}
}
if self.index < self.len {
// since get_unchecked executes code which can panic we increment the counters beforehand
// so that the same index won't be accessed twice, as required by TrustedRandomAccess
self.len -= 1;
self.a_len -= 1;
let i = self.len;
Expand Down
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/x86_64-gnu-tools/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ENV PATH="/node-v14.4.0-linux-x64/bin:${PATH}"
# https://github.com/puppeteer/puppeteer/issues/375
#
# We also specify the version in case we need to update it to go around cache limitations.
RUN npm install -g browser-ui-test@0.3.0 --unsafe-perm=true
RUN npm install -g browser-ui-test@0.4.0 --unsafe-perm=true

ENV RUST_CONFIGURE_ARGS \
--build=x86_64-unknown-linux-gnu \
Expand Down
2 changes: 1 addition & 1 deletion src/doc/embedded-book
Loading

0 comments on commit 574c9dd

Please sign in to comment.