Skip to content

Commit e0e5d82

Browse files
committed
Auto merge of #69271 - Centril:rollup-iupsol6, r=Centril
Rollup of 6 pull requests Successful merges: - #69146 (Always const qualify literals by type) - #69159 (Select an appropriate unused lifetime name in suggestion) - #69194 (parse: fuse associated and extern items up to defaultness) - #69211 (parser: Simplify treatment of macro variables in `Parser::bump`) - #69217 (Do not emit note suggesting to implement operation trait to foreign type) - #69236 (parse: recover `mut (x @ y)` as `(mut x @ mut y)`.) Failed merges: r? @ghost
2 parents e620d0f + 6c6d45c commit e0e5d82

File tree

122 files changed

+1576
-936
lines changed

Some content is hidden

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

122 files changed

+1576
-936
lines changed

src/librustc/ty/util.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ impl<'tcx> ty::TyS<'tcx> {
697697
/// strange rules like `<T as Foo<'static>>::Bar: Sized` that
698698
/// actually carry lifetime requirements.
699699
pub fn is_sized(&'tcx self, tcx_at: TyCtxtAt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
700-
tcx_at.is_sized_raw(param_env.and(self))
700+
self.is_trivially_sized(tcx_at.tcx) || tcx_at.is_sized_raw(param_env.and(self))
701701
}
702702

703703
/// Checks whether values of this type `T` implement the `Freeze`
@@ -713,7 +713,43 @@ impl<'tcx> ty::TyS<'tcx> {
713713
param_env: ty::ParamEnv<'tcx>,
714714
span: Span,
715715
) -> bool {
716-
tcx.at(span).is_freeze_raw(param_env.and(self))
716+
self.is_trivially_freeze() || tcx.at(span).is_freeze_raw(param_env.and(self))
717+
}
718+
719+
/// Fast path helper for testing if a type is `Freeze`.
720+
///
721+
/// Returning true means the type is known to be `Freeze`. Returning
722+
/// `false` means nothing -- could be `Freeze`, might not be.
723+
fn is_trivially_freeze(&self) -> bool {
724+
match self.kind {
725+
ty::Int(_)
726+
| ty::Uint(_)
727+
| ty::Float(_)
728+
| ty::Bool
729+
| ty::Char
730+
| ty::Str
731+
| ty::Never
732+
| ty::Ref(..)
733+
| ty::RawPtr(_)
734+
| ty::FnDef(..)
735+
| ty::Error
736+
| ty::FnPtr(_) => true,
737+
ty::Tuple(_) => self.tuple_fields().all(Self::is_trivially_freeze),
738+
ty::Slice(elem_ty) | ty::Array(elem_ty, _) => elem_ty.is_trivially_freeze(),
739+
ty::Adt(..)
740+
| ty::Bound(..)
741+
| ty::Closure(..)
742+
| ty::Dynamic(..)
743+
| ty::Foreign(_)
744+
| ty::Generator(..)
745+
| ty::GeneratorWitness(_)
746+
| ty::Infer(_)
747+
| ty::Opaque(..)
748+
| ty::Param(_)
749+
| ty::Placeholder(_)
750+
| ty::Projection(_)
751+
| ty::UnnormalizedProjection(_) => false,
752+
}
717753
}
718754

719755
/// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely

src/librustc_ast_lowering/item.rs

+53-49
Original file line numberDiff line numberDiff line change
@@ -269,26 +269,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
269269
self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs)
270270
}
271271
ItemKind::Static(ref t, m, ref e) => {
272-
let ty = self.lower_ty(
273-
t,
274-
if self.sess.features_untracked().impl_trait_in_bindings {
275-
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
276-
} else {
277-
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
278-
},
279-
);
280-
hir::ItemKind::Static(ty, m, self.lower_const_body(span, Some(e)))
272+
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
273+
hir::ItemKind::Static(ty, m, body_id)
281274
}
282275
ItemKind::Const(ref t, ref e) => {
283-
let ty = self.lower_ty(
284-
t,
285-
if self.sess.features_untracked().impl_trait_in_bindings {
286-
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
287-
} else {
288-
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
289-
},
290-
);
291-
hir::ItemKind::Const(ty, self.lower_const_body(span, Some(e)))
276+
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
277+
hir::ItemKind::Const(ty, body_id)
292278
}
293279
ItemKind::Fn(FnSig { ref decl, header }, ref generics, ref body) => {
294280
let fn_def_id = self.resolver.definitions().local_def_id(id);
@@ -457,6 +443,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
457443
// not cause an assertion failure inside the `lower_defaultness` function.
458444
}
459445

446+
fn lower_const_item(
447+
&mut self,
448+
ty: &Ty,
449+
span: Span,
450+
body: Option<&Expr>,
451+
) -> (&'hir hir::Ty<'hir>, hir::BodyId) {
452+
let itctx = if self.sess.features_untracked().impl_trait_in_bindings {
453+
ImplTraitContext::OpaqueTy(None, hir::OpaqueTyOrigin::Misc)
454+
} else {
455+
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
456+
};
457+
let ty = self.lower_ty(ty, itctx);
458+
(ty, self.lower_const_body(span, body))
459+
}
460+
460461
fn lower_use_tree(
461462
&mut self,
462463
tree: &UseTree,
@@ -678,11 +679,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
678679

679680
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
680681
}
681-
ForeignItemKind::Static(ref t, m) => {
682+
ForeignItemKind::Static(ref t, m, _) => {
682683
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
683684
hir::ForeignItemKind::Static(ty, m)
684685
}
685-
ForeignItemKind::Ty => hir::ForeignItemKind::Type,
686+
ForeignItemKind::Const(ref t, _) => {
687+
// For recovery purposes.
688+
let ty = self.lower_ty(t, ImplTraitContext::disallowed());
689+
hir::ForeignItemKind::Static(ty, Mutability::Not)
690+
}
691+
ForeignItemKind::TyAlias(..) => hir::ForeignItemKind::Type,
686692
ForeignItemKind::Macro(_) => panic!("macro shouldn't exist here"),
687693
},
688694
vis: self.lower_visibility(&i.vis, None),
@@ -759,32 +765,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
759765
let trait_item_def_id = self.resolver.definitions().local_def_id(i.id);
760766

761767
let (generics, kind) = match i.kind {
762-
AssocItemKind::Const(ref ty, ref default) => {
763-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
768+
AssocItemKind::Static(ref ty, _, ref default) // Let's pretend this is a `const`.
769+
| AssocItemKind::Const(ref ty, ref default) => {
764770
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
765-
(
766-
generics,
767-
hir::TraitItemKind::Const(
768-
ty,
769-
default.as_ref().map(|x| self.lower_const_body(i.span, Some(x))),
770-
),
771-
)
771+
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
772+
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
772773
}
773-
AssocItemKind::Fn(ref sig, None) => {
774+
AssocItemKind::Fn(ref sig, ref generics, None) => {
774775
let names = self.lower_fn_params_to_names(&sig.decl);
775776
let (generics, sig) =
776-
self.lower_method_sig(&i.generics, sig, trait_item_def_id, false, None);
777+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
777778
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Required(names)))
778779
}
779-
AssocItemKind::Fn(ref sig, Some(ref body)) => {
780+
AssocItemKind::Fn(ref sig, ref generics, Some(ref body)) => {
780781
let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body));
781782
let (generics, sig) =
782-
self.lower_method_sig(&i.generics, sig, trait_item_def_id, false, None);
783+
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
783784
(generics, hir::TraitItemKind::Method(sig, hir::TraitMethod::Provided(body_id)))
784785
}
785-
AssocItemKind::TyAlias(ref bounds, ref default) => {
786+
AssocItemKind::TyAlias(ref generics, ref bounds, ref default) => {
786787
let ty = default.as_ref().map(|x| self.lower_ty(x, ImplTraitContext::disallowed()));
787-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
788+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
788789
let kind = hir::TraitItemKind::Type(
789790
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
790791
ty,
@@ -806,10 +807,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
806807
}
807808

808809
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
809-
let (kind, has_default) = match i.kind {
810-
AssocItemKind::Const(_, ref default) => (hir::AssocItemKind::Const, default.is_some()),
811-
AssocItemKind::TyAlias(_, ref default) => (hir::AssocItemKind::Type, default.is_some()),
812-
AssocItemKind::Fn(ref sig, ref default) => {
810+
let (kind, has_default) = match &i.kind {
811+
AssocItemKind::Static(_, _, default) // Let's pretend this is a `const` for recovery.
812+
| AssocItemKind::Const(_, default) => {
813+
(hir::AssocItemKind::Const, default.is_some())
814+
}
815+
AssocItemKind::TyAlias(_, _, default) => (hir::AssocItemKind::Type, default.is_some()),
816+
AssocItemKind::Fn(sig, _, default) => {
813817
(hir::AssocItemKind::Method { has_self: sig.decl.has_self() }, default.is_some())
814818
}
815819
AssocItemKind::Macro(..) => unimplemented!(),
@@ -832,22 +836,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
832836
let impl_item_def_id = self.resolver.definitions().local_def_id(i.id);
833837

834838
let (generics, kind) = match i.kind {
835-
AssocItemKind::Const(ref ty, ref expr) => {
836-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
839+
AssocItemKind::Static(ref ty, _, ref expr) | AssocItemKind::Const(ref ty, ref expr) => {
837840
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
838841
(
839-
generics,
842+
hir::Generics::empty(),
840843
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
841844
)
842845
}
843-
AssocItemKind::Fn(ref sig, ref body) => {
846+
AssocItemKind::Fn(ref sig, ref generics, ref body) => {
844847
self.current_item = Some(i.span);
845848
let asyncness = sig.header.asyncness;
846849
let body_id =
847850
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, body.as_deref());
848851
let impl_trait_return_allow = !self.is_in_trait_impl;
849852
let (generics, sig) = self.lower_method_sig(
850-
&i.generics,
853+
generics,
851854
sig,
852855
impl_item_def_id,
853856
impl_trait_return_allow,
@@ -856,8 +859,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
856859

857860
(generics, hir::ImplItemKind::Method(sig, body_id))
858861
}
859-
AssocItemKind::TyAlias(_, ref ty) => {
860-
let generics = self.lower_generics(&i.generics, ImplTraitContext::disallowed());
862+
AssocItemKind::TyAlias(ref generics, _, ref ty) => {
863+
let generics = self.lower_generics(generics, ImplTraitContext::disallowed());
861864
let kind = match ty {
862865
None => {
863866
let ty = self.arena.alloc(self.ty(i.span, hir::TyKind::Err));
@@ -901,14 +904,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
901904
vis: self.lower_visibility(&i.vis, Some(i.id)),
902905
defaultness: self.lower_defaultness(i.defaultness, true /* [1] */),
903906
kind: match &i.kind {
904-
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
905-
AssocItemKind::TyAlias(_, ty) => {
907+
AssocItemKind::Static(..) // Let's pretend this is a `const` for recovery.
908+
| AssocItemKind::Const(..) => hir::AssocItemKind::Const,
909+
AssocItemKind::TyAlias(_, _, ty) => {
906910
match ty.as_deref().and_then(|ty| ty.kind.opaque_top_hack()) {
907911
None => hir::AssocItemKind::Type,
908912
Some(_) => hir::AssocItemKind::OpaqueTy,
909913
}
910914
}
911-
AssocItemKind::Fn(sig, _) => {
915+
AssocItemKind::Fn(sig, _, _) => {
912916
hir::AssocItemKind::Method { has_self: sig.decl.has_self() }
913917
}
914918
AssocItemKind::Macro(..) => unimplemented!(),

src/librustc_ast_lowering/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
490490
self.lctx.allocate_hir_id_counter(item.id);
491491
let owner = match (&item.kind, ctxt) {
492492
// Ignore patterns in trait methods without bodies.
493-
(AssocItemKind::Fn(_, None), AssocCtxt::Trait) => None,
493+
(AssocItemKind::Fn(_, _, None), AssocCtxt::Trait) => None,
494494
_ => Some(item.id),
495495
};
496496
self.with_hir_id_owner(owner, |this| visit::walk_assoc_item(this, item, ctxt));

0 commit comments

Comments
 (0)