Auto merge of #122119 - estebank:issue-117846, r=Nadrieril
Silence unecessary !Sized binding error When gathering locals, we introduce a `Sized` obligation for each binding in the pattern. *After* doing so, we typecheck the init expression. If this has a type failure, we store `{type error}`, for both the expression and the pattern. But later we store an inference variable for the pattern. We now avoid any override of an existing type on a hir node when they've already been marked as `{type error}`, and on E0277, when it comes from `VariableType` we silence the error in support of the type error. Fix https://github.com/rust-lang/rust/issues/117846
This commit is contained in:
commit
b7dcabe55e
6 changed files with 94 additions and 1 deletions
|
@ -138,7 +138,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn write_ty(&self, id: hir::HirId, ty: Ty<'tcx>) {
|
pub fn write_ty(&self, id: hir::HirId, ty: Ty<'tcx>) {
|
||||||
debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
|
debug!("write_ty({:?}, {:?}) in fcx {}", id, self.resolve_vars_if_possible(ty), self.tag());
|
||||||
self.typeck_results.borrow_mut().node_types_mut().insert(id, ty);
|
let mut typeck = self.typeck_results.borrow_mut();
|
||||||
|
let mut node_ty = typeck.node_types_mut();
|
||||||
|
if let Some(ty) = node_ty.get(id)
|
||||||
|
&& let Err(e) = ty.error_reported()
|
||||||
|
{
|
||||||
|
// Do not overwrite nodes that were already marked as `{type error}`. This allows us to
|
||||||
|
// silence unnecessary errors from obligations that were set earlier than a type error
|
||||||
|
// was produced, but that is overwritten by later analysis. This happens in particular
|
||||||
|
// for `Sized` obligations introduced in gather_locals. (#117846)
|
||||||
|
self.set_tainted_by_errors(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
node_ty.insert(id, ty);
|
||||||
|
|
||||||
if let Err(e) = ty.error_reported() {
|
if let Err(e) = ty.error_reported() {
|
||||||
self.set_tainted_by_errors(e);
|
self.set_tainted_by_errors(e);
|
||||||
|
|
|
@ -1892,11 +1892,35 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
pat: &'tcx hir::Pat<'tcx>,
|
pat: &'tcx hir::Pat<'tcx>,
|
||||||
ty: Ty<'tcx>,
|
ty: Ty<'tcx>,
|
||||||
) {
|
) {
|
||||||
|
struct V<'tcx> {
|
||||||
|
tcx: TyCtxt<'tcx>,
|
||||||
|
pat_hir_ids: Vec<hir::HirId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'tcx> Visitor<'tcx> for V<'tcx> {
|
||||||
|
type NestedFilter = rustc_middle::hir::nested_filter::All;
|
||||||
|
|
||||||
|
fn nested_visit_map(&mut self) -> Self::Map {
|
||||||
|
self.tcx.hir()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
|
||||||
|
self.pat_hir_ids.push(p.hir_id);
|
||||||
|
hir::intravisit::walk_pat(self, p);
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Err(guar) = ty.error_reported() {
|
if let Err(guar) = ty.error_reported() {
|
||||||
// Override the types everywhere with `err()` to avoid knock on errors.
|
// Override the types everywhere with `err()` to avoid knock on errors.
|
||||||
let err = Ty::new_error(self.tcx, guar);
|
let err = Ty::new_error(self.tcx, guar);
|
||||||
self.write_ty(hir_id, err);
|
self.write_ty(hir_id, err);
|
||||||
self.write_ty(pat.hir_id, err);
|
self.write_ty(pat.hir_id, err);
|
||||||
|
let mut visitor = V { tcx: self.tcx, pat_hir_ids: vec![] };
|
||||||
|
hir::intravisit::walk_pat(&mut visitor, pat);
|
||||||
|
// Mark all the subpatterns as `{type error}` as well. This allows errors for specific
|
||||||
|
// subpatterns to be silenced.
|
||||||
|
for hir_id in visitor.pat_hir_ids {
|
||||||
|
self.write_ty(hir_id, err);
|
||||||
|
}
|
||||||
self.locals.borrow_mut().insert(hir_id, err);
|
self.locals.borrow_mut().insert(hir_id, err);
|
||||||
self.locals.borrow_mut().insert(pat.hir_id, err);
|
self.locals.borrow_mut().insert(pat.hir_id, err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -568,6 +568,11 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
|
||||||
self.data.get_mut(&id.local_id)
|
self.data.get_mut(&id.local_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get(&mut self, id: hir::HirId) -> Option<&V> {
|
||||||
|
validate_hir_id_for_typeck_results(self.hir_owner, id);
|
||||||
|
self.data.get(&id.local_id)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn entry(&mut self, id: hir::HirId) -> Entry<'_, hir::ItemLocalId, V> {
|
pub fn entry(&mut self, id: hir::HirId) -> Entry<'_, hir::ItemLocalId, V> {
|
||||||
validate_hir_id_for_typeck_results(self.hir_owner, id);
|
validate_hir_id_for_typeck_results(self.hir_owner, id);
|
||||||
self.data.entry(id.local_id)
|
self.data.entry(id.local_id)
|
||||||
|
|
|
@ -2953,6 +2953,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ObligationCauseCode::VariableType(hir_id) => {
|
ObligationCauseCode::VariableType(hir_id) => {
|
||||||
|
if let Some(typeck_results) = &self.typeck_results
|
||||||
|
&& let Some(ty) = typeck_results.node_type_opt(hir_id)
|
||||||
|
&& let ty::Error(_) = ty.kind()
|
||||||
|
{
|
||||||
|
err.note(format!(
|
||||||
|
"`{predicate}` isn't satisfied, but the type of this pattern is \
|
||||||
|
`{{type error}}`",
|
||||||
|
));
|
||||||
|
err.downgrade_to_delayed_bug();
|
||||||
|
}
|
||||||
match tcx.parent_hir_node(hir_id) {
|
match tcx.parent_hir_node(hir_id) {
|
||||||
Node::Local(hir::Local { ty: Some(ty), .. }) => {
|
Node::Local(hir::Local { ty: Some(ty), .. }) => {
|
||||||
err.span_suggestion_verbose(
|
err.span_suggestion_verbose(
|
||||||
|
|
22
tests/ui/sized/expr-type-error-plus-sized-obligation.rs
Normal file
22
tests/ui/sized/expr-type-error-plus-sized-obligation.rs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#![allow(warnings)]
|
||||||
|
|
||||||
|
fn issue_117846_repro() {
|
||||||
|
let (a, _) = if true {
|
||||||
|
produce()
|
||||||
|
} else {
|
||||||
|
(Vec::new(), &[]) //~ ERROR E0308
|
||||||
|
};
|
||||||
|
|
||||||
|
accept(&a);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Foo;
|
||||||
|
struct Bar;
|
||||||
|
|
||||||
|
fn produce() -> (Vec<Foo>, &'static [Bar]) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn accept(c: &[Foo]) {}
|
||||||
|
|
||||||
|
fn main() {}
|
19
tests/ui/sized/expr-type-error-plus-sized-obligation.stderr
Normal file
19
tests/ui/sized/expr-type-error-plus-sized-obligation.stderr
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
error[E0308]: `if` and `else` have incompatible types
|
||||||
|
--> $DIR/expr-type-error-plus-sized-obligation.rs:7:9
|
||||||
|
|
|
||||||
|
LL | let (a, _) = if true {
|
||||||
|
| __________________-
|
||||||
|
LL | | produce()
|
||||||
|
| | --------- expected because of this
|
||||||
|
LL | | } else {
|
||||||
|
LL | | (Vec::new(), &[])
|
||||||
|
| | ^^^^^^^^^^^^^^^^^ expected `(Vec<Foo>, &[Bar])`, found `(Vec<_>, &[_; 0])`
|
||||||
|
LL | | };
|
||||||
|
| |_____- `if` and `else` have incompatible types
|
||||||
|
|
|
||||||
|
= note: expected tuple `(Vec<Foo>, &[Bar])`
|
||||||
|
found tuple `(Vec<_>, &[_; 0])`
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
Loading…
Add table
Add a link
Reference in a new issue