Skip to content

Commit 40624dd

Browse files
committed
Auto merge of rust-lang#79345 - jonas-schievink:rollup-1yhhzx9, r=jonas-schievink
Rollup of 10 pull requests Successful merges: - rust-lang#76829 (stabilize const_int_pow) - rust-lang#79080 (MIR visitor: Don't treat debuginfo field access as a use of the struct) - rust-lang#79236 (const_generics: assert resolve hack causes an error) - rust-lang#79287 (Allow using generic trait methods in `const fn`) - rust-lang#79324 (Use Option::and_then instead of open-coding it) - rust-lang#79325 (Reduce boilerplate with the `?` operator) - rust-lang#79330 (Fix typo in comment) - rust-lang#79333 (doc typo) - rust-lang#79337 (Use Option::map instead of open coding it) - rust-lang#79343 (Add my (`@flip1995)` work mail to the mailmap) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 40cf721 + ea3c269 commit 40624dd

File tree

39 files changed

+283
-101
lines changed

39 files changed

+283
-101
lines changed

.mailmap

+1
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ Phil Dawes <[email protected]> Phil Dawes <[email protected]>
230230
Philipp Brüschweiler <[email protected]> <[email protected]>
231231
Philipp Brüschweiler <[email protected]> <[email protected]>
232232
Philipp Krones <[email protected]> flip1995 <[email protected]>
233+
233234
Philipp Matthias Schäfer <[email protected]>
234235
Przemysław Wesołek <[email protected]> Przemek Wesołek <[email protected]>
235236
Rafael Ávila de Espíndola <[email protected]> Rafael Avila de Espindola <espindola@dream.(none)>

compiler/rustc_builtin_macros/src/format_foreign.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -649,17 +649,13 @@ pub mod shell {
649649
impl<'a> Iterator for Substitutions<'a> {
650650
type Item = Substitution<'a>;
651651
fn next(&mut self) -> Option<Self::Item> {
652-
match parse_next_substitution(self.s) {
653-
Some((mut sub, tail)) => {
654-
self.s = tail;
655-
if let Some(InnerSpan { start, end }) = sub.position() {
656-
sub.set_position(start + self.pos, end + self.pos);
657-
self.pos += end;
658-
}
659-
Some(sub)
660-
}
661-
None => None,
652+
let (mut sub, tail) = parse_next_substitution(self.s)?;
653+
self.s = tail;
654+
if let Some(InnerSpan { start, end }) = sub.position() {
655+
sub.set_position(start + self.pos, end + self.pos);
656+
self.pos += end;
662657
}
658+
Some(sub)
663659
}
664660

665661
fn size_hint(&self) -> (usize, Option<usize>) {

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1152,10 +1152,7 @@ impl<'ll> MemberDescription<'ll> {
11521152
self.size.bits(),
11531153
self.align.bits() as u32,
11541154
self.offset.bits(),
1155-
match self.discriminant {
1156-
None => None,
1157-
Some(value) => Some(cx.const_u64(value)),
1158-
},
1155+
self.discriminant.map(|v| cx.const_u64(v)),
11591156
self.flags,
11601157
self.type_metadata,
11611158
)

compiler/rustc_codegen_ssa/src/mir/block.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
3737
/// `funclet_bb` member if it is not `None`.
3838
fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>(
3939
&self,
40-
fx: &'b mut FunctionCx<'a, 'tcx, Bx>,
40+
fx: &'b FunctionCx<'a, 'tcx, Bx>,
4141
) -> Option<&'b Bx::Funclet> {
42-
match self.funclet_bb {
43-
Some(funcl) => fx.funclets[funcl].as_ref(),
44-
None => None,
45-
}
42+
self.funclet_bb.and_then(|funcl| fx.funclets[funcl].as_ref())
4643
}
4744

4845
fn lltarget<Bx: BuilderMethods<'a, 'tcx>>(

compiler/rustc_infer/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToT
309309
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
310310
while let Some(obligation) = self.base_iterator.next() {
311311
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
312-
return Some(data);
312+
return Some(data.value);
313313
}
314314
}
315315
None

compiler/rustc_middle/src/mir/visit.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1017,11 +1017,14 @@ macro_rules! visit_place_fns {
10171017
let mut context = context;
10181018

10191019
if !place.projection.is_empty() {
1020-
context = if context.is_mutating_use() {
1021-
PlaceContext::MutatingUse(MutatingUseContext::Projection)
1022-
} else {
1023-
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1024-
};
1020+
if context.is_use() {
1021+
// ^ Only change the context if it is a real use, not a "use" in debuginfo.
1022+
context = if context.is_mutating_use() {
1023+
PlaceContext::MutatingUse(MutatingUseContext::Projection)
1024+
} else {
1025+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection)
1026+
};
1027+
}
10251028
}
10261029

10271030
self.visit_local(&place.local, context, location);

compiler/rustc_middle/src/traits/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::ty::{self, AdtKind, Ty, TyCtxt};
1616
use rustc_errors::{Applicability, DiagnosticBuilder};
1717
use rustc_hir as hir;
1818
use rustc_hir::def_id::DefId;
19+
use rustc_hir::Constness;
1920
use rustc_span::symbol::Symbol;
2021
use rustc_span::{Span, DUMMY_SP};
2122
use smallvec::SmallVec;
@@ -457,7 +458,7 @@ pub enum ImplSource<'tcx, N> {
457458
/// for some type parameter. The `Vec<N>` represents the
458459
/// obligations incurred from normalizing the where-clause (if
459460
/// any).
460-
Param(Vec<N>),
461+
Param(Vec<N>, Constness),
461462

462463
/// Virtual calls through an object.
463464
Object(ImplSourceObjectData<'tcx, N>),
@@ -487,7 +488,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
487488
pub fn nested_obligations(self) -> Vec<N> {
488489
match self {
489490
ImplSource::UserDefined(i) => i.nested,
490-
ImplSource::Param(n) => n,
491+
ImplSource::Param(n, _) => n,
491492
ImplSource::Builtin(i) => i.nested,
492493
ImplSource::AutoImpl(d) => d.nested,
493494
ImplSource::Closure(c) => c.nested,
@@ -502,7 +503,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
502503
pub fn borrow_nested_obligations(&self) -> &[N] {
503504
match &self {
504505
ImplSource::UserDefined(i) => &i.nested[..],
505-
ImplSource::Param(n) => &n[..],
506+
ImplSource::Param(n, _) => &n[..],
506507
ImplSource::Builtin(i) => &i.nested[..],
507508
ImplSource::AutoImpl(d) => &d.nested[..],
508509
ImplSource::Closure(c) => &c.nested[..],
@@ -524,7 +525,7 @@ impl<'tcx, N> ImplSource<'tcx, N> {
524525
substs: i.substs,
525526
nested: i.nested.into_iter().map(f).collect(),
526527
}),
527-
ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()),
528+
ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct),
528529
ImplSource::Builtin(i) => ImplSource::Builtin(ImplSourceBuiltinData {
529530
nested: i.nested.into_iter().map(f).collect(),
530531
}),

compiler/rustc_middle/src/traits/select.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub enum SelectionCandidate<'tcx> {
101101
/// `false` if there are no *further* obligations.
102102
has_nested: bool,
103103
},
104-
ParamCandidate(ty::PolyTraitRef<'tcx>),
104+
ParamCandidate(ty::ConstnessAnd<ty::PolyTraitRef<'tcx>>),
105105
ImplCandidate(DefId),
106106
AutoImplCandidate(DefId),
107107

compiler/rustc_middle/src/traits/structural_impls.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
2121

2222
super::ImplSource::Object(ref d) => write!(f, "{:?}", d),
2323

24-
super::ImplSource::Param(ref n) => write!(f, "ImplSourceParamData({:?})", n),
24+
super::ImplSource::Param(ref n, ct) => {
25+
write!(f, "ImplSourceParamData({:?}, {:?})", n, ct)
26+
}
2527

2628
super::ImplSource::Builtin(ref d) => write!(f, "{:?}", d),
2729

compiler/rustc_middle/src/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
4242
use rustc_hir::definitions::{DefPathHash, Definitions};
4343
use rustc_hir::intravisit::Visitor;
4444
use rustc_hir::lang_items::LangItem;
45-
use rustc_hir::{HirId, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet, Node, TraitCandidate};
45+
use rustc_hir::{
46+
Constness, HirId, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet, Node, TraitCandidate,
47+
};
4648
use rustc_index::vec::{Idx, IndexVec};
4749
use rustc_macros::HashStable;
4850
use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames};
@@ -1635,6 +1637,8 @@ nop_list_lift! {projs; ProjectionKind => ProjectionKind}
16351637
// This is the impl for `&'a InternalSubsts<'a>`.
16361638
nop_list_lift! {substs; GenericArg<'a> => GenericArg<'tcx>}
16371639

1640+
CloneLiftImpls! { for<'tcx> { Constness, } }
1641+
16381642
pub mod tls {
16391643
use super::{ptr_eq, GlobalCtxt, TyCtxt};
16401644

compiler/rustc_middle/src/ty/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1503,9 +1503,11 @@ impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> {
15031503
}
15041504

15051505
impl<'tcx> Predicate<'tcx> {
1506-
pub fn to_opt_poly_trait_ref(self) -> Option<PolyTraitRef<'tcx>> {
1506+
pub fn to_opt_poly_trait_ref(self) -> Option<ConstnessAnd<PolyTraitRef<'tcx>>> {
15071507
match self.skip_binders() {
1508-
PredicateAtom::Trait(t, _) => Some(ty::Binder::bind(t.trait_ref)),
1508+
PredicateAtom::Trait(t, constness) => {
1509+
Some(ConstnessAnd { constness, value: ty::Binder::bind(t.trait_ref) })
1510+
}
15091511
PredicateAtom::Projection(..)
15101512
| PredicateAtom::Subtype(..)
15111513
| PredicateAtom::RegionOutlives(..)
@@ -1947,7 +1949,7 @@ impl<'tcx> ParamEnv<'tcx> {
19471949
}
19481950
}
19491951

1950-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1952+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TypeFoldable)]
19511953
pub struct ConstnessAnd<T> {
19521954
pub constness: Constness,
19531955
pub value: T,

compiler/rustc_middle/src/ty/sty.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,7 @@ impl<T> Binder<T> {
11061106

11071107
impl<T> Binder<Option<T>> {
11081108
pub fn transpose(self) -> Option<Binder<T>> {
1109-
match self.0 {
1110-
Some(v) => Some(Binder(v)),
1111-
None => None,
1112-
}
1109+
self.0.map(Binder)
11131110
}
11141111
}
11151112

compiler/rustc_mir/src/transform/check_consts/validation.rs

+36-4
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ use rustc_errors::{struct_span_err, Applicability, Diagnostic, ErrorReported};
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{self as hir, HirId, LangItem};
66
use rustc_infer::infer::TyCtxtInferExt;
7+
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
78
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
89
use rustc_middle::mir::*;
910
use rustc_middle::ty::cast::CastTy;
1011
use rustc_middle::ty::subst::GenericArgKind;
1112
use rustc_middle::ty::{
1213
self, adjustment::PointerCast, Instance, InstanceDef, Ty, TyCtxt, TypeAndMut,
1314
};
15+
use rustc_middle::ty::{Binder, TraitPredicate, TraitRef};
1416
use rustc_span::{sym, Span, Symbol};
1517
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
16-
use rustc_trait_selection::traits::{self, TraitEngine};
18+
use rustc_trait_selection::traits::{self, SelectionContext, TraitEngine};
1719

1820
use std::mem;
1921
use std::ops::Deref;
@@ -765,9 +767,39 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
765767
}
766768
};
767769

768-
// Resolve a trait method call to its concrete implementation, which may be in a
769-
// `const` trait impl.
770-
if self.tcx.features().const_trait_impl {
770+
// Attempting to call a trait method?
771+
if let Some(trait_id) = tcx.trait_of_item(callee) {
772+
if !self.tcx.features().const_trait_impl {
773+
self.check_op(ops::FnCallNonConst(callee));
774+
return;
775+
}
776+
777+
let trait_ref = TraitRef::from_method(tcx, trait_id, substs);
778+
let obligation = Obligation::new(
779+
ObligationCause::dummy(),
780+
param_env,
781+
Binder::bind(TraitPredicate {
782+
trait_ref: TraitRef::from_method(tcx, trait_id, substs),
783+
}),
784+
);
785+
786+
let implsrc = tcx.infer_ctxt().enter(|infcx| {
787+
let mut selcx = SelectionContext::new(&infcx);
788+
selcx.select(&obligation).unwrap()
789+
});
790+
791+
// If the method is provided via a where-clause that does not use the `?const`
792+
// opt-out, the call is allowed.
793+
if let Some(ImplSource::Param(_, hir::Constness::Const)) = implsrc {
794+
debug!(
795+
"const_trait_impl: provided {:?} via where-clause in {:?}",
796+
trait_ref, param_env
797+
);
798+
return;
799+
}
800+
801+
// Resolve a trait method call to its concrete implementation, which may be in a
802+
// `const` trait impl.
771803
let instance = Instance::resolve(tcx, param_env, callee, substs);
772804
debug!("Resolving ({:?}) -> {:?}", callee, instance);
773805
if let Ok(Some(func)) = instance {

compiler/rustc_mir/src/transform/validate.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::mir::traversal;
1212
use rustc_middle::mir::visit::{PlaceContext, Visitor};
1313
use rustc_middle::mir::{
1414
AggregateKind, BasicBlock, Body, BorrowKind, Local, Location, MirPhase, Operand, PlaceRef,
15-
Rvalue, SourceScope, Statement, StatementKind, Terminator, TerminatorKind, VarDebugInfo,
15+
Rvalue, SourceScope, Statement, StatementKind, Terminator, TerminatorKind,
1616
};
1717
use rustc_middle::ty::fold::BottomUpFolder;
1818
use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt, TypeFoldable};
@@ -200,12 +200,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
200200
}
201201
}
202202

203-
fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) {
204-
// Debuginfo can contain field projections, which count as a use of the base local. Skip
205-
// debuginfo so that we avoid the storage liveness assertion in that case.
206-
self.visit_source_info(&var_debug_info.source_info);
207-
}
208-
209203
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
210204
// This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
211205
if self.tcx.sess.opts.debugging_opts.validate_mir {

compiler/rustc_privacy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
691691
hir::ItemKind::GlobalAsm(..) => {}
692692
hir::ItemKind::OpaqueTy(..) => {
693693
// HACK(jynelson): trying to infer the type of `impl trait` breaks `async-std` (and `pub async fn` in general)
694-
// Since rustdoc never need to do codegen and doesn't care about link-time reachability,
694+
// Since rustdoc never needs to do codegen and doesn't care about link-time reachability,
695695
// mark this as unreachable.
696696
// See https://github.com/rust-lang/rust/issues/75100
697697
if !self.tcx.sess.opts.actually_rustdoc {

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a> Resolver<'a> {
143143
_ => {
144144
bug!(
145145
"GenericParamsFromOuterFunction should only be used with Res::SelfTy, \
146-
DefKind::TyParam"
146+
DefKind::TyParam or DefKind::ConstParam"
147147
);
148148
}
149149
}

compiler/rustc_resolve/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2539,6 +2539,7 @@ impl<'a> Resolver<'a> {
25392539
span: Span,
25402540
all_ribs: &[Rib<'a>],
25412541
) -> Res {
2542+
const CG_BUG_STR: &str = "min_const_generics resolve check didn't stop compilation";
25422543
debug!("validate_res_from_ribs({:?})", res);
25432544
let ribs = &all_ribs[rib_index + 1..];
25442545

@@ -2639,6 +2640,8 @@ impl<'a> Resolver<'a> {
26392640
},
26402641
);
26412642
}
2643+
2644+
self.session.delay_span_bug(span, CG_BUG_STR);
26422645
return Res::Err;
26432646
}
26442647
}
@@ -2720,6 +2723,8 @@ impl<'a> Resolver<'a> {
27202723
},
27212724
);
27222725
}
2726+
2727+
self.session.delay_span_bug(span, CG_BUG_STR);
27232728
return Res::Err;
27242729
}
27252730

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
350350

351351
// Micro-optimization: filter out predicates relating to different traits.
352352
let matching_bounds =
353-
all_bounds.filter(|p| p.def_id() == stack.obligation.predicate.def_id());
353+
all_bounds.filter(|p| p.value.def_id() == stack.obligation.predicate.def_id());
354354

355355
// Keep only those bounds which may apply, and propagate overflow if it occurs.
356356
for bound in matching_bounds {
357-
let wc = self.evaluate_where_clause(stack, bound)?;
357+
let wc = self.evaluate_where_clause(stack, bound.value)?;
358358
if wc.may_apply() {
359359
candidates.vec.push(ParamCandidate(bound));
360360
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! https://rustc-dev-guide.rust-lang.org/traits/resolution.html#confirmation
99
use rustc_data_structures::stack::ensure_sufficient_stack;
1010
use rustc_hir::lang_items::LangItem;
11+
use rustc_hir::Constness;
1112
use rustc_index::bit_set::GrowableBitSet;
1213
use rustc_infer::infer::InferOk;
1314
use rustc_infer::infer::LateBoundRegionConversionTime::HigherRankedType;
@@ -55,8 +56,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
5556
}
5657

5758
ParamCandidate(param) => {
58-
let obligations = self.confirm_param_candidate(obligation, param);
59-
Ok(ImplSource::Param(obligations))
59+
let obligations = self.confirm_param_candidate(obligation, param.value);
60+
Ok(ImplSource::Param(obligations, param.constness))
6061
}
6162

6263
ImplCandidate(impl_def_id) => {
@@ -70,7 +71,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
7071

7172
ProjectionCandidate(idx) => {
7273
let obligations = self.confirm_projection_candidate(obligation, idx)?;
73-
Ok(ImplSource::Param(obligations))
74+
// FIXME(jschievink): constness
75+
Ok(ImplSource::Param(obligations, Constness::NotConst))
7476
}
7577

7678
ObjectCandidate(idx) => {
@@ -106,7 +108,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
106108
// This indicates something like `Trait + Send: Send`. In this case, we know that
107109
// this holds because that's what the object type is telling us, and there's really
108110
// no additional obligations to prove and no types in particular to unify, etc.
109-
Ok(ImplSource::Param(Vec::new()))
111+
Ok(ImplSource::Param(Vec::new(), Constness::NotConst))
110112
}
111113

112114
BuiltinUnsizeCandidate => {
@@ -151,7 +153,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
151153
obligations.extend(self.infcx.commit_if_ok(|_| {
152154
self.infcx
153155
.at(&obligation.cause, obligation.param_env)
154-
.sup(placeholder_trait_predicate.trait_ref.to_poly_trait_ref(), candidate)
156+
.sup(placeholder_trait_predicate.trait_ref.to_poly_trait_ref(), candidate.value)
155157
.map(|InferOk { obligations, .. }| obligations)
156158
.map_err(|_| Unimplemented)
157159
})?);

0 commit comments

Comments
 (0)