Skip to content

Commit 0816b94

Browse files
committed
Auto merge of #42642 - venkatagiri:issue_42312, r=nikomatsakis
rustc_typeck: enforce argument type is sized closes #42312 r? @nikomatsakis
2 parents c28cbfb + 5ed21f5 commit 0816b94

File tree

14 files changed

+61
-66
lines changed

14 files changed

+61
-66
lines changed

src/librustc/infer/error_reporting/need_type_info.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use hir::{self, map, Local, Pat, Body};
11+
use hir::{self, Local, Pat, Body};
1212
use hir::intravisit::{self, Visitor, NestedVisitorMap};
1313
use infer::InferCtxt;
1414
use infer::type_variable::TypeVariableOrigin;
@@ -88,7 +88,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
8888
}
8989
}
9090

91-
pub fn need_type_info(&self, body_id: hir::BodyId, span: Span, ty: Ty<'tcx>) {
91+
pub fn need_type_info(&self, body_id: Option<hir::BodyId>, span: Span, ty: Ty<'tcx>) {
9292
let ty = self.resolve_type_vars_if_possible(&ty);
9393
let name = self.extract_type_name(&ty);
9494

@@ -103,11 +103,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
103103
found_arg_pattern: None,
104104
};
105105

106-
// #40294: cause.body_id can also be a fn declaration.
107-
// Currently, if it's anything other than NodeExpr, we just ignore it
108-
match self.tcx.hir.find(body_id.node_id) {
109-
Some(map::NodeExpr(expr)) => local_visitor.visit_expr(expr),
110-
_ => ()
106+
if let Some(body_id) = body_id {
107+
let expr = self.tcx.hir.expect_expr(body_id.node_id);
108+
local_visitor.visit_expr(expr);
111109
}
112110

113111
if let Some(pattern) = local_visitor.found_arg_pattern {

src/librustc/traits/error_reporting.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ use syntax_pos::{DUMMY_SP, Span};
4545

4646
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
4747
pub fn report_fulfillment_errors(&self,
48-
errors: &Vec<FulfillmentError<'tcx>>) {
48+
errors: &Vec<FulfillmentError<'tcx>>,
49+
body_id: Option<hir::BodyId>) {
4950
#[derive(Debug)]
5051
struct ErrorDescriptor<'tcx> {
5152
predicate: ty::Predicate<'tcx>,
@@ -105,7 +106,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
105106

106107
for (error, suppressed) in errors.iter().zip(is_suppressed) {
107108
if !suppressed {
108-
self.report_fulfillment_error(error);
109+
self.report_fulfillment_error(error, body_id);
109110
}
110111
}
111112
}
@@ -148,7 +149,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
148149
false
149150
}
150151

151-
fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) {
152+
fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>,
153+
body_id: Option<hir::BodyId>) {
152154
debug!("report_fulfillment_errors({:?})", error);
153155
match error.code {
154156
FulfillmentErrorCode::CodeSelectionError(ref e) => {
@@ -158,7 +160,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
158160
self.report_projection_error(&error.obligation, e);
159161
}
160162
FulfillmentErrorCode::CodeAmbiguity => {
161-
self.maybe_report_ambiguity(&error.obligation);
163+
self.maybe_report_ambiguity(&error.obligation, body_id);
162164
}
163165
FulfillmentErrorCode::CodeSubtypeError(ref expected_found, ref err) => {
164166
self.report_mismatched_types(&error.obligation.cause,
@@ -869,14 +871,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
869871
}
870872

871873
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
872-
fn maybe_report_ambiguity(&self, obligation: &PredicateObligation<'tcx>) {
874+
fn maybe_report_ambiguity(&self, obligation: &PredicateObligation<'tcx>,
875+
body_id: Option<hir::BodyId>) {
873876
// Unable to successfully determine, probably means
874877
// insufficient type information, but could mean
875878
// ambiguous impls. The latter *ought* to be a
876879
// coherence violation, so we don't report it here.
877880

878881
let predicate = self.resolve_type_vars_if_possible(&obligation.predicate);
879-
let body_id = hir::BodyId { node_id: obligation.cause.body_id };
880882
let span = obligation.cause.span;
881883

882884
debug!("maybe_report_ambiguity(predicate={:?}, obligation={:?})",
@@ -953,7 +955,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
953955
let &SubtypePredicate { a_is_expected: _, a, b } = data.skip_binder();
954956
// both must be type variables, or the other would've been instantiated
955957
assert!(a.is_ty_var() && b.is_ty_var());
956-
self.need_type_info(hir::BodyId { node_id: obligation.cause.body_id },
958+
self.need_type_info(body_id,
957959
obligation.cause.span,
958960
a);
959961
}

src/librustc/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
509509
) {
510510
Ok(predicates) => predicates,
511511
Err(errors) => {
512-
infcx.report_fulfillment_errors(&errors);
512+
infcx.report_fulfillment_errors(&errors, None);
513513
// An unnormalized env is better than nothing.
514514
return elaborated_env;
515515
}

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ impl MirPass for QualifyAndPromoteConstants {
996996
tcx.require_lang_item(lang_items::SyncTraitLangItem),
997997
cause);
998998
if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
999-
infcx.report_fulfillment_errors(&err);
999+
infcx.report_fulfillment_errors(&err, None);
10001000
}
10011001
});
10021002
}

src/librustc_typeck/check/compare_method.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
328328
// Check that all obligations are satisfied by the implementation's
329329
// version.
330330
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
331-
infcx.report_fulfillment_errors(errors);
331+
infcx.report_fulfillment_errors(errors, None);
332332
return Err(ErrorReported);
333333
}
334334

@@ -793,7 +793,7 @@ pub fn compare_const_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
793793
// Check that all obligations are satisfied by the implementation's
794794
// version.
795795
if let Err(ref errors) = inh.fulfillment_cx.borrow_mut().select_all_or_error(&infcx) {
796-
infcx.report_fulfillment_errors(errors);
796+
infcx.report_fulfillment_errors(errors, None);
797797
return;
798798
}
799799

src/librustc_typeck/check/dropck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn ensure_drop_params_and_item_params_correspond<'a, 'tcx>(
110110

111111
if let Err(ref errors) = fulfillment_cx.select_all_or_error(&infcx) {
112112
// this could be reached when we get lazy normalization
113-
infcx.report_fulfillment_errors(errors);
113+
infcx.report_fulfillment_errors(errors, None);
114114
return Err(ErrorReported);
115115
}
116116

src/librustc_typeck/check/mod.rs

+14-36
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ pub struct Inherited<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
217217
/// environment is for an item or something where the "callee" is
218218
/// not clear.
219219
implicit_region_bound: Option<ty::Region<'tcx>>,
220+
221+
body_id: Option<hir::BodyId>,
220222
}
221223

222224
impl<'a, 'gcx, 'tcx> Deref for Inherited<'a, 'gcx, 'tcx> {
@@ -605,6 +607,7 @@ impl<'a, 'gcx, 'tcx> Inherited<'a, 'gcx, 'tcx> {
605607
deferred_cast_checks: RefCell::new(Vec::new()),
606608
anon_types: RefCell::new(NodeMap()),
607609
implicit_region_bound,
610+
body_id,
608611
}
609612
}
610613

@@ -993,16 +996,17 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
993996

994997
// Add formal parameters.
995998
for (arg_ty, arg) in fn_sig.inputs().iter().zip(&body.arguments) {
996-
// The type of the argument must be well-formed.
997-
//
998-
// NB -- this is now checked in wfcheck, but that
999-
// currently only results in warnings, so we issue an
1000-
// old-style WF obligation here so that we still get the
1001-
// errors that we used to get.
1002-
fcx.register_old_wf_obligation(arg_ty, arg.pat.span, traits::MiscObligation);
1003-
1004999
// Check the pattern.
10051000
fcx.check_pat_arg(&arg.pat, arg_ty, true);
1001+
1002+
// Check that argument is Sized.
1003+
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
1004+
// for simple cases like `fn foo(x: Trait)`,
1005+
// where we would error once on the parameter as a whole, and once on the binding `x`.
1006+
if arg.pat.simple_name().is_none() {
1007+
fcx.require_type_is_sized(arg_ty, decl.output.span(), traits::MiscObligation);
1008+
}
1009+
10061010
fcx.write_ty(arg.id, arg_ty);
10071011
}
10081012

@@ -1978,17 +1982,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
19781982
}
19791983
}
19801984

1981-
/// Registers an obligation for checking later, during regionck, that the type `ty` must
1982-
/// outlive the region `r`.
1983-
pub fn register_region_obligation(&self,
1984-
ty: Ty<'tcx>,
1985-
region: ty::Region<'tcx>,
1986-
cause: traits::ObligationCause<'tcx>)
1987-
{
1988-
let mut fulfillment_cx = self.fulfillment_cx.borrow_mut();
1989-
fulfillment_cx.register_region_obligation(ty, region, cause);
1990-
}
1991-
19921985
/// Registers an obligation for checking later, during regionck, that the type `ty` must
19931986
/// outlive the region `r`.
19941987
pub fn register_wf_obligation(&self,
@@ -2003,21 +1996,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
20031996
ty::Predicate::WellFormed(ty)));
20041997
}
20051998

2006-
pub fn register_old_wf_obligation(&self,
2007-
ty: Ty<'tcx>,
2008-
span: Span,
2009-
code: traits::ObligationCauseCode<'tcx>)
2010-
{
2011-
// Registers an "old-style" WF obligation that uses the
2012-
// implicator code. This is basically a buggy version of
2013-
// `register_wf_obligation` that is being kept around
2014-
// temporarily just to help with phasing in the newer rules.
2015-
//
2016-
// FIXME(#27579) all uses of this should be migrated to register_wf_obligation eventually
2017-
let cause = traits::ObligationCause::new(span, self.body_id, code);
2018-
self.register_region_obligation(ty, self.tcx.types.re_empty, cause);
2019-
}
2020-
20211999
/// Registers obligations that all types appearing in `substs` are well-formed.
20222000
pub fn add_wf_bounds(&self, substs: &Substs<'tcx>, expr: &hir::Expr)
20232001
{
@@ -2145,15 +2123,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
21452123

21462124
match fulfillment_cx.select_all_or_error(self) {
21472125
Ok(()) => { }
2148-
Err(errors) => { self.report_fulfillment_errors(&errors); }
2126+
Err(errors) => { self.report_fulfillment_errors(&errors, self.inh.body_id); }
21492127
}
21502128
}
21512129

21522130
/// Select as many obligations as we can at present.
21532131
fn select_obligations_where_possible(&self) {
21542132
match self.fulfillment_cx.borrow_mut().select_where_possible(self) {
21552133
Ok(()) => { }
2156-
Err(errors) => { self.report_fulfillment_errors(&errors); }
2134+
Err(errors) => { self.report_fulfillment_errors(&errors, self.inh.body_id); }
21572135
}
21582136
}
21592137

src/librustc_typeck/check/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'cx, 'gcx, 'tcx> Resolver<'cx, 'gcx, 'tcx> {
377377

378378
fn report_error(&self, t: Ty<'tcx>) {
379379
if !self.tcx.sess.has_errors() {
380-
self.infcx.need_type_info(self.body.id(), self.span.to_span(&self.tcx), t);
380+
self.infcx.need_type_info(Some(self.body.id()), self.span.to_span(&self.tcx), t);
381381
}
382382
}
383383
}

src/librustc_typeck/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ pub fn coerce_unsized_info<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
386386

387387
// Check that all transitive obligations are satisfied.
388388
if let Err(errors) = fulfill_cx.select_all_or_error(&infcx) {
389-
infcx.report_fulfillment_errors(&errors);
389+
infcx.report_fulfillment_errors(&errors, None);
390390
}
391391

392392
// Finally, resolve all regions.

src/librustc_typeck/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn require_same_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
166166
match fulfill_cx.select_all_or_error(infcx) {
167167
Ok(()) => true,
168168
Err(errors) => {
169-
infcx.report_fulfillment_errors(&errors);
169+
infcx.report_fulfillment_errors(&errors, None);
170170
false
171171
}
172172
}

src/test/compile-fail/issue-23046.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn let_<'var, VAR, F: for<'v: 'var> Fn(Expr<'v, VAR>) -> Expr<'v, VAR>>
2424
}
2525

2626
fn main() {
27-
let ex = |x| {
28-
let_(add(x,x), |y| { //~ ERROR type annotations needed
27+
let ex = |x| { //~ ERROR type annotations needed
28+
let_(add(x,x), |y| {
2929
let_(add(x, x), |x|x)})};
3030
}

src/test/compile-fail/issue-38954.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rustc_attrs)]
12-
1311
fn _test(ref _p: str) {}
12+
//~^ ERROR the trait bound `str: std::marker::Sized` is not satisfied [E0277]
1413

15-
#[rustc_error]
16-
fn main() { } //~ ERROR compilation successful
14+
fn main() { }

src/test/compile-fail/issue-42312.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::ops::Deref;
12+
13+
pub trait Foo {
14+
fn baz(_: Self::Target) where Self: Deref {}
15+
//~^ ERROR `<Self as std::ops::Deref>::Target: std::marker::Sized` is not satisfied
16+
}
17+
18+
pub fn f(_: ToString) {}
19+
//~^ ERROR the trait bound `std::string::ToString + 'static: std::marker::Sized` is not satisfied
20+
21+
fn main() { }

src/test/run-pass/associated-types-sugar-path.rs

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ use std::ops::Deref;
1515
pub trait Foo {
1616
type A;
1717
fn boo(&self) -> Self::A;
18-
19-
fn baz(_: Self::Target) where Self: Deref {}
2018
}
2119

2220
impl Foo for isize {

0 commit comments

Comments
 (0)