Skip to content

Commit 5518eaa

Browse files
committed
Auto merge of rust-lang#120466 - Dylan-DPC:rollup-v0or19a, r=Dylan-DPC
Rollup of 9 pull requests Successful merges: - rust-lang#116677 (References refer to allocated objects) - rust-lang#118625 (Improve handling of expressions in patterns) - rust-lang#120266 (Improve documentation for [A]Rc::into_inner) - rust-lang#120373 (Adjust Behaviour of `read_dir` and `ReadDir` in Windows Implementation: Check Whether Path to Search In Exists) - rust-lang#120390 (Borrow check inline const patterns) - rust-lang#120420 (Stop using derivative in rustc_pattern_analysis) - rust-lang#120428 (hir: Two preparatory changes for rust-lang#120206) - rust-lang#120453 (Fix incorrect comment in normalize_newlines) - rust-lang#120462 (Clean dead code) r? `@ghost` `@rustbot` modify labels: rollup
2 parents af08c64 + c70c4cc commit 5518eaa

Some content is hidden

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

62 files changed

+1281
-159
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -4342,7 +4342,6 @@ dependencies = [
43424342
name = "rustc_pattern_analysis"
43434343
version = "0.0.0"
43444344
dependencies = [
4345-
"derivative",
43464345
"rustc-hash",
43474346
"rustc_apfloat",
43484347
"rustc_arena",

compiler/rustc_ast_lowering/src/expr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
153153
}
154154
ExprKind::Let(pat, scrutinee, span, is_recovered) => {
155155
hir::ExprKind::Let(self.arena.alloc(hir::Let {
156-
hir_id: self.next_id(),
157156
span: self.lower_span(*span),
158157
pat: self.lower_pat(pat),
159158
ty: None,

compiler/rustc_ast_lowering/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23052305
match c.value.kind {
23062306
ExprKind::Underscore => {
23072307
if self.tcx.features().generic_arg_infer {
2308-
hir::ArrayLen::Infer(self.lower_node_id(c.id), self.lower_span(c.value.span))
2308+
hir::ArrayLen::Infer(hir::InferArg {
2309+
hir_id: self.lower_node_id(c.id),
2310+
span: self.lower_span(c.value.span),
2311+
})
23092312
} else {
23102313
feature_err(
23112314
&self.tcx.sess,

compiler/rustc_borrowck/src/type_check/mod.rs

+56-8
Original file line numberDiff line numberDiff line change
@@ -1099,10 +1099,17 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10991099
#[instrument(skip(self), level = "debug")]
11001100
fn check_user_type_annotations(&mut self) {
11011101
debug!(?self.user_type_annotations);
1102+
let tcx = self.tcx();
11021103
for user_annotation in self.user_type_annotations {
11031104
let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
11041105
let annotation = self.instantiate_canonical_with_fresh_inference_vars(span, user_ty);
1105-
self.ascribe_user_type(inferred_ty, annotation, span);
1106+
if let ty::UserType::TypeOf(def, args) = annotation
1107+
&& let DefKind::InlineConst = tcx.def_kind(def)
1108+
{
1109+
self.check_inline_const(inferred_ty, def.expect_local(), args, span);
1110+
} else {
1111+
self.ascribe_user_type(inferred_ty, annotation, span);
1112+
}
11061113
}
11071114
}
11081115

@@ -1195,6 +1202,36 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11951202
Ok(())
11961203
}
11971204

1205+
fn check_inline_const(
1206+
&mut self,
1207+
inferred_ty: Ty<'tcx>,
1208+
def_id: LocalDefId,
1209+
args: UserArgs<'tcx>,
1210+
span: Span,
1211+
) {
1212+
assert!(args.user_self_ty.is_none());
1213+
let tcx = self.tcx();
1214+
let const_ty = tcx.type_of(def_id).instantiate(tcx, args.args);
1215+
if let Err(terr) =
1216+
self.eq_types(const_ty, inferred_ty, Locations::All(span), ConstraintCategory::Boring)
1217+
{
1218+
span_bug!(
1219+
span,
1220+
"bad inline const pattern: ({:?} = {:?}) {:?}",
1221+
const_ty,
1222+
inferred_ty,
1223+
terr
1224+
);
1225+
}
1226+
let args = self.infcx.resolve_vars_if_possible(args.args);
1227+
let predicates = self.prove_closure_bounds(tcx, def_id, args, Locations::All(span));
1228+
self.normalize_and_prove_instantiated_predicates(
1229+
def_id.to_def_id(),
1230+
predicates,
1231+
Locations::All(span),
1232+
);
1233+
}
1234+
11981235
fn tcx(&self) -> TyCtxt<'tcx> {
11991236
self.infcx.tcx
12001237
}
@@ -1851,7 +1888,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
18511888
let def_id = uv.def;
18521889
if tcx.def_kind(def_id) == DefKind::InlineConst {
18531890
let def_id = def_id.expect_local();
1854-
let predicates = self.prove_closure_bounds(tcx, def_id, uv.args, location);
1891+
let predicates = self.prove_closure_bounds(
1892+
tcx,
1893+
def_id,
1894+
uv.args,
1895+
location.to_locations(),
1896+
);
18551897
self.normalize_and_prove_instantiated_predicates(
18561898
def_id.to_def_id(),
18571899
predicates,
@@ -2654,9 +2696,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26542696
// desugaring. A closure gets desugared to a struct, and
26552697
// these extra requirements are basically like where
26562698
// clauses on the struct.
2657-
AggregateKind::Closure(def_id, args) | AggregateKind::Coroutine(def_id, args) => {
2658-
(def_id, self.prove_closure_bounds(tcx, def_id.expect_local(), args, location))
2659-
}
2699+
AggregateKind::Closure(def_id, args) | AggregateKind::Coroutine(def_id, args) => (
2700+
def_id,
2701+
self.prove_closure_bounds(
2702+
tcx,
2703+
def_id.expect_local(),
2704+
args,
2705+
location.to_locations(),
2706+
),
2707+
),
26602708

26612709
AggregateKind::Array(_) | AggregateKind::Tuple => {
26622710
(CRATE_DEF_ID.to_def_id(), ty::InstantiatedPredicates::empty())
@@ -2675,7 +2723,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26752723
tcx: TyCtxt<'tcx>,
26762724
def_id: LocalDefId,
26772725
args: GenericArgsRef<'tcx>,
2678-
location: Location,
2726+
locations: Locations,
26792727
) -> ty::InstantiatedPredicates<'tcx> {
26802728
if let Some(closure_requirements) = &tcx.mir_borrowck(def_id).closure_requirements {
26812729
constraint_conversion::ConstraintConversion::new(
@@ -2684,7 +2732,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26842732
self.region_bound_pairs,
26852733
self.implicit_region_bound,
26862734
self.param_env,
2687-
location.to_locations(),
2735+
locations,
26882736
DUMMY_SP, // irrelevant; will be overridden.
26892737
ConstraintCategory::Boring, // same as above.
26902738
self.borrowck_context.constraints,
@@ -2710,7 +2758,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
27102758
if let Err(_) = self.eq_args(
27112759
typeck_root_args,
27122760
parent_args,
2713-
location.to_locations(),
2761+
locations,
27142762
ConstraintCategory::BoringNoLocation,
27152763
) {
27162764
span_mirbug!(

compiler/rustc_hir/src/hir.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,6 @@ pub struct Arm<'hir> {
12731273
/// desugaring to if-let. Only let-else supports the type annotation at present.
12741274
#[derive(Debug, Clone, Copy, HashStable_Generic)]
12751275
pub struct Let<'hir> {
1276-
pub hir_id: HirId,
12771276
pub span: Span,
12781277
pub pat: &'hir Pat<'hir>,
12791278
pub ty: Option<&'hir Ty<'hir>>,
@@ -1532,14 +1531,16 @@ pub type Lit = Spanned<LitKind>;
15321531

15331532
#[derive(Copy, Clone, Debug, HashStable_Generic)]
15341533
pub enum ArrayLen {
1535-
Infer(HirId, Span),
1534+
Infer(InferArg),
15361535
Body(AnonConst),
15371536
}
15381537

15391538
impl ArrayLen {
15401539
pub fn hir_id(&self) -> HirId {
15411540
match self {
1542-
&ArrayLen::Infer(hir_id, _) | &ArrayLen::Body(AnonConst { hir_id, .. }) => hir_id,
1541+
ArrayLen::Infer(InferArg { hir_id, .. }) | ArrayLen::Body(AnonConst { hir_id, .. }) => {
1542+
*hir_id
1543+
}
15431544
}
15441545
}
15451546
}
@@ -2424,7 +2425,7 @@ impl<'hir> Ty<'hir> {
24242425
TyKind::Infer => true,
24252426
TyKind::Slice(ty) => ty.is_suggestable_infer_ty(),
24262427
TyKind::Array(ty, length) => {
2427-
ty.is_suggestable_infer_ty() || matches!(length, ArrayLen::Infer(_, _))
2428+
ty.is_suggestable_infer_ty() || matches!(length, ArrayLen::Infer(..))
24282429
}
24292430
TyKind::Tup(tys) => tys.iter().any(Self::is_suggestable_infer_ty),
24302431
TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => mut_ty.ty.is_suggestable_infer_ty(),

compiler/rustc_hir/src/intravisit.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,6 @@ pub trait Visitor<'v>: Sized {
341341
fn visit_expr(&mut self, ex: &'v Expr<'v>) {
342342
walk_expr(self, ex)
343343
}
344-
fn visit_let_expr(&mut self, lex: &'v Let<'v>) {
345-
walk_let_expr(self, lex)
346-
}
347344
fn visit_expr_field(&mut self, field: &'v ExprField<'v>) {
348345
walk_expr_field(self, field)
349346
}
@@ -672,7 +669,7 @@ pub fn walk_pat_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v PatField<'
672669

673670
pub fn walk_array_len<'v, V: Visitor<'v>>(visitor: &mut V, len: &'v ArrayLen) {
674671
match len {
675-
&ArrayLen::Infer(hir_id, _span) => visitor.visit_id(hir_id),
672+
ArrayLen::Infer(InferArg { hir_id, span: _ }) => visitor.visit_id(*hir_id),
676673
ArrayLen::Body(c) => visitor.visit_anon_const(c),
677674
}
678675
}
@@ -729,7 +726,12 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
729726
ExprKind::DropTemps(ref subexpression) => {
730727
visitor.visit_expr(subexpression);
731728
}
732-
ExprKind::Let(ref let_expr) => visitor.visit_let_expr(let_expr),
729+
ExprKind::Let(Let { span: _, pat, ty, init, is_recovered: _ }) => {
730+
// match the visit order in walk_local
731+
visitor.visit_expr(init);
732+
visitor.visit_pat(pat);
733+
walk_list!(visitor, visit_ty, ty);
734+
}
733735
ExprKind::If(ref cond, ref then, ref else_opt) => {
734736
visitor.visit_expr(cond);
735737
visitor.visit_expr(then);
@@ -806,14 +808,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
806808
}
807809
}
808810

809-
pub fn walk_let_expr<'v, V: Visitor<'v>>(visitor: &mut V, let_expr: &'v Let<'v>) {
810-
// match the visit order in walk_local
811-
visitor.visit_expr(let_expr.init);
812-
visitor.visit_id(let_expr.hir_id);
813-
visitor.visit_pat(let_expr.pat);
814-
walk_list!(visitor, visit_ty, let_expr.ty);
815-
}
816-
817811
pub fn walk_expr_field<'v, V: Visitor<'v>>(visitor: &mut V, field: &'v ExprField<'v>) {
818812
visitor.visit_id(field.hir_id);
819813
visitor.visit_ident(field.ident);

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
25292529
}
25302530
hir::TyKind::Array(ty, length) => {
25312531
let length = match length {
2532-
&hir::ArrayLen::Infer(_, span) => self.ct_infer(tcx.types.usize, None, span),
2532+
hir::ArrayLen::Infer(inf) => self.ct_infer(tcx.types.usize, None, inf.span),
25332533
hir::ArrayLen::Body(constant) => {
25342534
ty::Const::from_anon_const(tcx, constant.def_id)
25352535
}

compiler/rustc_hir_analysis/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ impl<'v> Visitor<'v> for HirPlaceholderCollector {
146146
}
147147
}
148148
fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
149-
if let &hir::ArrayLen::Infer(_, span) = length {
150-
self.0.push(span);
149+
if let hir::ArrayLen::Infer(inf) = length {
150+
self.0.push(inf.span);
151151
}
152152
intravisit::walk_array_len(self, length)
153153
}

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ impl<'a> State<'a> {
956956

957957
fn print_array_length(&mut self, len: &hir::ArrayLen) {
958958
match len {
959-
hir::ArrayLen::Infer(_, _) => self.word("_"),
959+
hir::ArrayLen::Infer(..) => self.word("_"),
960960
hir::ArrayLen::Body(ct) => self.print_anon_const(ct),
961961
}
962962
}

compiler/rustc_hir_typeck/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
320320
}
321321
ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
322322
ExprKind::Become(call) => self.check_expr_become(call, expr),
323-
ExprKind::Let(let_expr) => self.check_expr_let(let_expr),
323+
ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id),
324324
ExprKind::Loop(body, _, source, _) => {
325325
self.check_expr_loop(body, source, expected, expr)
326326
}
@@ -1259,12 +1259,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12591259
}
12601260
}
12611261

1262-
pub(super) fn check_expr_let(&self, let_expr: &'tcx hir::Let<'tcx>) -> Ty<'tcx> {
1262+
pub(super) fn check_expr_let(&self, let_expr: &'tcx hir::Let<'tcx>, hir_id: HirId) -> Ty<'tcx> {
12631263
// for let statements, this is done in check_stmt
12641264
let init = let_expr.init;
12651265
self.warn_if_unreachable(init.hir_id, init.span, "block in `let` expression");
12661266
// otherwise check exactly as a let statement
1267-
self.check_decl(let_expr.into());
1267+
self.check_decl((let_expr, hir_id).into());
12681268
// but return a bool, for this is a boolean expression
12691269
if let Some(error_guaranteed) = let_expr.is_recovered {
12701270
self.set_tainted_by_errors(error_guaranteed);

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
405405

406406
pub fn array_length_to_const(&self, length: &hir::ArrayLen) -> ty::Const<'tcx> {
407407
match length {
408-
&hir::ArrayLen::Infer(_, span) => self.ct_infer(self.tcx.types.usize, None, span),
408+
hir::ArrayLen::Infer(inf) => self.ct_infer(self.tcx.types.usize, None, inf.span),
409409
hir::ArrayLen::Body(anon_const) => {
410410
let span = self.tcx.def_span(anon_const.def_id);
411411
let c = ty::Const::from_anon_const(self.tcx, anon_const.def_id);

compiler/rustc_hir_typeck/src/gather_locals.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ impl<'a> From<&'a hir::Local<'a>> for Declaration<'a> {
4848
}
4949
}
5050

51-
impl<'a> From<&'a hir::Let<'a>> for Declaration<'a> {
52-
fn from(let_expr: &'a hir::Let<'a>) -> Self {
53-
let hir::Let { hir_id, pat, ty, span, init, is_recovered: _ } = *let_expr;
51+
impl<'a> From<(&'a hir::Let<'a>, hir::HirId)> for Declaration<'a> {
52+
fn from((let_expr, hir_id): (&'a hir::Let<'a>, hir::HirId)) -> Self {
53+
let hir::Let { pat, ty, span, init, is_recovered: _ } = *let_expr;
5454
Declaration { hir_id, pat, ty, span, init: Some(init), origin: DeclOrigin::LetExpr }
5555
}
5656
}
@@ -125,9 +125,11 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
125125
intravisit::walk_local(self, local)
126126
}
127127

128-
fn visit_let_expr(&mut self, let_expr: &'tcx hir::Let<'tcx>) {
129-
self.declare(let_expr.into());
130-
intravisit::walk_let_expr(self, let_expr);
128+
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
129+
if let hir::ExprKind::Let(let_expr) = expr.kind {
130+
self.declare((let_expr, expr.hir_id).into());
131+
}
132+
intravisit::walk_expr(self, expr)
131133
}
132134

133135
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {

compiler/rustc_mir_build/src/build/matches/simplify.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use crate::build::expr::as_place::PlaceBuilder;
1616
use crate::build::matches::{Ascription, Binding, Candidate, MatchPair};
1717
use crate::build::Builder;
18+
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1819
use rustc_middle::thir::{self, *};
20+
use rustc_middle::ty;
1921

2022
use std::mem;
2123

@@ -149,7 +151,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
149151
ref subpattern,
150152
ascription: thir::Ascription { ref annotation, variance },
151153
} => {
152-
// Apply the type ascription to the value at `match_pair.place`, which is the
154+
// Apply the type ascription to the value at `match_pair.place`
153155
if let Some(source) = match_pair.place.try_to_place(self) {
154156
candidate.ascriptions.push(Ascription {
155157
annotation: annotation.clone(),
@@ -205,7 +207,38 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
205207
Err(match_pair)
206208
}
207209

208-
PatKind::InlineConstant { subpattern: ref pattern, def: _ } => {
210+
PatKind::InlineConstant { subpattern: ref pattern, def } => {
211+
// Apply a type ascription for the inline constant to the value at `match_pair.place`
212+
if let Some(source) = match_pair.place.try_to_place(self) {
213+
let span = match_pair.pattern.span;
214+
let parent_id = self.tcx.typeck_root_def_id(self.def_id.to_def_id());
215+
let args = ty::InlineConstArgs::new(
216+
self.tcx,
217+
ty::InlineConstArgsParts {
218+
parent_args: ty::GenericArgs::identity_for_item(self.tcx, parent_id),
219+
ty: self.infcx.next_ty_var(TypeVariableOrigin {
220+
kind: TypeVariableOriginKind::MiscVariable,
221+
span,
222+
}),
223+
},
224+
)
225+
.args;
226+
let user_ty =
227+
self.infcx.canonicalize_user_type_annotation(ty::UserType::TypeOf(
228+
def.to_def_id(),
229+
ty::UserArgs { args, user_self_ty: None },
230+
));
231+
let annotation = ty::CanonicalUserTypeAnnotation {
232+
inferred_ty: pattern.ty,
233+
span,
234+
user_ty: Box::new(user_ty),
235+
};
236+
candidate.ascriptions.push(Ascription {
237+
annotation,
238+
source,
239+
variance: ty::Contravariant,
240+
});
241+
}
209242
candidate.match_pairs.push(MatchPair::new(match_pair.place, pattern, self));
210243

211244
Ok(())

compiler/rustc_parse/messages.ftl

+14
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,20 @@ parse_unexpected_const_param_declaration = unexpected `const` parameter declarat
772772
parse_unexpected_default_value_for_lifetime_in_generic_parameters = unexpected default lifetime parameter
773773
.label = lifetime parameters cannot have default values
774774
775+
parse_unexpected_expr_in_pat =
776+
expected {$is_bound ->
777+
[true] a pattern range bound
778+
*[false] a pattern
779+
}, found {$is_method_call ->
780+
[true] a method call
781+
*[false] an expression
782+
}
783+
784+
.label = {$is_method_call ->
785+
[true] method calls
786+
*[false] arbitrary expressions
787+
} are not allowed in patterns
788+
775789
parse_unexpected_if_with_if = unexpected `if` in the condition expression
776790
.suggestion = remove the `if`
777791

0 commit comments

Comments
 (0)