Skip to content

Commit 8d5096b

Browse files
committed
Do not error on let ref x: str = *"";
1 parent 17ed1ff commit 8d5096b

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

compiler/rustc_hir_typeck/src/gather_locals.rs

+35-18
Original file line numberDiff line numberDiff line change
@@ -105,24 +105,41 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
105105
.insert(hir_ty.hir_id, c_ty);
106106

107107
let ty = o_ty.normalized;
108-
if let hir::PatKind::Wild = decl.pat.kind {
109-
// We explicitly allow `let _: dyn Trait;` (!)
110-
} else {
111-
if self.outermost_fn_param_pat.is_some() {
112-
if !self.fcx.tcx.features().unsized_fn_params {
113-
self.fcx.require_type_is_sized(
114-
ty,
115-
hir_ty.span,
116-
traits::SizedArgumentType(Some(decl.pat.hir_id)),
117-
);
118-
}
119-
} else {
120-
if !self.fcx.tcx.features().unsized_locals {
121-
self.fcx.require_type_is_sized(
122-
ty,
123-
hir_ty.span,
124-
traits::VariableType(decl.pat.hir_id),
125-
);
108+
match decl.pat.kind {
109+
// We explicitly allow `let ref x: str = *"";`
110+
hir::PatKind::Binding(hir::BindingAnnotation(hir::ByRef::Yes(_), _), ..) => {}
111+
// We explicitly allow `let _: dyn Trait;` and allow the `visit_pat` check to
112+
// handle `let (x, _): (sized, str) = *r;`. Otherwise with the later we'd
113+
// complain incorrectly about the `str` that is otherwise unused.
114+
_ if {
115+
let mut is_wild = false;
116+
decl.pat.walk(|pat| {
117+
if let hir::PatKind::Wild = pat.kind {
118+
is_wild = true;
119+
false
120+
} else {
121+
true
122+
}
123+
});
124+
is_wild
125+
} => {}
126+
_ => {
127+
if self.outermost_fn_param_pat.is_some() {
128+
if !self.fcx.tcx.features().unsized_fn_params {
129+
self.fcx.require_type_is_sized(
130+
ty,
131+
hir_ty.span,
132+
traits::SizedArgumentType(Some(decl.pat.hir_id)),
133+
);
134+
}
135+
} else {
136+
if !self.fcx.tcx.features().unsized_locals {
137+
self.fcx.require_type_is_sized(
138+
ty,
139+
hir_ty.span,
140+
traits::VariableType(decl.pat.hir_id),
141+
);
142+
}
126143
}
127144
}
128145
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ build-pass
2+
#![allow(unused)]
3+
4+
fn main() {
5+
let ref x: str = *"";
6+
}
7+
8+
fn foo(r: &(usize, str)) -> usize {
9+
let (x, _): (usize, str) = *r;
10+
x
11+
}

0 commit comments

Comments
 (0)