From 70089110803f7548e161503964794667c33547cc Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 31 Dec 2020 11:25:53 +0900 Subject: [PATCH] FIx ICE on wf check for foreign fns --- .../src/infer/error_reporting/mod.rs | 8 +++++++ compiler/rustc_typeck/src/check/wfcheck.rs | 2 +- .../wf/wf-in-foreign-fn-decls-issue-80468.rs | 17 +++++++++++++ .../wf-in-foreign-fn-decls-issue-80468.stderr | 24 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs create mode 100644 src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6d6bf4bf5f7..777107ed863 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -153,6 +153,7 @@ fn msg_span_from_early_bound_and_free_regions( Some(Node::Item(it)) => item_scope_tag(&it), Some(Node::TraitItem(it)) => trait_item_scope_tag(&it), Some(Node::ImplItem(it)) => impl_item_scope_tag(&it), + Some(Node::ForeignItem(it)) => foreign_item_scope_tag(&it), _ => unreachable!(), }; let (prefix, span) = match *region { @@ -233,6 +234,13 @@ fn impl_item_scope_tag(item: &hir::ImplItem<'_>) -> &'static str { } } +fn foreign_item_scope_tag(item: &hir::ForeignItem<'_>) -> &'static str { + match item.kind { + hir::ForeignItemKind::Fn(..) => "method body", + hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => "associated item", + } +} + fn explain_span(tcx: TyCtxt<'tcx>, heading: &str, span: Span) -> (String, Option) { let lo = tcx.sess.source_map().lookup_char_pos(span.lo()); (format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1), Some(span)) diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index cd871a4da97..2ae9ded3fa0 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -51,7 +51,7 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> { let fcx = FnCtxt::new(&inh, param_env, id); if !inh.tcx.features().trivial_bounds { // As predicates are cached rather than obligations, this - // needsto be called first so that they are checked with an + // needs to be called first so that they are checked with an // empty `param_env`. check_false_global_bounds(&fcx, span, id); } diff --git a/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs new file mode 100644 index 00000000000..8386959cfb3 --- /dev/null +++ b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.rs @@ -0,0 +1,17 @@ +// Regression test for #80468. + +#![crate_type = "lib"] + +pub trait Trait {} + +#[repr(transparent)] +pub struct Wrapper(T); + +#[repr(transparent)] +pub struct Ref<'a>(&'a u8); + +impl Trait for Ref {} //~ ERROR: implicit elided lifetime not allowed here + +extern "C" { + pub fn repro(_: Wrapper); //~ ERROR: mismatched types +} diff --git a/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr new file mode 100644 index 00000000000..bb839d0a5ec --- /dev/null +++ b/src/test/ui/wf/wf-in-foreign-fn-decls-issue-80468.stderr @@ -0,0 +1,24 @@ +error[E0726]: implicit elided lifetime not allowed here + --> $DIR/wf-in-foreign-fn-decls-issue-80468.rs:13:16 + | +LL | impl Trait for Ref {} + | ^^^- help: indicate the anonymous lifetime: `<'_>` + +error[E0308]: mismatched types + --> $DIR/wf-in-foreign-fn-decls-issue-80468.rs:16:21 + | +LL | pub fn repro(_: Wrapper); + | ^^^^^^^^^^^^ lifetime mismatch + | + = note: expected trait `Trait` + found trait `Trait` +note: the anonymous lifetime #1 defined on the method body at 16:5... + --> $DIR/wf-in-foreign-fn-decls-issue-80468.rs:16:5 + | +LL | pub fn repro(_: Wrapper); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: ...does not necessarily outlive the static lifetime + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0308`.