Skip to content

Commit dfe0d21

Browse files
committed
Auto merge of rust-lang#2971 - rust-lang:rustup2023-07-08, r=RalfJung
Automatic sync from rustc
2 parents 2323ecb + c182669 commit dfe0d21

File tree

116 files changed

+1049
-405
lines changed

Some content is hidden

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

116 files changed

+1049
-405
lines changed

compiler/rustc_abi/src/layout.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub trait LayoutCalculator {
134134
scalar_valid_range: (Bound<u128>, Bound<u128>),
135135
discr_range_of_repr: impl Fn(i128, i128) -> (Integer, bool),
136136
discriminants: impl Iterator<Item = (VariantIdx, i128)>,
137-
niche_optimize_enum: bool,
137+
dont_niche_optimize_enum: bool,
138138
always_sized: bool,
139139
) -> Option<LayoutS> {
140140
let dl = self.current_data_layout();
@@ -183,10 +183,10 @@ pub trait LayoutCalculator {
183183
// (Typechecking will reject discriminant-sizing attrs.)
184184

185185
let v = present_first;
186-
let kind = if is_enum || variants[v].is_empty() {
186+
let kind = if is_enum || variants[v].is_empty() || always_sized {
187187
StructKind::AlwaysSized
188188
} else {
189-
if !always_sized { StructKind::MaybeUnsized } else { StructKind::AlwaysSized }
189+
StructKind::MaybeUnsized
190190
};
191191

192192
let mut st = self.univariant(dl, &variants[v], repr, kind)?;
@@ -280,7 +280,7 @@ pub trait LayoutCalculator {
280280
}
281281

282282
let calculate_niche_filling_layout = || -> Option<TmpLayout> {
283-
if niche_optimize_enum {
283+
if dont_niche_optimize_enum {
284284
return None;
285285
}
286286

compiler/rustc_ast/src/tokenstream.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2525
use rustc_span::{Span, DUMMY_SP};
2626
use smallvec::{smallvec, SmallVec};
2727

28-
use std::{fmt, iter};
28+
use std::{fmt, iter, mem};
2929

3030
/// When the main Rust parser encounters a syntax-extension invocation, it
3131
/// parses the arguments to the invocation as a token tree. This is a very
@@ -410,8 +410,17 @@ impl TokenStream {
410410
t1.next().is_none() && t2.next().is_none()
411411
}
412412

413-
pub fn map_enumerated<F: FnMut(usize, &TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream {
414-
TokenStream(Lrc::new(self.0.iter().enumerate().map(|(i, tree)| f(i, tree)).collect()))
413+
/// Applies the supplied function to each `TokenTree` and its index in `self`, returning a new `TokenStream`
414+
///
415+
/// It is equivalent to `TokenStream::new(self.trees().cloned().enumerate().map(|(i, tt)| f(i, tt)).collect())`.
416+
pub fn map_enumerated_owned(
417+
mut self,
418+
mut f: impl FnMut(usize, TokenTree) -> TokenTree,
419+
) -> TokenStream {
420+
let owned = Lrc::make_mut(&mut self.0); // clone if necessary
421+
// rely on vec's in-place optimizations to avoid another allocation
422+
*owned = mem::take(owned).into_iter().enumerate().map(|(i, tree)| f(i, tree)).collect();
423+
self
415424
}
416425

417426
/// Create a token stream containing a single token with alone spacing.

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
1010
use rustc_middle::mir::*;
1111
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
1212
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt};
13-
use rustc_middle::ty::{Binder, TraitRef, TypeVisitableExt};
13+
use rustc_middle::ty::{TraitRef, TypeVisitableExt};
1414
use rustc_mir_dataflow::{self, Analysis};
1515
use rustc_span::{sym, Span, Symbol};
1616
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
@@ -755,10 +755,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
755755
}
756756

757757
let trait_ref = TraitRef::from_method(tcx, trait_id, substs);
758-
let poly_trait_pred =
759-
Binder::dummy(trait_ref).with_constness(ty::BoundConstness::ConstIfConst);
758+
let trait_ref = trait_ref.with_constness(ty::BoundConstness::ConstIfConst);
760759
let obligation =
761-
Obligation::new(tcx, ObligationCause::dummy(), param_env, poly_trait_pred);
760+
Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
762761

763762
let implsrc = {
764763
let infcx = tcx.infer_ctxt().build();
@@ -776,11 +775,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
776775
}
777776
// Closure: Fn{Once|Mut}
778777
Ok(Some(ImplSource::Builtin(_)))
779-
if poly_trait_pred.self_ty().skip_binder().is_closure()
778+
if trait_ref.self_ty().is_closure()
780779
&& tcx.fn_trait_kind_from_def_id(trait_id).is_some() =>
781780
{
782781
let ty::Closure(closure_def_id, substs) =
783-
*poly_trait_pred.self_ty().no_bound_vars().unwrap().kind()
782+
*trait_ref.self_ty().kind()
784783
else {
785784
unreachable!()
786785
};
@@ -840,7 +839,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
840839
tcx,
841840
ObligationCause::dummy_with_span(*fn_span),
842841
param_env,
843-
poly_trait_pred,
842+
trait_ref,
844843
);
845844

846845
// improve diagnostics by showing what failed. Our requirements are stricter this time

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
1010
use rustc_middle::mir::{self, CallSource};
1111
use rustc_middle::ty::print::with_no_trimmed_paths;
1212
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
13+
use rustc_middle::ty::TraitRef;
1314
use rustc_middle::ty::{suggest_constraining_type_param, Adt, Closure, FnDef, FnPtr, Param, Ty};
14-
use rustc_middle::ty::{Binder, TraitRef};
1515
use rustc_middle::util::{call_kind, CallDesugaringKind, CallKind};
1616
use rustc_session::parse::feature_err;
1717
use rustc_span::symbol::sym;
@@ -137,12 +137,8 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
137137
}
138138
}
139139
Adt(..) => {
140-
let obligation = Obligation::new(
141-
tcx,
142-
ObligationCause::dummy(),
143-
param_env,
144-
Binder::dummy(trait_ref),
145-
);
140+
let obligation =
141+
Obligation::new(tcx, ObligationCause::dummy(), param_env, trait_ref);
146142

147143
let infcx = tcx.infer_ctxt().build();
148144
let mut selcx = SelectionContext::new(&infcx);

compiler/rustc_error_messages/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn register_functions(bundle: &mut FluentBundle) {
226226
pub type LazyFallbackBundle = Lrc<Lazy<FluentBundle, impl FnOnce() -> FluentBundle>>;
227227

228228
/// Return the default `FluentBundle` with standard "en-US" diagnostic messages.
229-
#[instrument(level = "trace")]
229+
#[instrument(level = "trace", skip(resources))]
230230
pub fn fallback_fluent_bundle(
231231
resources: Vec<&'static str>,
232232
with_directionality_markers: bool,
@@ -242,7 +242,6 @@ pub fn fallback_fluent_bundle(
242242
for resource in resources {
243243
let resource = FluentResource::try_new(resource.to_string())
244244
.expect("failed to parse fallback fluent resource");
245-
trace!(?resource);
246245
fallback_bundle.add_resource_overriding(resource);
247246
}
248247

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ fn expand_macro<'cx>(
223223
// Replace all the tokens for the corresponding positions in the macro, to maintain
224224
// proper positions in error reporting, while maintaining the macro_backtrace.
225225
if tts.len() == rhs.tts.len() {
226-
tts = tts.map_enumerated(|i, tt| {
227-
let mut tt = tt.clone();
226+
tts = tts.map_enumerated_owned(|i, mut tt| {
228227
let rhs_tt = &rhs.tts[i];
229228
let ctxt = tt.span().ctxt();
230229
match (&mut tt, rhs_tt) {

compiler/rustc_hir_analysis/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ hir_analysis_static_specialize = cannot specialize on `'static` lifetime
248248
249249
hir_analysis_substs_on_overridden_impl = could not resolve substs on overridden impl
250250
251+
hir_analysis_tait_forward_compat = item constrains opaque type that is not in its signature
252+
.note = this item must mention the opaque type in its signature in order to be able to register hidden types
253+
251254
hir_analysis_target_feature_on_main = `main` function is not allowed to have `#[target_feature]`
252255
253256
hir_analysis_too_large_static = extern static is too large for the current architecture

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -981,7 +981,7 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
981981
// intermediate types must be sized.
982982
let needs_drop_copy = || {
983983
packed && {
984-
let ty = tcx.type_of(variant.fields.raw.last().unwrap().did).subst_identity();
984+
let ty = tcx.type_of(variant.tail().did).subst_identity();
985985
let ty = tcx.erase_regions(ty);
986986
if ty.has_infer() {
987987
tcx.sess

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::hir::nested_filter;
66
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
77
use rustc_span::DUMMY_SP;
88

9-
use crate::errors::UnconstrainedOpaqueType;
9+
use crate::errors::{TaitForwardCompat, UnconstrainedOpaqueType};
1010

1111
/// Checks "defining uses" of opaque `impl Trait` types to ensure that they meet the restrictions
1212
/// laid for "higher-order pattern unification".
@@ -139,6 +139,15 @@ impl TaitConstraintLocator<'_> {
139139
continue;
140140
}
141141
constrained = true;
142+
if !self.tcx.opaque_types_defined_by(item_def_id).contains(&self.def_id) {
143+
self.tcx.sess.emit_err(TaitForwardCompat {
144+
span: hidden_type.span,
145+
item_span: self
146+
.tcx
147+
.def_ident_span(item_def_id)
148+
.unwrap_or_else(|| self.tcx.def_span(item_def_id)),
149+
});
150+
}
142151
let concrete_type =
143152
self.tcx.erase_regions(hidden_type.remap_generic_params_to_declaration_params(
144153
opaque_type_key,

compiler/rustc_hir_analysis/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ pub struct UnconstrainedOpaqueType {
184184
pub what: &'static str,
185185
}
186186

187+
#[derive(Diagnostic)]
188+
#[diag(hir_analysis_tait_forward_compat)]
189+
#[note]
190+
pub struct TaitForwardCompat {
191+
#[primary_span]
192+
pub span: Span,
193+
#[note]
194+
pub item_span: Span,
195+
}
196+
187197
pub struct MissingTypeParams {
188198
pub span: Span,
189199
pub def_span: Span,

compiler/rustc_hir_typeck/src/cast.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
103103
Ok(match *t.kind() {
104104
ty::Slice(_) | ty::Str => Some(PointerKind::Length),
105105
ty::Dynamic(ref tty, _, ty::Dyn) => Some(PointerKind::VTable(tty.principal_def_id())),
106-
ty::Adt(def, substs) if def.is_struct() => {
107-
match def.non_enum_variant().fields.raw.last() {
108-
None => Some(PointerKind::Thin),
109-
Some(f) => {
110-
let field_ty = self.field_ty(span, f, substs);
111-
self.pointer_kind(field_ty, span)?
112-
}
106+
ty::Adt(def, substs) if def.is_struct() => match def.non_enum_variant().tail_opt() {
107+
None => Some(PointerKind::Thin),
108+
Some(f) => {
109+
let field_ty = self.field_ty(span, f, substs);
110+
self.pointer_kind(field_ty, span)?
113111
}
114-
}
112+
},
115113
ty::Tuple(fields) => match fields.last() {
116114
None => Some(PointerKind::Thin),
117115
Some(&f) => self.pointer_kind(f, span)?,

compiler/rustc_hir_typeck/src/coercion.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
632632
while !queue.is_empty() {
633633
let obligation = queue.remove(0);
634634
debug!("coerce_unsized resolve step: {:?}", obligation);
635-
let bound_predicate = obligation.predicate.kind();
636-
let trait_pred = match bound_predicate.skip_binder() {
637-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred))
635+
let trait_pred = match obligation.predicate.kind().no_bound_vars() {
636+
Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)))
638637
if traits.contains(&trait_pred.def_id()) =>
639638
{
640639
if unsize_did == trait_pred.def_id() {
@@ -652,7 +651,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
652651
has_unsized_tuple_coercion = true;
653652
}
654653
}
655-
bound_predicate.rebind(trait_pred)
654+
trait_pred
656655
}
657656
_ => {
658657
coercion.obligations.push(obligation);
@@ -664,8 +663,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
664663
Ok(None) => {
665664
if trait_pred.def_id() == unsize_did {
666665
let trait_pred = self.resolve_vars_if_possible(trait_pred);
667-
let self_ty = trait_pred.skip_binder().self_ty();
668-
let unsize_ty = trait_pred.skip_binder().trait_ref.substs[1].expect_ty();
666+
let self_ty = trait_pred.self_ty();
667+
let unsize_ty = trait_pred.trait_ref.substs[1].expect_ty();
669668
debug!("coerce_unsized: ambiguous unsize case for {:?}", trait_pred);
670669
match (self_ty.kind(), unsize_ty.kind()) {
671670
(&ty::Infer(ty::TyVar(v)), ty::Dynamic(..))

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19821982
self.tcx,
19831983
traits::ObligationCause::dummy(),
19841984
self.param_env,
1985-
ty::Binder::dummy(trait_ref),
1985+
trait_ref,
19861986
);
19871987
match SelectionContext::new(&self).select(&obligation) {
19881988
Ok(Some(traits::ImplSource::UserDefined(impl_source))) => {

compiler/rustc_hir_typeck/src/inherited.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'tcx> Inherited<'tcx> {
8080
let infcx = tcx
8181
.infer_ctxt()
8282
.ignoring_regions()
83-
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id))
83+
.with_opaque_type_inference(DefiningAnchor::Bind(def_id))
8484
.build();
8585
let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
8686

compiler/rustc_hir_typeck/src/method/probe.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1441,8 +1441,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14411441
trait_ref: ty::TraitRef<'tcx>,
14421442
) -> traits::SelectionResult<'tcx, traits::Selection<'tcx>> {
14431443
let cause = traits::ObligationCause::misc(self.span, self.body_id);
1444-
let predicate = ty::Binder::dummy(trait_ref);
1445-
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, predicate);
1444+
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, trait_ref);
14461445
traits::SelectionContext::new(self).select(&obligation)
14471446
}
14481447

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl<T> Trait<T> for X {
257257
);
258258
}
259259
}
260-
(ty::Alias(ty::Opaque, alias), _) | (_, ty::Alias(ty::Opaque, alias)) if alias.def_id.is_local() && matches!(tcx.def_kind(body_owner_def_id), DefKind::AssocFn | DefKind::AssocConst) => {
260+
(ty::Alias(ty::Opaque, alias), _) | (_, ty::Alias(ty::Opaque, alias)) if alias.def_id.is_local() && matches!(tcx.def_kind(body_owner_def_id), DefKind::Fn | DefKind::Static(_) | DefKind::Const | DefKind::AssocFn | DefKind::AssocConst) => {
261261
if tcx.is_type_alias_impl_trait(alias.def_id) {
262262
if !tcx.opaque_types_defined_by(body_owner_def_id.expect_local()).contains(&alias.def_id.expect_local()) {
263263
let sp = tcx.def_ident_span(body_owner_def_id).unwrap_or_else(|| tcx.def_span(body_owner_def_id));

compiler/rustc_infer/src/traits/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ impl<'tcx, P> From<Obligation<'tcx, P>> for solve::Goal<'tcx, P> {
6262
}
6363

6464
pub type PredicateObligation<'tcx> = Obligation<'tcx, ty::Predicate<'tcx>>;
65-
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
65+
pub type TraitObligation<'tcx> = Obligation<'tcx, ty::TraitPredicate<'tcx>>;
66+
pub type PolyTraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;
6667

6768
impl<'tcx> PredicateObligation<'tcx> {
6869
/// Flips the polarity of the inner predicate.
@@ -86,7 +87,7 @@ impl<'tcx> PredicateObligation<'tcx> {
8687
}
8788
}
8889

89-
impl<'tcx> TraitObligation<'tcx> {
90+
impl<'tcx> PolyTraitObligation<'tcx> {
9091
/// Returns `true` if the trait predicate is considered `const` in its ParamEnv.
9192
pub fn is_const(&self) -> bool {
9293
matches!(
@@ -193,7 +194,7 @@ impl<'tcx> FulfillmentError<'tcx> {
193194
}
194195
}
195196

196-
impl<'tcx> TraitObligation<'tcx> {
197+
impl<'tcx> PolyTraitObligation<'tcx> {
197198
pub fn polarity(&self) -> ty::ImplPolarity {
198199
self.predicate.skip_binder().polarity
199200
}

compiler/rustc_middle/src/query/keys.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
318318
}
319319
}
320320

321-
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>) {
321+
impl<'tcx> Key for (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>) {
322322
type CacheSelector = DefaultCacheSelector<Self>;
323323

324324
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
325-
tcx.def_span(self.1.def_id())
325+
tcx.def_span(self.1.def_id)
326326
}
327327
}
328328

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ rustc_queries! {
12781278
}
12791279

12801280
query codegen_select_candidate(
1281-
key: (ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)
1281+
key: (ty::ParamEnv<'tcx>, ty::TraitRef<'tcx>)
12821282
) -> Result<&'tcx ImplSource<'tcx, ()>, CodegenObligationError> {
12831283
cache_on_disk_if { true }
12841284
desc { |tcx| "computing candidate for `{}`", key.1 }

compiler/rustc_middle/src/ty/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,13 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> {
13061306
}
13071307
}
13081308

1309+
impl<'tcx> ToPredicate<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> {
1310+
#[inline(always)]
1311+
fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> {
1312+
self.without_const()
1313+
}
1314+
}
1315+
13091316
impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for TraitRef<'tcx> {
13101317
#[inline(always)]
13111318
fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> {
@@ -2028,6 +2035,22 @@ impl VariantDef {
20282035

20292036
&self.fields[FieldIdx::from_u32(0)]
20302037
}
2038+
2039+
/// Returns the last field in this variant, if present.
2040+
#[inline]
2041+
pub fn tail_opt(&self) -> Option<&FieldDef> {
2042+
self.fields.raw.last()
2043+
}
2044+
2045+
/// Returns the last field in this variant.
2046+
///
2047+
/// # Panics
2048+
///
2049+
/// Panics, if the variant has no fields.
2050+
#[inline]
2051+
pub fn tail(&self) -> &FieldDef {
2052+
self.tail_opt().expect("expected unsized ADT to have a tail field")
2053+
}
20312054
}
20322055

20332056
impl PartialEq for VariantDef {

0 commit comments

Comments
 (0)