From 770538ef9b9a93038983ee5d82e07754c279df06 Mon Sep 17 00:00:00 2001 From: Nicolas Barrios Date: Wed, 19 Oct 2022 22:06:50 -0400 Subject: [PATCH 01/13] Add fix suggestions for E0199, E0200, and E0569 --- .../src/coherence/unsafety.rs | 30 +++++++++++++++++++ .../coherence-default-trait-impl.stderr | 12 ++++++++ ...dropck-eyepatch-implies-unsafe-impl.stderr | 12 ++++++++ src/test/ui/error-codes/E0199.stderr | 6 ++++ src/test/ui/error-codes/E0200.stderr | 6 ++++ .../ui/traits/safety-trait-impl-cc.stderr | 6 ++++ src/test/ui/traits/safety-trait-impl.stderr | 12 ++++++++ 7 files changed, 84 insertions(+) diff --git a/compiler/rustc_hir_analysis/src/coherence/unsafety.rs b/compiler/rustc_hir_analysis/src/coherence/unsafety.rs index e45fb5fe41c02..25b2ed76adf2c 100644 --- a/compiler/rustc_hir_analysis/src/coherence/unsafety.rs +++ b/compiler/rustc_hir_analysis/src/coherence/unsafety.rs @@ -26,6 +26,12 @@ pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { "implementing the trait `{}` is not unsafe", trait_ref.print_only_trait_path() ) + .span_suggestion_verbose( + item.span.with_hi(item.span.lo() + rustc_span::BytePos(7)), + "remove `unsafe` from this trait implementation", + "", + rustc_errors::Applicability::MachineApplicable, + ) .emit(); } @@ -37,6 +43,18 @@ pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { "the trait `{}` requires an `unsafe impl` declaration", trait_ref.print_only_trait_path() ) + .note(format!( + "the trait `{}` enforces invariants that the compiler can't check. \ + Review the trait documentation and make sure this implementation \ + upholds those invariants before adding the `unsafe` keyword", + trait_ref.print_only_trait_path() + )) + .span_suggestion_verbose( + item.span.shrink_to_lo(), + "add `unsafe` to this trait implementation", + "unsafe ", + rustc_errors::Applicability::MaybeIncorrect, + ) .emit(); } @@ -48,6 +66,18 @@ pub(super) fn check_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { "requires an `unsafe impl` declaration due to `#[{}]` attribute", attr_name ) + .note(format!( + "the trait `{}` enforces invariants that the compiler can't check. \ + Review the trait documentation and make sure this implementation \ + upholds those invariants before adding the `unsafe` keyword", + trait_ref.print_only_trait_path() + )) + .span_suggestion_verbose( + item.span.shrink_to_lo(), + "add `unsafe` to this trait implementation", + "unsafe ", + rustc_errors::Applicability::MaybeIncorrect, + ) .emit(); } diff --git a/src/test/ui/coherence/coherence-default-trait-impl.stderr b/src/test/ui/coherence/coherence-default-trait-impl.stderr index b08ccb087d91c..6320187827256 100644 --- a/src/test/ui/coherence/coherence-default-trait-impl.stderr +++ b/src/test/ui/coherence/coherence-default-trait-impl.stderr @@ -3,12 +3,24 @@ error[E0199]: implementing the trait `MySafeTrait` is not unsafe | LL | unsafe impl MySafeTrait for Foo {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove `unsafe` from this trait implementation + | +LL - unsafe impl MySafeTrait for Foo {} +LL + impl MySafeTrait for Foo {} + | error[E0200]: the trait `MyUnsafeTrait` requires an `unsafe impl` declaration --> $DIR/coherence-default-trait-impl.rs:13:1 | LL | impl MyUnsafeTrait for Foo {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the trait `MyUnsafeTrait` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword +help: add `unsafe` to this trait implementation + | +LL | unsafe impl MyUnsafeTrait for Foo {} + | ++++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/dropck/dropck-eyepatch-implies-unsafe-impl.stderr b/src/test/ui/dropck/dropck-eyepatch-implies-unsafe-impl.stderr index 49e55be1b49e4..82169ee01bedd 100644 --- a/src/test/ui/dropck/dropck-eyepatch-implies-unsafe-impl.stderr +++ b/src/test/ui/dropck/dropck-eyepatch-implies-unsafe-impl.stderr @@ -8,6 +8,12 @@ LL | | // (unsafe to access self.1 due to #[may_dangle] on A) LL | | fn drop(&mut self) { println!("drop {} {:?}", self.0, self.2); } LL | | } | |_^ + | + = note: the trait `Drop` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword +help: add `unsafe` to this trait implementation + | +LL | unsafe impl<#[may_dangle] A, B: fmt::Debug> Drop for Pt { + | ++++++ error[E0569]: requires an `unsafe impl` declaration due to `#[may_dangle]` attribute --> $DIR/dropck-eyepatch-implies-unsafe-impl.rs:27:1 @@ -19,6 +25,12 @@ LL | | // (unsafe to access self.1 due to #[may_dangle] on 'a) LL | | fn drop(&mut self) { println!("drop {} {:?}", self.0, self.2); } LL | | } | |_^ + | + = note: the trait `Drop` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword +help: add `unsafe` to this trait implementation + | +LL | unsafe impl<#[may_dangle] 'a, 'b, B: fmt::Debug> Drop for Pr<'a, 'b, B> { + | ++++++ error: aborting due to 2 previous errors diff --git a/src/test/ui/error-codes/E0199.stderr b/src/test/ui/error-codes/E0199.stderr index 3632d26cd32e5..99d808c0d4b1e 100644 --- a/src/test/ui/error-codes/E0199.stderr +++ b/src/test/ui/error-codes/E0199.stderr @@ -3,6 +3,12 @@ error[E0199]: implementing the trait `Bar` is not unsafe | LL | unsafe impl Bar for Foo { } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove `unsafe` from this trait implementation + | +LL - unsafe impl Bar for Foo { } +LL + impl Bar for Foo { } + | error: aborting due to previous error diff --git a/src/test/ui/error-codes/E0200.stderr b/src/test/ui/error-codes/E0200.stderr index 677271aad445e..1fd86aecee177 100644 --- a/src/test/ui/error-codes/E0200.stderr +++ b/src/test/ui/error-codes/E0200.stderr @@ -3,6 +3,12 @@ error[E0200]: the trait `Bar` requires an `unsafe impl` declaration | LL | impl Bar for Foo { } | ^^^^^^^^^^^^^^^^^^^^ + | + = note: the trait `Bar` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword +help: add `unsafe` to this trait implementation + | +LL | unsafe impl Bar for Foo { } + | ++++++ error: aborting due to previous error diff --git a/src/test/ui/traits/safety-trait-impl-cc.stderr b/src/test/ui/traits/safety-trait-impl-cc.stderr index 5a0f8d3b8caca..0b1fb30478ff6 100644 --- a/src/test/ui/traits/safety-trait-impl-cc.stderr +++ b/src/test/ui/traits/safety-trait-impl-cc.stderr @@ -7,6 +7,12 @@ LL | | panic!(); LL | | } LL | | } | |_^ + | + = note: the trait `Foo` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword +help: add `unsafe` to this trait implementation + | +LL | unsafe impl lib::Foo for Bar { + | ++++++ error: aborting due to previous error diff --git a/src/test/ui/traits/safety-trait-impl.stderr b/src/test/ui/traits/safety-trait-impl.stderr index fc0f6c6930891..721e2b48b954f 100644 --- a/src/test/ui/traits/safety-trait-impl.stderr +++ b/src/test/ui/traits/safety-trait-impl.stderr @@ -3,12 +3,24 @@ error[E0200]: the trait `UnsafeTrait` requires an `unsafe impl` declaration | LL | impl UnsafeTrait for u16 { } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: the trait `UnsafeTrait` enforces invariants that the compiler can't check. Review the trait documentation and make sure this implementation upholds those invariants before adding the `unsafe` keyword +help: add `unsafe` to this trait implementation + | +LL | unsafe impl UnsafeTrait for u16 { } + | ++++++ error[E0199]: implementing the trait `SafeTrait` is not unsafe --> $DIR/safety-trait-impl.rs:16:1 | LL | unsafe impl SafeTrait for u32 { } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: remove `unsafe` from this trait implementation + | +LL - unsafe impl SafeTrait for u32 { } +LL + impl SafeTrait for u32 { } + | error: aborting due to 2 previous errors From 641bf083969ce1bd519d8270c1985c9a86f3a0af Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 25 Oct 2022 16:27:21 +0200 Subject: [PATCH 02/13] Don't merge inline doc comments for impl blocks --- src/librustdoc/clean/inline.rs | 43 +++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 432d318907fa0..88592494493be 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -55,12 +55,39 @@ pub(crate) fn try_inline( let mut ret = Vec::new(); debug!("attrs={:?}", attrs); - let attrs_clone = attrs; + + let attrs_without_docs = attrs.map(|attrs| { + attrs.into_iter().filter(|a| a.doc_str().is_none()).cloned().collect::>() + }); + // We need this ugly code because: + // + // ``` + // attrs_without_docs.map(|a| a.as_slice()) + // ``` + // + // will fail because it returns a temporary slice and: + // + // ``` + // attrs_without_docs.map(|s| { + // vec = s.as_slice(); + // vec + // }) + // ``` + // + // will fail because we're moving an uninitialized variable into a closure. + let vec; + let attrs_without_docs = match attrs_without_docs { + Some(s) => { + vec = s; + Some(vec.as_slice()) + } + None => None, + }; let kind = match res { Res::Def(DefKind::Trait, did) => { record_extern_fqn(cx, did, ItemType::Trait); - build_impls(cx, Some(parent_module), did, attrs, &mut ret); + build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret); clean::TraitItem(Box::new(build_external_trait(cx, did))) } Res::Def(DefKind::Fn, did) => { @@ -69,27 +96,27 @@ pub(crate) fn try_inline( } Res::Def(DefKind::Struct, did) => { record_extern_fqn(cx, did, ItemType::Struct); - build_impls(cx, Some(parent_module), did, attrs, &mut ret); + build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret); clean::StructItem(build_struct(cx, did)) } Res::Def(DefKind::Union, did) => { record_extern_fqn(cx, did, ItemType::Union); - build_impls(cx, Some(parent_module), did, attrs, &mut ret); + build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret); clean::UnionItem(build_union(cx, did)) } Res::Def(DefKind::TyAlias, did) => { record_extern_fqn(cx, did, ItemType::Typedef); - build_impls(cx, Some(parent_module), did, attrs, &mut ret); + build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret); clean::TypedefItem(build_type_alias(cx, did)) } Res::Def(DefKind::Enum, did) => { record_extern_fqn(cx, did, ItemType::Enum); - build_impls(cx, Some(parent_module), did, attrs, &mut ret); + build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret); clean::EnumItem(build_enum(cx, did)) } Res::Def(DefKind::ForeignTy, did) => { record_extern_fqn(cx, did, ItemType::ForeignType); - build_impls(cx, Some(parent_module), did, attrs, &mut ret); + build_impls(cx, Some(parent_module), did, attrs_without_docs, &mut ret); clean::ForeignTypeItem } // Never inline enum variants but leave them shown as re-exports. @@ -123,7 +150,7 @@ pub(crate) fn try_inline( _ => return None, }; - let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs_clone); + let (attrs, cfg) = merge_attrs(cx, Some(parent_module), load_attrs(cx, did), attrs); cx.inlined.insert(did.into()); let mut item = clean::Item::from_def_id_and_attrs_and_parts( did, From a4279a1d1f9fd8bde6dfc46ac9d53d72fd32a41d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 25 Oct 2022 16:28:27 +0200 Subject: [PATCH 03/13] Add regression test for inlined doc comment on impl block --- src/test/rustdoc/auxiliary/reexport-doc-aux.rs | 5 +++++ src/test/rustdoc/reexport-doc.rs | 8 ++++++++ 2 files changed, 13 insertions(+) create mode 100644 src/test/rustdoc/auxiliary/reexport-doc-aux.rs create mode 100644 src/test/rustdoc/reexport-doc.rs diff --git a/src/test/rustdoc/auxiliary/reexport-doc-aux.rs b/src/test/rustdoc/auxiliary/reexport-doc-aux.rs new file mode 100644 index 0000000000000..3400717eba1e6 --- /dev/null +++ b/src/test/rustdoc/auxiliary/reexport-doc-aux.rs @@ -0,0 +1,5 @@ +pub struct Foo; + +impl Foo { + pub fn foo() {} +} diff --git a/src/test/rustdoc/reexport-doc.rs b/src/test/rustdoc/reexport-doc.rs new file mode 100644 index 0000000000000..df2c889b4d574 --- /dev/null +++ b/src/test/rustdoc/reexport-doc.rs @@ -0,0 +1,8 @@ +// aux-build:reexport-doc-aux.rs + +extern crate reexport_doc_aux as dep; + +// @has 'reexport_doc/struct.Foo.html' +// @count - '//p' 'These are the docs for Foo.' 1 +/// These are the docs for Foo. +pub use dep::Foo; From b205a5a67c3ababf7e7d6f47fe667f4dd5f12e58 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 25 Oct 2022 14:46:14 -0700 Subject: [PATCH 04/13] diagnostics: do not suggest static candidates as traits to import If it's a static candidate, then it's already implemented. Do not suggest it a second time for implementing. --- .../rustc_hir_typeck/src/method/suggest.rs | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index f4351bfa84aed..6c21ed902d007 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -106,7 +106,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let report_candidates = |span: Span, err: &mut Diagnostic, - mut sources: Vec, + sources: &mut Vec, sugg_span: Span| { sources.sort(); sources.dedup(); @@ -248,7 +248,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { match error { MethodError::NoMatch(NoMatchData { - static_candidates: static_sources, + static_candidates: mut static_sources, unsatisfied_predicates, out_of_scope_traits, lev_candidate, @@ -422,9 +422,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.help(&format!("try with `{}::{}`", ty_str, item_name,)); } - report_candidates(span, &mut err, static_sources, sugg_span); + report_candidates(span, &mut err, &mut static_sources, sugg_span); } else if static_sources.len() > 1 { - report_candidates(span, &mut err, static_sources, sugg_span); + report_candidates(span, &mut err, &mut static_sources, sugg_span); } let mut bound_spans = vec![]; @@ -1007,6 +1007,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { source, out_of_scope_traits, &unsatisfied_predicates, + &static_sources, unsatisfied_bounds, ); } @@ -1079,7 +1080,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return Some(err); } - MethodError::Ambiguity(sources) => { + MethodError::Ambiguity(mut sources) => { let mut err = struct_span_err!( self.sess(), item_name.span, @@ -1088,7 +1089,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); err.span_label(item_name.span, format!("multiple `{}` found", item_name)); - report_candidates(span, &mut err, sources, sugg_span); + report_candidates(span, &mut err, &mut sources, sugg_span); err.emit(); } @@ -2015,6 +2016,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Option>, Option>, )], + static_candidates: &[CandidateSource], unsatisfied_bounds: bool, ) { let mut alt_rcvr_sugg = false; @@ -2128,6 +2130,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Some(attr) => attr.level.is_stable(), None => true, }) + .filter(|info| { + // Static candidates are already implemented, and known not to work + // Do not suggest them again + static_candidates.iter().all(|sc| match *sc { + CandidateSource::Trait(def_id) => def_id != info.def_id, + CandidateSource::Impl(def_id) => { + self.tcx.trait_id_of_impl(def_id) != Some(info.def_id) + } + }) + }) .filter(|info| { // We approximate the coherence rules to only suggest // traits that are legal to implement by requiring that From 8cf9ad634b049af8b9f442e5b31bc6a85c3a3392 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 25 Oct 2022 15:28:02 -0700 Subject: [PATCH 05/13] diagnostics: add test case for issue 102354 --- src/test/ui/suggestions/issue-102354.rs | 10 +++++++++ src/test/ui/suggestions/issue-102354.stderr | 24 +++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/test/ui/suggestions/issue-102354.rs create mode 100644 src/test/ui/suggestions/issue-102354.stderr diff --git a/src/test/ui/suggestions/issue-102354.rs b/src/test/ui/suggestions/issue-102354.rs new file mode 100644 index 0000000000000..f881feb0060ab --- /dev/null +++ b/src/test/ui/suggestions/issue-102354.rs @@ -0,0 +1,10 @@ +trait Trait { + fn func() {} +} + +impl Trait for i32 {} + +fn main() { + let x: i32 = 123; + x.func(); //~ERROR no method +} diff --git a/src/test/ui/suggestions/issue-102354.stderr b/src/test/ui/suggestions/issue-102354.stderr new file mode 100644 index 0000000000000..4f76c5f2e75b2 --- /dev/null +++ b/src/test/ui/suggestions/issue-102354.stderr @@ -0,0 +1,24 @@ +error[E0599]: no method named `func` found for type `i32` in the current scope + --> $DIR/issue-102354.rs:9:7 + | +LL | x.func(); + | ^^^^ this is an associated function, not a method + | + = note: found the following associated functions; to be used as methods, functions must have a `self` parameter +note: the candidate is defined in the trait `Trait` + --> $DIR/issue-102354.rs:2:5 + | +LL | fn func() {} + | ^^^^^^^^^ +help: use associated function syntax instead + | +LL | i32::func(); + | ~~~~~~~~~ +help: disambiguate the associated function for the candidate + | +LL | ::func(x); + | ~~~~~~~~~~~~~~~~~~~~~~~ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. From dce44faf5b11cc9dd4491d7ed9211455f81bbdf2 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 27 Oct 2022 16:15:11 +0000 Subject: [PATCH 06/13] Revert "Make ClosureOutlivesRequirement not rely on an unresolved type" This reverts commit a6b5f95fb028f9feb4a2957c06b35035be2c6155. --- .../rustc_borrowck/src/constraints/mod.rs | 2 +- .../src/diagnostics/conflict_errors.rs | 10 ++++---- .../src/diagnostics/explain_borrow.rs | 12 +++++----- .../src/diagnostics/outlives_suggestion.rs | 2 +- .../src/diagnostics/region_errors.rs | 24 +++++++------------ .../rustc_borrowck/src/region_infer/mod.rs | 10 ++++---- .../src/type_check/canonical.rs | 10 ++++---- .../src/type_check/constraint_conversion.rs | 8 +++---- compiler/rustc_borrowck/src/type_check/mod.rs | 19 ++++++++++----- .../src/type_check/relate_tys.rs | 8 +++---- .../src/infer/canonical/query_response.rs | 2 +- compiler/rustc_infer/src/infer/mod.rs | 2 +- .../src/infer/outlives/obligations.rs | 8 +++---- compiler/rustc_middle/src/infer/canonical.rs | 6 +++-- compiler/rustc_middle/src/mir/mod.rs | 2 +- compiler/rustc_middle/src/mir/query.rs | 16 +++++-------- compiler/rustc_middle/src/traits/mod.rs | 2 +- .../ui/async-await/issues/issue-72312.stderr | 13 +++++----- ...er-to-static-comparing-against-free.stderr | 15 ++++++------ .../user-annotations/adt-nullary-enums.stderr | 16 ++++++------- 20 files changed, 93 insertions(+), 94 deletions(-) diff --git a/compiler/rustc_borrowck/src/constraints/mod.rs b/compiler/rustc_borrowck/src/constraints/mod.rs index 58aeb43ef976f..df04128135b89 100644 --- a/compiler/rustc_borrowck/src/constraints/mod.rs +++ b/compiler/rustc_borrowck/src/constraints/mod.rs @@ -92,7 +92,7 @@ pub struct OutlivesConstraint<'tcx> { pub span: Span, /// What caused this constraint? - pub category: ConstraintCategory, + pub category: ConstraintCategory<'tcx>, /// Variance diagnostic information pub variance_info: VarianceDiagInfo<'tcx>, diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index 3f5d9fb62066d..583bc2e281da4 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -983,7 +983,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { err: &mut Diagnostic, location: Location, issued_borrow: &BorrowData<'tcx>, - explanation: BorrowExplanation, + explanation: BorrowExplanation<'tcx>, ) { let used_in_call = matches!( explanation, @@ -1333,7 +1333,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { borrow: &BorrowData<'tcx>, drop_span: Span, borrow_spans: UseSpans<'tcx>, - explanation: BorrowExplanation, + explanation: BorrowExplanation<'tcx>, ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> { debug!( "report_local_value_does_not_live_long_enough(\ @@ -1539,7 +1539,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { drop_span: Span, borrow_spans: UseSpans<'tcx>, proper_span: Span, - explanation: BorrowExplanation, + explanation: BorrowExplanation<'tcx>, ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> { if let BorrowExplanation::MustBeValidFor { category, span, from_closure: false, .. } = explanation @@ -1653,7 +1653,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { borrow: &BorrowData<'tcx>, borrow_span: Span, return_span: Span, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, opt_place_desc: Option<&String>, ) -> Option> { let return_kind = match category { @@ -1748,7 +1748,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { use_span: UseSpans<'tcx>, var_span: Span, fr_name: &RegionName, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, constraint_span: Span, captured_var: &str, ) -> DiagnosticBuilder<'cx, ErrorGuaranteed> { diff --git a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs index 545237bb39205..582d683dd3593 100644 --- a/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs +++ b/compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs @@ -21,7 +21,7 @@ use crate::{ use super::{find_use, RegionName, UseSpans}; #[derive(Debug)] -pub(crate) enum BorrowExplanation { +pub(crate) enum BorrowExplanation<'tcx> { UsedLater(LaterUseKind, Span, Option), UsedLaterInLoop(LaterUseKind, Span, Option), UsedLaterWhenDropped { @@ -30,7 +30,7 @@ pub(crate) enum BorrowExplanation { should_note_order: bool, }, MustBeValidFor { - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, from_closure: bool, span: Span, region_name: RegionName, @@ -49,7 +49,7 @@ pub(crate) enum LaterUseKind { Other, } -impl<'tcx> BorrowExplanation { +impl<'tcx> BorrowExplanation<'tcx> { pub(crate) fn is_explained(&self) -> bool { !matches!(self, BorrowExplanation::Unexplained) } @@ -284,7 +284,7 @@ impl<'tcx> BorrowExplanation { fn add_lifetime_bound_suggestion_to_diagnostic( &self, err: &mut Diagnostic, - category: &ConstraintCategory, + category: &ConstraintCategory<'tcx>, span: Span, region_name: &RegionName, ) { @@ -316,7 +316,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { &self, borrow_region: RegionVid, outlived_region: RegionVid, - ) -> (ConstraintCategory, bool, Span, Option, Vec) { + ) -> (ConstraintCategory<'tcx>, bool, Span, Option, Vec) { let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint( borrow_region, NllRegionVariableOrigin::FreeRegion, @@ -348,7 +348,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { location: Location, borrow: &BorrowData<'tcx>, kind_place: Option<(WriteKind, Place<'tcx>)>, - ) -> BorrowExplanation { + ) -> BorrowExplanation<'tcx> { let regioncx = &self.regioncx; let body: &Body<'_> = &self.body; let tcx = self.infcx.tcx; diff --git a/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs b/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs index 245ea07d120bc..35c3df768995a 100644 --- a/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs +++ b/compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs @@ -161,7 +161,7 @@ impl OutlivesSuggestionBuilder { pub(crate) fn intermediate_suggestion( &mut self, mbcx: &MirBorrowckCtxt<'_, '_>, - errci: &ErrorConstraintInfo, + errci: &ErrorConstraintInfo<'_>, diag: &mut Diagnostic, ) { // Emit an intermediate note. diff --git a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs index b619537f31769..15230718dc0de 100644 --- a/compiler/rustc_borrowck/src/diagnostics/region_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/region_errors.rs @@ -2,7 +2,6 @@ #![deny(rustc::diagnostic_outside_of_impl)] //! Error reporting machinery for lifetime errors. -use either::Either; use rustc_data_structures::fx::FxHashSet; use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan}; use rustc_hir::def_id::DefId; @@ -17,7 +16,7 @@ use rustc_infer::infer::{ NllRegionVariableOrigin, RelateParamBound, }; use rustc_middle::hir::place::PlaceBase; -use rustc_middle::mir::{ConstraintCategory, ReturnConstraint, TerminatorKind}; +use rustc_middle::mir::{ConstraintCategory, ReturnConstraint}; use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::Region; use rustc_middle::ty::TypeVisitor; @@ -40,7 +39,7 @@ use crate::{ MirBorrowckCtxt, }; -impl ConstraintDescription for ConstraintCategory { +impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> { fn description(&self) -> &'static str { // Must end with a space. Allows for empty names to be provided. match self { @@ -116,7 +115,7 @@ pub(crate) enum RegionErrorKind<'tcx> { /// Information about the various region constraints involved in a borrow checker error. #[derive(Clone, Debug)] -pub struct ErrorConstraintInfo { +pub struct ErrorConstraintInfo<'tcx> { // fr: outlived_fr pub(super) fr: RegionVid, pub(super) fr_is_local: bool, @@ -124,7 +123,7 @@ pub struct ErrorConstraintInfo { pub(super) outlived_fr_is_local: bool, // Category and span for best blame constraint - pub(super) category: ConstraintCategory, + pub(super) category: ConstraintCategory<'tcx>, pub(super) span: Span, } @@ -499,7 +498,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { /// ``` fn report_fnmut_error( &self, - errci: &ErrorConstraintInfo, + errci: &ErrorConstraintInfo<'tcx>, kind: ReturnConstraint, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { let ErrorConstraintInfo { outlived_fr, span, .. } = errci; @@ -572,7 +571,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { #[instrument(level = "debug", skip(self))] fn report_escaping_data_error( &self, - errci: &ErrorConstraintInfo, + errci: &ErrorConstraintInfo<'tcx>, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { let ErrorConstraintInfo { span, category, .. } = errci; @@ -676,7 +675,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { /// ``` fn report_general_error( &self, - errci: &ErrorConstraintInfo, + errci: &ErrorConstraintInfo<'tcx>, ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { let ErrorConstraintInfo { fr, @@ -789,7 +788,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { diag: &mut Diagnostic, f: Region<'tcx>, o: Region<'tcx>, - category: &ConstraintCategory, + category: &ConstraintCategory<'tcx>, ) { if !o.is_static() { return; @@ -797,12 +796,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> { let tcx = self.infcx.tcx; - let instance = - if let ConstraintCategory::CallArgument(location) = category - && let Either::Right(term) = self.body.stmt_at(*location) - && let TerminatorKind::Call { func, .. } = &term.kind - { - let func_ty = func.ty(self.body, tcx); + let instance = if let ConstraintCategory::CallArgument(Some(func_ty)) = category { let (fn_did, substs) = match func_ty.kind() { ty::FnDef(fn_did, substs) => (fn_did, substs), _ => return, diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 85b9bde2c8192..8b63294fbab0e 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -91,7 +91,7 @@ pub struct RegionInferenceContext<'tcx> { /// Map closure bounds to a `Span` that should be used for error reporting. closure_bounds_mapping: - FxHashMap>, + FxHashMap, Span)>>, /// Map universe indexes to information on why we created it. universe_causes: FxHashMap>, @@ -267,7 +267,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { member_constraints_in: MemberConstraintSet<'tcx, RegionVid>, closure_bounds_mapping: FxHashMap< Location, - FxHashMap<(RegionVid, RegionVid), (ConstraintCategory, Span)>, + FxHashMap<(RegionVid, RegionVid), (ConstraintCategory<'tcx>, Span)>, >, universe_causes: FxHashMap>, type_tests: Vec>, @@ -1807,7 +1807,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { pub(crate) fn retrieve_closure_constraint_info( &self, constraint: OutlivesConstraint<'tcx>, - ) -> Option<(ConstraintCategory, Span)> { + ) -> Option<(ConstraintCategory<'tcx>, Span)> { match constraint.locations { Locations::All(_) => None, Locations::Single(loc) => { @@ -1822,7 +1822,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { fr1: RegionVid, fr1_origin: NllRegionVariableOrigin, fr2: RegionVid, - ) -> (ConstraintCategory, ObligationCause<'tcx>) { + ) -> (ConstraintCategory<'tcx>, ObligationCause<'tcx>) { let BlameConstraint { category, cause, .. } = self .best_blame_constraint(fr1, fr1_origin, |r| self.provides_universal_region(r, fr1, fr2)) .0; @@ -2362,7 +2362,7 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx #[derive(Clone, Debug)] pub struct BlameConstraint<'tcx> { - pub category: ConstraintCategory, + pub category: ConstraintCategory<'tcx>, pub from_closure: bool, pub cause: ObligationCause<'tcx>, pub variance_info: ty::VarianceDiagInfo<'tcx>, diff --git a/compiler/rustc_borrowck/src/type_check/canonical.rs b/compiler/rustc_borrowck/src/type_check/canonical.rs index 459ecfe17e3e7..a581726a15c9c 100644 --- a/compiler/rustc_borrowck/src/type_check/canonical.rs +++ b/compiler/rustc_borrowck/src/type_check/canonical.rs @@ -28,7 +28,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { pub(super) fn fully_perform_op( &mut self, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, op: Op, ) -> Fallible where @@ -85,7 +85,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &mut self, trait_ref: ty::TraitRef<'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) { self.prove_predicate( ty::Binder::dummy(ty::PredicateKind::Trait(ty::TraitPredicate { @@ -124,7 +124,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &mut self, predicates: impl IntoIterator>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) { for predicate in predicates { let predicate = predicate.to_predicate(self.tcx()); @@ -139,7 +139,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &mut self, predicate: ty::Predicate<'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) { let param_env = self.param_env; self.fully_perform_op( @@ -164,7 +164,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { &mut self, value: T, location: impl NormalizeLocation, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) -> T where T: type_op::normalize::Normalizable<'tcx> + fmt::Display + Copy + 'tcx, diff --git a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs index d7e5a118a2e0b..d5bfc2f520826 100644 --- a/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs +++ b/compiler/rustc_borrowck/src/type_check/constraint_conversion.rs @@ -37,7 +37,7 @@ pub(crate) struct ConstraintConversion<'a, 'tcx> { param_env: ty::ParamEnv<'tcx>, locations: Locations, span: Span, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, constraints: &'a mut MirTypeckRegionConstraints<'tcx>, } @@ -50,7 +50,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> { param_env: ty::ParamEnv<'tcx>, locations: Locations, span: Span, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, constraints: &'a mut MirTypeckRegionConstraints<'tcx>, ) -> Self { Self { @@ -175,7 +175,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> { &mut self, sup: ty::RegionVid, sub: ty::RegionVid, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) { let category = match self.category { ConstraintCategory::Boring | ConstraintCategory::BoringNoLocation => category, @@ -203,7 +203,7 @@ impl<'a, 'b, 'tcx> TypeOutlivesDelegate<'tcx> for &'a mut ConstraintConversion<' _origin: SubregionOrigin<'tcx>, a: ty::Region<'tcx>, b: ty::Region<'tcx>, - constraint_category: ConstraintCategory, + constraint_category: ConstraintCategory<'tcx>, ) { let b = self.to_region_vid(b); let a = self.to_region_vid(a); diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 00cacd515a1e2..dc0f0e7cd3c4c 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -942,7 +942,7 @@ pub(crate) struct MirTypeckRegionConstraints<'tcx> { pub(crate) member_constraints: MemberConstraintSet<'tcx, RegionVid>, pub(crate) closure_bounds_mapping: - FxHashMap>, + FxHashMap, Span)>>, pub(crate) universe_causes: FxHashMap>, @@ -1133,7 +1133,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { fn push_region_constraints( &mut self, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, data: &QueryRegionConstraints<'tcx>, ) { debug!("constraints generated: {:#?}", data); @@ -1158,7 +1158,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { sub: Ty<'tcx>, sup: Ty<'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) -> Fallible<()> { // Use this order of parameters because the sup type is usually the // "expected" type in diagnostics. @@ -1171,7 +1171,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { expected: Ty<'tcx>, found: Ty<'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) -> Fallible<()> { self.relate_types(expected, ty::Variance::Invariant, found, locations, category) } @@ -1183,7 +1183,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { v: ty::Variance, user_ty: &UserTypeProjection, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) -> Fallible<()> { let annotated_type = self.user_type_annotations[user_ty.base].inferred_ty; let mut curr_projected_ty = PlaceTy::from_ty(annotated_type); @@ -1618,12 +1618,19 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { span_mirbug!(self, term, "call to {:?} with wrong # of args", sig); } + let func_ty = if let TerminatorKind::Call { func, .. } = &term.kind { + Some(func.ty(body, self.infcx.tcx)) + } else { + None + }; + debug!(?func_ty); + for (n, (fn_arg, op_arg)) in iter::zip(sig.inputs(), args).enumerate() { let op_arg_ty = op_arg.ty(body, self.tcx()); let op_arg_ty = self.normalize(op_arg_ty, term_location); let category = if from_hir_call { - ConstraintCategory::CallArgument(term_location) + ConstraintCategory::CallArgument(func_ty) } else { ConstraintCategory::Boring }; diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index b53360ea61bbf..c97a6a1a6587a 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -28,7 +28,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { v: ty::Variance, b: Ty<'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) -> Fallible<()> { TypeRelating::new( self.infcx, @@ -45,7 +45,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { a: ty::SubstsRef<'tcx>, b: ty::SubstsRef<'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) -> Fallible<()> { TypeRelating::new( self.infcx, @@ -64,7 +64,7 @@ struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> { locations: Locations, /// What category do we assign the resulting `'a: 'b` relationships? - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, /// Information so that error reporting knows what types we are relating /// when reporting a bound region error. @@ -75,7 +75,7 @@ impl<'me, 'bccx, 'tcx> NllTypeRelatingDelegate<'me, 'bccx, 'tcx> { fn new( type_checker: &'me mut TypeChecker<'bccx, 'tcx>, locations: Locations, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, universe_info: UniverseInfo<'tcx>, ) -> Self { Self { type_checker, locations, category, universe_info } diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 608b5cc8756a6..eb0135d76f1e4 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -632,7 +632,7 @@ impl<'tcx> InferCtxt<'tcx> { /// creates query region constraints. pub fn make_query_region_constraints<'tcx>( tcx: TyCtxt<'tcx>, - outlives_obligations: impl Iterator, ty::Region<'tcx>, ConstraintCategory)>, + outlives_obligations: impl Iterator, ty::Region<'tcx>, ConstraintCategory<'tcx>)>, region_constraints: &RegionConstraintData<'tcx>, ) -> QueryRegionConstraints<'tcx> { let RegionConstraintData { constraints, verifys, givens, member_constraints } = diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index 7e2ea6c0e269c..ffb020398b858 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -425,7 +425,7 @@ pub enum SubregionOrigin<'tcx> { static_assert_size!(SubregionOrigin<'_>, 32); impl<'tcx> SubregionOrigin<'tcx> { - pub fn to_constraint_category(&self) -> ConstraintCategory { + pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> { match self { Self::Subtype(type_trace) => type_trace.cause.to_constraint_category(), Self::AscribeUserTypeProvePredicate(span) => ConstraintCategory::Predicate(*span), diff --git a/compiler/rustc_infer/src/infer/outlives/obligations.rs b/compiler/rustc_infer/src/infer/outlives/obligations.rs index 5ebf80b7b7481..6ca884799aa6f 100644 --- a/compiler/rustc_infer/src/infer/outlives/obligations.rs +++ b/compiler/rustc_infer/src/infer/outlives/obligations.rs @@ -210,7 +210,7 @@ pub trait TypeOutlivesDelegate<'tcx> { origin: SubregionOrigin<'tcx>, a: ty::Region<'tcx>, b: ty::Region<'tcx>, - constraint_category: ConstraintCategory, + constraint_category: ConstraintCategory<'tcx>, ); fn push_verify( @@ -259,7 +259,7 @@ where origin: infer::SubregionOrigin<'tcx>, ty: Ty<'tcx>, region: ty::Region<'tcx>, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) { assert!(!ty.has_escaping_bound_vars()); @@ -273,7 +273,7 @@ where origin: infer::SubregionOrigin<'tcx>, components: &[Component<'tcx>], region: ty::Region<'tcx>, - category: ConstraintCategory, + category: ConstraintCategory<'tcx>, ) { for component in components.iter() { let origin = origin.clone(); @@ -529,7 +529,7 @@ impl<'cx, 'tcx> TypeOutlivesDelegate<'tcx> for &'cx InferCtxt<'tcx> { origin: SubregionOrigin<'tcx>, a: ty::Region<'tcx>, b: ty::Region<'tcx>, - _constraint_category: ConstraintCategory, + _constraint_category: ConstraintCategory<'tcx>, ) { self.sub_regions(origin, a, b) } diff --git a/compiler/rustc_middle/src/infer/canonical.rs b/compiler/rustc_middle/src/infer/canonical.rs index f4f1d82c3b8d3..d3cf519b633c7 100644 --- a/compiler/rustc_middle/src/infer/canonical.rs +++ b/compiler/rustc_middle/src/infer/canonical.rs @@ -302,8 +302,10 @@ impl<'tcx, V> Canonical<'tcx, V> { } } -pub type QueryOutlivesConstraint<'tcx> = - (ty::Binder<'tcx, ty::OutlivesPredicate, Region<'tcx>>>, ConstraintCategory); +pub type QueryOutlivesConstraint<'tcx> = ( + ty::Binder<'tcx, ty::OutlivesPredicate, Region<'tcx>>>, + ConstraintCategory<'tcx>, +); TrivialTypeTraversalAndLiftImpls! { for <'tcx> { diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 3b80af184be47..79db35a764a80 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2904,7 +2904,7 @@ fn pretty_print_const_value<'tcx>( /// `Location` represents the position of the start of the statement; or, if /// `statement_index` equals the number of statements, then the start of the /// terminator. -#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable, TyEncodable, TyDecodable)] +#[derive(Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd, HashStable)] pub struct Location { /// The block that the location is within. pub block: BasicBlock, diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 1d847d8f3d31d..efd7357afc46c 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -15,7 +15,7 @@ use smallvec::SmallVec; use std::cell::Cell; use std::fmt::{self, Debug}; -use super::{Field, Location, SourceInfo}; +use super::{Field, SourceInfo}; #[derive(Copy, Clone, PartialEq, TyEncodable, TyDecodable, HashStable, Debug)] pub enum UnsafetyViolationKind { @@ -314,12 +314,12 @@ pub struct ClosureOutlivesRequirement<'tcx> { pub blame_span: Span, // ... due to this reason. - pub category: ConstraintCategory, + pub category: ConstraintCategory<'tcx>, } // Make sure this enum doesn't unintentionally grow #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] -rustc_data_structures::static_assert_size!(ConstraintCategory, 16); +rustc_data_structures::static_assert_size!(ConstraintCategory<'_>, 16); /// Outlives-constraints can be categorized to determine whether and why they /// are interesting (for error reporting). Order of variants indicates sort @@ -327,8 +327,8 @@ rustc_data_structures::static_assert_size!(ConstraintCategory, 16); /// /// See also `rustc_const_eval::borrow_check::constraints`. #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] -#[derive(TyEncodable, TyDecodable, HashStable)] -pub enum ConstraintCategory { +#[derive(TyEncodable, TyDecodable, HashStable, Lift, TypeVisitable, TypeFoldable)] +pub enum ConstraintCategory<'tcx> { Return(ReturnConstraint), Yield, UseAsConst, @@ -342,7 +342,7 @@ pub enum ConstraintCategory { ClosureBounds, /// Contains the function type if available. - CallArgument(Location), + CallArgument(Option>), CopyBound, SizedBound, Assignment, @@ -368,10 +368,6 @@ pub enum ConstraintCategory { Internal, } -TrivialTypeTraversalAndLiftImpls! { - ConstraintCategory, -} - #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] #[derive(TyEncodable, TyDecodable, HashStable, TypeVisitable, TypeFoldable)] pub enum ReturnConstraint { diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 3adc2e1b73e48..e73d44bbb36c3 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -185,7 +185,7 @@ impl<'tcx> ObligationCause<'tcx> { self } - pub fn to_constraint_category(&self) -> ConstraintCategory { + pub fn to_constraint_category(&self) -> ConstraintCategory<'tcx> { match self.code() { MatchImpl(cause, _) => cause.to_constraint_category(), AscribeUserTypeProvePredicate(predicate_span) => { diff --git a/src/test/ui/async-await/issues/issue-72312.stderr b/src/test/ui/async-await/issues/issue-72312.stderr index 5e5a980adb9f2..aa947b6900366 100644 --- a/src/test/ui/async-await/issues/issue-72312.stderr +++ b/src/test/ui/async-await/issues/issue-72312.stderr @@ -1,5 +1,5 @@ error[E0521]: borrowed data escapes outside of associated function - --> $DIR/issue-72312.rs:12:24 + --> $DIR/issue-72312.rs:12:9 | LL | pub async fn start(&self) { | ----- @@ -7,17 +7,16 @@ LL | pub async fn start(&self) { | `self` is a reference that is only valid in the associated function body | let's call the lifetime of this reference `'1` ... -LL | require_static(async move { - | ________________________^ +LL | / require_static(async move { LL | | LL | | LL | | LL | | &self; LL | | }); - | | ^ - | | | - | |_________`self` escapes the associated function body here - | argument requires that `'1` must outlive `'static` + | | ^ + | | | + | |__________`self` escapes the associated function body here + | argument requires that `'1` must outlive `'static` error: aborting due to previous error diff --git a/src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr b/src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr index da4bc499c7e51..01293379700d2 100644 --- a/src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr +++ b/src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr @@ -53,14 +53,15 @@ LL | fn case2() { error[E0597]: `a` does not live long enough --> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:30:26 | -LL | let cell = Cell::new(&a); - | ----------^^- - | | | - | | borrowed value does not live long enough - | argument requires that `a` is borrowed for `'static` +LL | let cell = Cell::new(&a); + | ^^ borrowed value does not live long enough ... -LL | } - | - `a` dropped here while still borrowed +LL | / foo(cell, |cell_a, cell_x| { +LL | | cell_x.set(cell_a.get()); // forces 'a: 'x, implies 'a = 'static -> borrow error +LL | | }) + | |______- argument requires that `a` is borrowed for `'static` +LL | } + | - `a` dropped here while still borrowed error: aborting due to 2 previous errors diff --git a/src/test/ui/nll/user-annotations/adt-nullary-enums.stderr b/src/test/ui/nll/user-annotations/adt-nullary-enums.stderr index bb70341222880..3326fa521fc9c 100644 --- a/src/test/ui/nll/user-annotations/adt-nullary-enums.stderr +++ b/src/test/ui/nll/user-annotations/adt-nullary-enums.stderr @@ -1,14 +1,14 @@ error[E0597]: `c` does not live long enough --> $DIR/adt-nullary-enums.rs:33:41 | -LL | SomeEnum::SomeVariant(Cell::new(&c)), - | ----------^^- - | | | - | | borrowed value does not live long enough - | argument requires that `c` is borrowed for `'static` -... -LL | } - | - `c` dropped here while still borrowed +LL | / combine( +LL | | SomeEnum::SomeVariant(Cell::new(&c)), + | | ^^ borrowed value does not live long enough +LL | | SomeEnum::SomeOtherVariant::>, +LL | | ); + | |_____- argument requires that `c` is borrowed for `'static` +LL | } + | - `c` dropped here while still borrowed error[E0597]: `c` does not live long enough --> $DIR/adt-nullary-enums.rs:41:41 From 4e0c27bbfd263580c971aee1ef6960914e20a7ff Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 27 Oct 2022 16:22:06 +0000 Subject: [PATCH 07/13] Erase regions from CallArgument, add test + bless --- compiler/rustc_borrowck/src/type_check/mod.rs | 2 +- .../project-fn-ret-invariant.oneuse.stderr | 10 +++--- .../cache/project-fn-ret-invariant.rs | 2 +- src/test/ui/borrowck/issue-103624.rs | 31 ++++++++++++++++ src/test/ui/borrowck/issue-103624.stderr | 35 +++++++++++++++++++ 5 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/borrowck/issue-103624.rs create mode 100644 src/test/ui/borrowck/issue-103624.stderr diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index dc0f0e7cd3c4c..42c92e56e1623 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1630,7 +1630,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let op_arg_ty = self.normalize(op_arg_ty, term_location); let category = if from_hir_call { - ConstraintCategory::CallArgument(func_ty) + ConstraintCategory::CallArgument(self.infcx.tcx.erase_regions(func_ty)) } else { ConstraintCategory::Boring }; diff --git a/src/test/ui/associated-types/cache/project-fn-ret-invariant.oneuse.stderr b/src/test/ui/associated-types/cache/project-fn-ret-invariant.oneuse.stderr index cc15601621278..77841780f6216 100644 --- a/src/test/ui/associated-types/cache/project-fn-ret-invariant.oneuse.stderr +++ b/src/test/ui/associated-types/cache/project-fn-ret-invariant.oneuse.stderr @@ -15,19 +15,19 @@ LL | let a = bar(f, x); = help: see for more information about variance error: lifetime may not live long enough - --> $DIR/project-fn-ret-invariant.rs:40:13 + --> $DIR/project-fn-ret-invariant.rs:42:13 | LL | fn baz<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) { | -- -- lifetime `'b` defined here | | | lifetime `'a` defined here -LL | let f = foo; // <-- No consistent type can be inferred for `f` here. -LL | let a = bar(f, x); +... +LL | let b = bar(f, y); | ^^^^^^^^^ argument requires that `'b` must outlive `'a` | = help: consider adding the following bound: `'b: 'a` - = note: requirement occurs because of a function pointer to `foo` - = note: the function `foo` is invariant over the parameter `'a` + = note: requirement occurs because of the type `Type<'_>`, which makes the generic argument `'_` invariant + = note: the struct `Type<'a>` is invariant over the parameter `'a` = help: see for more information about variance help: `'a` and `'b` must be the same: replace one with the other diff --git a/src/test/ui/associated-types/cache/project-fn-ret-invariant.rs b/src/test/ui/associated-types/cache/project-fn-ret-invariant.rs index 1075fd6e0923a..e043379133ab0 100644 --- a/src/test/ui/associated-types/cache/project-fn-ret-invariant.rs +++ b/src/test/ui/associated-types/cache/project-fn-ret-invariant.rs @@ -39,8 +39,8 @@ fn baz<'a, 'b>(x: Type<'a>, y: Type<'b>) -> (Type<'a>, Type<'b>) { let f = foo; // <-- No consistent type can be inferred for `f` here. let a = bar(f, x); //[oneuse]~^ ERROR lifetime may not live long enough - //[oneuse]~| ERROR lifetime may not live long enough let b = bar(f, y); + //[oneuse]~^ ERROR lifetime may not live long enough (a, b) } diff --git a/src/test/ui/borrowck/issue-103624.rs b/src/test/ui/borrowck/issue-103624.rs new file mode 100644 index 0000000000000..f1fa95f92469b --- /dev/null +++ b/src/test/ui/borrowck/issue-103624.rs @@ -0,0 +1,31 @@ +// edition:2021 + +struct StructA { + b: StructB, +} + +async fn spawn_blocking(f: impl (Fn() -> T) + Send + Sync + 'static) -> T { + todo!() +} + +impl StructA { + async fn foo(&self) { + let bar = self.b.bar().await; + spawn_blocking(move || { + //~^ ERROR borrowed data escapes outside of associated function + self.b; + //~^ ERROR cannot move out of `self.b`, as `self` is a captured variable in an `Fn` closure + }) + .await; + } +} + +struct StructB {} + +impl StructB { + async fn bar(&self) -> Option { + None + } +} + +fn main() {} diff --git a/src/test/ui/borrowck/issue-103624.stderr b/src/test/ui/borrowck/issue-103624.stderr new file mode 100644 index 0000000000000..e6a35dd8801af --- /dev/null +++ b/src/test/ui/borrowck/issue-103624.stderr @@ -0,0 +1,35 @@ +error[E0507]: cannot move out of `self.b`, as `self` is a captured variable in an `Fn` closure + --> $DIR/issue-103624.rs:16:13 + | +LL | async fn foo(&self) { + | ----- captured outer variable +LL | let bar = self.b.bar().await; +LL | spawn_blocking(move || { + | ------- captured by this `Fn` closure +LL | +LL | self.b; + | ^^^^^^ move occurs because `self.b` has type `StructB`, which does not implement the `Copy` trait + +error[E0521]: borrowed data escapes outside of associated function + --> $DIR/issue-103624.rs:14:9 + | +LL | async fn foo(&self) { + | ----- + | | + | `self` is a reference that is only valid in the associated function body + | let's call the lifetime of this reference `'1` +LL | let bar = self.b.bar().await; +LL | / spawn_blocking(move || { +LL | | +LL | | self.b; +LL | | +LL | | }) + | | ^ + | | | + | |__________`self` escapes the associated function body here + | argument requires that `'1` must outlive `'static` + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0507, E0521. +For more information about an error, try `rustc --explain E0507`. From 97423d331f5dbed3bbbf474e3b6bd2d8b7bd9af2 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Tue, 4 Oct 2022 01:27:11 +0000 Subject: [PATCH 08/13] Add tests for static async functions in traits This patch adds test cases for AFIT, the majority of which are currently expected to run as `check-fail`. --- .../in-trait/async-associated-types.rs | 24 +++++++ .../in-trait/async-associated-types.stderr | 57 +++++++++++++++++ .../async-associated-types2-desugared.rs | 31 +++++++++ .../async-associated-types2-desugared.stderr | 42 +++++++++++++ .../in-trait/async-associated-types2.rs | 32 ++++++++++ .../in-trait/async-associated-types2.stderr | 63 +++++++++++++++++++ .../ui/async-await/in-trait/async-example.rs | 17 +++++ .../in-trait/async-generics-and-bounds.rs | 21 +++++++ .../in-trait/async-generics-and-bounds.stderr | 37 +++++++++++ .../ui/async-await/in-trait/async-generics.rs | 18 ++++++ .../in-trait/async-generics.stderr | 37 +++++++++++ .../in-trait/async-lifetimes-and-bounds.rs | 20 ++++++ .../async-lifetimes-and-bounds.stderr | 23 +++++++ .../async-await/in-trait/async-lifetimes.rs | 18 ++++++ .../in-trait/async-lifetimes.stderr | 23 +++++++ .../async-await/in-trait/fn-not-async-err.rs | 17 +++++ .../in-trait/fn-not-async-err.stderr | 17 +++++ .../async-await/in-trait/fn-not-async-err2.rs | 21 +++++++ .../in-trait/fn-not-async-err2.stderr | 12 ++++ 19 files changed, 530 insertions(+) create mode 100644 src/test/ui/async-await/in-trait/async-associated-types.rs create mode 100644 src/test/ui/async-await/in-trait/async-associated-types.stderr create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2.rs create mode 100644 src/test/ui/async-await/in-trait/async-associated-types2.stderr create mode 100644 src/test/ui/async-await/in-trait/async-example.rs create mode 100644 src/test/ui/async-await/in-trait/async-generics-and-bounds.rs create mode 100644 src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr create mode 100644 src/test/ui/async-await/in-trait/async-generics.rs create mode 100644 src/test/ui/async-await/in-trait/async-generics.stderr create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes.rs create mode 100644 src/test/ui/async-await/in-trait/async-lifetimes.stderr create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err.rs create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err.stderr create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2.rs create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2.stderr diff --git a/src/test/ui/async-await/in-trait/async-associated-types.rs b/src/test/ui/async-await/in-trait/async-associated-types.rs new file mode 100644 index 0000000000000..0269f00bbab73 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types.rs @@ -0,0 +1,24 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::fmt::Debug; + +trait MyTrait<'a, 'b, T> where Self: 'a, T: Debug + Sized + 'b { + type MyAssoc;// = (&'a T, &'b U); + + async fn foo(&'a self, key: &'b T) -> Self::MyAssoc; +} + +impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { + type MyAssoc = (&'a U, &'b T); + + async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + (self, key) + } +} +//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements +//~| ERROR cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types.stderr b/src/test/ui/async-await/in-trait/async-associated-types.stderr new file mode 100644 index 0000000000000..c1c66f9023bd7 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types.stderr @@ -0,0 +1,57 @@ +error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements + --> $DIR/async-associated-types.rs:17:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + | ^^^^^^^^^^^^^^ + | +note: first, the lifetime cannot outlive the lifetime `'a` as defined here... + --> $DIR/async-associated-types.rs:14:6 + | +LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { + | ^^ +note: ...so that the types are compatible + --> $DIR/async-associated-types.rs:17:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + | ^^^^^^^^^^^^^^ + = note: expected `(&'a U, &'b T)` + found `(&U, &T)` + = note: but, the lifetime must be valid for the static lifetime... +note: ...so that the types are compatible + --> $DIR/async-associated-types.rs:17:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + | ^^^^^^^^^^^^^^ + = note: expected `MyTrait<'static, 'static, T>` + found `MyTrait<'_, '_, T>` + +error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements + --> $DIR/async-associated-types.rs:17:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + | ^^^^^^^^^^^^^^ + | +note: first, the lifetime cannot outlive the lifetime `'b` as defined here... + --> $DIR/async-associated-types.rs:14:10 + | +LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { + | ^^ +note: ...so that the types are compatible + --> $DIR/async-associated-types.rs:17:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + | ^^^^^^^^^^^^^^ + = note: expected `(&'a U, &'b T)` + found `(&U, &T)` + = note: but, the lifetime must be valid for the static lifetime... +note: ...so that the types are compatible + --> $DIR/async-associated-types.rs:17:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + | ^^^^^^^^^^^^^^ + = note: expected `MyTrait<'static, 'static, T>` + found `MyTrait<'_, '_, T>` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0495`. diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs new file mode 100644 index 0000000000000..85d29eb703e76 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs @@ -0,0 +1,31 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + type Fut<'a>: Future + where + Self: 'a; + + async fn foo(&self) -> Self::Fut<'a>; + //~^ ERROR use of undeclared lifetime name `'a` + //~| ERROR the parameter type `Self` may not live long enough +} + +impl MyTrait for i32 { + type Fut<'a> = impl Future + 'a + where + Self: 'a; + //~^^^ ERROR `impl Trait` in type aliases is unstable + + fn foo<'a>(&'a self) -> Self::Fut<'a> { + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr new file mode 100644 index 0000000000000..6bc553b26350a --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr @@ -0,0 +1,42 @@ +error[E0261]: use of undeclared lifetime name `'a` + --> $DIR/async-associated-types2-desugared.rs:13:38 + | +LL | async fn foo(&self) -> Self::Fut<'a>; + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | async fn foo<'a>(&self) -> Self::Fut<'a>; + | ++++ +help: consider introducing lifetime `'a` here + | +LL | trait MyTrait<'a> { + | ++++ + +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/async-associated-types2-desugared.rs:19:20 + | +LL | type Fut<'a> = impl Future + 'a + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error[E0310]: the parameter type `Self` may not live long enough + --> $DIR/async-associated-types2-desugared.rs:13:28 + | +LL | async fn foo(&self) -> Self::Fut<'a>; + | ^^^^^^^^^^^^^ + | + = help: consider adding an explicit lifetime bound `Self: 'static`... + = note: ...so that the type `Self` will meet its required lifetime bounds... +note: ...that is required by this bound + --> $DIR/async-associated-types2-desugared.rs:11:15 + | +LL | Self: 'a; + | ^^ + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0261, E0310, E0658. +For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.rs b/src/test/ui/async-await/in-trait/async-associated-types2.rs new file mode 100644 index 0000000000000..c2133b04bc847 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2.rs @@ -0,0 +1,32 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + type Fut<'a>: Future + where + Self: 'a; + + fn foo(&self) -> Self::Fut<'a>; + //~^ ERROR use of undeclared lifetime name `'a` +} + +impl MyTrait for i32 { + type Fut<'a> = impl Future + 'a + where + Self: 'a; + //~^^^ ERROR `impl Trait` in type aliases is unstable + //~| ERROR expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` + + fn foo<'a>(&'a self) -> Self::Fut<'a> { + //~^ ERROR `impl` item signature doesn't match `trait` item signature + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.stderr b/src/test/ui/async-await/in-trait/async-associated-types2.stderr new file mode 100644 index 0000000000000..f3343852fa093 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-associated-types2.stderr @@ -0,0 +1,63 @@ +error[E0261]: use of undeclared lifetime name `'a` + --> $DIR/async-associated-types2.rs:13:32 + | +LL | fn foo(&self) -> Self::Fut<'a>; + | ^^ undeclared lifetime + | +help: consider introducing lifetime `'a` here + | +LL | fn foo<'a>(&self) -> Self::Fut<'a>; + | ++++ +help: consider introducing lifetime `'a` here + | +LL | trait MyTrait<'a> { + | ++++ + +error[E0658]: `impl Trait` in type aliases is unstable + --> $DIR/async-associated-types2.rs:18:20 + | +LL | type Fut<'a> = impl Future + 'a + | ^^^^^^^^^^^^^^^^ + | + = note: see issue #63063 for more information + = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable + +error[E0271]: expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` + --> $DIR/async-associated-types2.rs:18:20 + | +LL | type Fut<'a> = impl Future + 'a + | ^^^^^^^^^^^^^^^^ expected `i32`, found associated type + | + = note: expected type `i32` + found associated type `<::Fut<'a> as Future>::Output` +note: required by a bound in `MyTrait::Fut` + --> $DIR/async-associated-types2.rs:9:26 + | +LL | type Fut<'a>: Future + | ^^^^^^^^^^^^ required by this bound in `MyTrait::Fut` +help: consider constraining the associated type `<::Fut<'a> as Future>::Output` to `i32` + | +LL | type Fut<'a> = impl Future + 'a + | ++++++++++++++ + +error: `impl` item signature doesn't match `trait` item signature + --> $DIR/async-associated-types2.rs:24:5 + | +LL | fn foo(&self) -> Self::Fut<'a>; + | ------------------------------- expected `fn(&'1 i32) -> ::Fut<'static>` +... +LL | fn foo<'a>(&'a self) -> Self::Fut<'a> { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 i32) -> ::Fut<'1>` + | + = note: expected `fn(&'1 i32) -> ::Fut<'static>` + found `fn(&'1 i32) -> ::Fut<'1>` +help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` + --> $DIR/async-associated-types2.rs:13:22 + | +LL | fn foo(&self) -> Self::Fut<'a>; + | ^^^^ consider borrowing this type parameter in the trait + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0261, E0271, E0658. +For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/async-example.rs b/src/test/ui/async-await/in-trait/async-example.rs new file mode 100644 index 0000000000000..2c2297c6b584b --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example.rs @@ -0,0 +1,17 @@ +// check-pass +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + async fn foo(&self) -> i32 { + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs new file mode 100644 index 0000000000000..d748430c5ee8e --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::fmt::Debug; +use std::hash::Hash; + +trait MyTrait { + async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; +} +//~^^ ERROR the parameter type `U` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +impl MyTrait for (T, U) { + async fn foo(&self) -> &(T, U) { + self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr new file mode 100644 index 0000000000000..8975ec21a015d --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr @@ -0,0 +1,37 @@ +error[E0311]: the parameter type `U` may not live long enough + --> $DIR/async-generics-and-bounds.rs:10:28 + | +LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; + | ^^^^^^^ + | +note: the parameter type `U` must be valid for the anonymous lifetime as defined here... + --> $DIR/async-generics-and-bounds.rs:10:18 + | +LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; + | ^ +note: ...so that the reference type `&(T, U)` does not outlive the data it points at + --> $DIR/async-generics-and-bounds.rs:10:28 + | +LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; + | ^^^^^^^ + +error[E0311]: the parameter type `T` may not live long enough + --> $DIR/async-generics-and-bounds.rs:10:28 + | +LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; + | ^^^^^^^ + | +note: the parameter type `T` must be valid for the anonymous lifetime as defined here... + --> $DIR/async-generics-and-bounds.rs:10:18 + | +LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; + | ^ +note: ...so that the reference type `&(T, U)` does not outlive the data it points at + --> $DIR/async-generics-and-bounds.rs:10:28 + | +LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; + | ^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0311`. diff --git a/src/test/ui/async-await/in-trait/async-generics.rs b/src/test/ui/async-await/in-trait/async-generics.rs new file mode 100644 index 0000000000000..3e6f70cd49f66 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics.rs @@ -0,0 +1,18 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo(&self) -> &(T, U); +} +//~^^ ERROR the parameter type `U` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +impl MyTrait for (T, U) { + async fn foo(&self) -> &(T, U) { + self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-generics.stderr b/src/test/ui/async-await/in-trait/async-generics.stderr new file mode 100644 index 0000000000000..84c7b3095368c --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-generics.stderr @@ -0,0 +1,37 @@ +error[E0311]: the parameter type `U` may not live long enough + --> $DIR/async-generics.rs:7:28 + | +LL | async fn foo(&self) -> &(T, U); + | ^^^^^^^ + | +note: the parameter type `U` must be valid for the anonymous lifetime as defined here... + --> $DIR/async-generics.rs:7:18 + | +LL | async fn foo(&self) -> &(T, U); + | ^ +note: ...so that the reference type `&(T, U)` does not outlive the data it points at + --> $DIR/async-generics.rs:7:28 + | +LL | async fn foo(&self) -> &(T, U); + | ^^^^^^^ + +error[E0311]: the parameter type `T` may not live long enough + --> $DIR/async-generics.rs:7:28 + | +LL | async fn foo(&self) -> &(T, U); + | ^^^^^^^ + | +note: the parameter type `T` must be valid for the anonymous lifetime as defined here... + --> $DIR/async-generics.rs:7:18 + | +LL | async fn foo(&self) -> &(T, U); + | ^ +note: ...so that the reference type `&(T, U)` does not outlive the data it points at + --> $DIR/async-generics.rs:7:28 + | +LL | async fn foo(&self) -> &(T, U); + | ^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0311`. diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs new file mode 100644 index 0000000000000..6a2276c96c8c1 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs @@ -0,0 +1,20 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::fmt::Debug; + +trait MyTrait<'a, 'b, T> { + async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized; +} +//~^^ ERROR the parameter type `Self` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { + async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + (self, key) + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr new file mode 100644 index 0000000000000..d74f80917a48d --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr @@ -0,0 +1,23 @@ +error[E0309]: the parameter type `Self` may not live long enough + --> $DIR/async-lifetimes-and-bounds.rs:9:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized; + | ^^^^^^^^^^^^^^^^^ + | + = help: consider adding an explicit lifetime bound `Self: 'a`... + = note: ...so that the reference type `&'a Self` does not outlive the data it points at + +error[E0309]: the parameter type `T` may not live long enough + --> $DIR/async-lifetimes-and-bounds.rs:9:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized; + | ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'b T` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound... + | +LL | trait MyTrait<'a, 'b, T: 'b> { + | ++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0309`. diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.rs b/src/test/ui/async-await/in-trait/async-lifetimes.rs new file mode 100644 index 0000000000000..1032a3435479d --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes.rs @@ -0,0 +1,18 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait<'a, 'b, T> { + async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); +} +//~^^ ERROR the parameter type `Self` may not live long enough +//~| ERROR the parameter type `T` may not live long enough + +impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { + async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { + (self, key) + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.stderr b/src/test/ui/async-await/in-trait/async-lifetimes.stderr new file mode 100644 index 0000000000000..780f75cf683d4 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-lifetimes.stderr @@ -0,0 +1,23 @@ +error[E0309]: the parameter type `Self` may not live long enough + --> $DIR/async-lifetimes.rs:7:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); + | ^^^^^^^^^^^^^^^^^ + | + = help: consider adding an explicit lifetime bound `Self: 'a`... + = note: ...so that the reference type `&'a Self` does not outlive the data it points at + +error[E0309]: the parameter type `T` may not live long enough + --> $DIR/async-lifetimes.rs:7:43 + | +LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); + | ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'b T` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound... + | +LL | trait MyTrait<'a, 'b, T: 'b> { + | ++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0309`. diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err.rs b/src/test/ui/async-await/in-trait/fn-not-async-err.rs new file mode 100644 index 0000000000000..f94d32145a290 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err.rs @@ -0,0 +1,17 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + fn foo(&self) -> i32 { + //~^ ERROR: `i32` is not a future [E0277] + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err.stderr b/src/test/ui/async-await/in-trait/fn-not-async-err.stderr new file mode 100644 index 0000000000000..03321dc5b5af1 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err.stderr @@ -0,0 +1,17 @@ +error[E0277]: `i32` is not a future + --> $DIR/fn-not-async-err.rs:11:22 + | +LL | fn foo(&self) -> i32 { + | ^^^ `i32` is not a future + | + = help: the trait `Future` is not implemented for `i32` + = note: i32 must be a future or must implement `IntoFuture` to be awaited +note: required by a bound in `MyTrait::foo::{opaque#0}` + --> $DIR/fn-not-async-err.rs:7:28 + | +LL | async fn foo(&self) -> i32; + | ^^^ required by this bound in `MyTrait::foo::{opaque#0}` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs new file mode 100644 index 0000000000000..f617a19ab3415 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + fn foo(&self) -> impl Future { + //~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `impl` method return [E0562] + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2.stderr b/src/test/ui/async-await/in-trait/fn-not-async-err2.stderr new file mode 100644 index 0000000000000..f591f18477290 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2.stderr @@ -0,0 +1,12 @@ +error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in `impl` method return + --> $DIR/fn-not-async-err2.rs:13:22 + | +LL | fn foo(&self) -> impl Future { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #91611 for more information + = help: add `#![feature(return_position_impl_trait_in_trait)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0562`. From 8a0ebca97eabaed14a36d90aa25327dcec2fa8b1 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Tue, 4 Oct 2022 23:35:49 +0000 Subject: [PATCH 09/13] Update static AFIT tests based on feedback --- .../in-trait/async-associated-types.rs | 2 +- .../async-associated-types2-desugared.rs | 31 --------- .../async-associated-types2-desugared.stderr | 42 ------------- .../in-trait/async-associated-types2.rs | 10 ++- .../in-trait/async-associated-types2.stderr | 63 ------------------- .../in-trait/fn-not-async-err2-rpitit.rs | 22 +++++++ .../async-await/in-trait/fn-not-async-err2.rs | 2 +- 7 files changed, 28 insertions(+), 144 deletions(-) delete mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs delete mode 100644 src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr delete mode 100644 src/test/ui/async-await/in-trait/async-associated-types2.stderr create mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs diff --git a/src/test/ui/async-await/in-trait/async-associated-types.rs b/src/test/ui/async-await/in-trait/async-associated-types.rs index 0269f00bbab73..8f679190d7a12 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types.rs +++ b/src/test/ui/async-await/in-trait/async-associated-types.rs @@ -6,7 +6,7 @@ use std::fmt::Debug; trait MyTrait<'a, 'b, T> where Self: 'a, T: Debug + Sized + 'b { - type MyAssoc;// = (&'a T, &'b U); + type MyAssoc; async fn foo(&'a self, key: &'b T) -> Self::MyAssoc; } diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs deleted file mode 100644 index 85d29eb703e76..0000000000000 --- a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.rs +++ /dev/null @@ -1,31 +0,0 @@ -// edition: 2021 - -#![feature(async_fn_in_trait)] -#![allow(incomplete_features)] - -use std::future::Future; - -trait MyTrait { - type Fut<'a>: Future - where - Self: 'a; - - async fn foo(&self) -> Self::Fut<'a>; - //~^ ERROR use of undeclared lifetime name `'a` - //~| ERROR the parameter type `Self` may not live long enough -} - -impl MyTrait for i32 { - type Fut<'a> = impl Future + 'a - where - Self: 'a; - //~^^^ ERROR `impl Trait` in type aliases is unstable - - fn foo<'a>(&'a self) -> Self::Fut<'a> { - async { - *self - } - } -} - -fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr b/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr deleted file mode 100644 index 6bc553b26350a..0000000000000 --- a/src/test/ui/async-await/in-trait/async-associated-types2-desugared.stderr +++ /dev/null @@ -1,42 +0,0 @@ -error[E0261]: use of undeclared lifetime name `'a` - --> $DIR/async-associated-types2-desugared.rs:13:38 - | -LL | async fn foo(&self) -> Self::Fut<'a>; - | ^^ undeclared lifetime - | -help: consider introducing lifetime `'a` here - | -LL | async fn foo<'a>(&self) -> Self::Fut<'a>; - | ++++ -help: consider introducing lifetime `'a` here - | -LL | trait MyTrait<'a> { - | ++++ - -error[E0658]: `impl Trait` in type aliases is unstable - --> $DIR/async-associated-types2-desugared.rs:19:20 - | -LL | type Fut<'a> = impl Future + 'a - | ^^^^^^^^^^^^^^^^ - | - = note: see issue #63063 for more information - = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable - -error[E0310]: the parameter type `Self` may not live long enough - --> $DIR/async-associated-types2-desugared.rs:13:28 - | -LL | async fn foo(&self) -> Self::Fut<'a>; - | ^^^^^^^^^^^^^ - | - = help: consider adding an explicit lifetime bound `Self: 'static`... - = note: ...so that the type `Self` will meet its required lifetime bounds... -note: ...that is required by this bound - --> $DIR/async-associated-types2-desugared.rs:11:15 - | -LL | Self: 'a; - | ^^ - -error: aborting due to 3 previous errors - -Some errors have detailed explanations: E0261, E0310, E0658. -For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.rs b/src/test/ui/async-await/in-trait/async-associated-types2.rs index c2133b04bc847..e546a0579c66f 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types2.rs +++ b/src/test/ui/async-await/in-trait/async-associated-types2.rs @@ -1,6 +1,8 @@ +// check-pass // edition: 2021 #![feature(async_fn_in_trait)] +#![feature(type_alias_impl_trait)] #![allow(incomplete_features)] use std::future::Future; @@ -10,19 +12,15 @@ trait MyTrait { where Self: 'a; - fn foo(&self) -> Self::Fut<'a>; - //~^ ERROR use of undeclared lifetime name `'a` + fn foo<'a>(&'a self) -> Self::Fut<'a>; } impl MyTrait for i32 { - type Fut<'a> = impl Future + 'a + type Fut<'a> = impl Future + 'a where Self: 'a; - //~^^^ ERROR `impl Trait` in type aliases is unstable - //~| ERROR expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` fn foo<'a>(&'a self) -> Self::Fut<'a> { - //~^ ERROR `impl` item signature doesn't match `trait` item signature async { *self } diff --git a/src/test/ui/async-await/in-trait/async-associated-types2.stderr b/src/test/ui/async-await/in-trait/async-associated-types2.stderr deleted file mode 100644 index f3343852fa093..0000000000000 --- a/src/test/ui/async-await/in-trait/async-associated-types2.stderr +++ /dev/null @@ -1,63 +0,0 @@ -error[E0261]: use of undeclared lifetime name `'a` - --> $DIR/async-associated-types2.rs:13:32 - | -LL | fn foo(&self) -> Self::Fut<'a>; - | ^^ undeclared lifetime - | -help: consider introducing lifetime `'a` here - | -LL | fn foo<'a>(&self) -> Self::Fut<'a>; - | ++++ -help: consider introducing lifetime `'a` here - | -LL | trait MyTrait<'a> { - | ++++ - -error[E0658]: `impl Trait` in type aliases is unstable - --> $DIR/async-associated-types2.rs:18:20 - | -LL | type Fut<'a> = impl Future + 'a - | ^^^^^^^^^^^^^^^^ - | - = note: see issue #63063 for more information - = help: add `#![feature(type_alias_impl_trait)]` to the crate attributes to enable - -error[E0271]: expected `::Fut<'a>` to be a future that resolves to `i32`, but it resolves to `<::Fut<'a> as Future>::Output` - --> $DIR/async-associated-types2.rs:18:20 - | -LL | type Fut<'a> = impl Future + 'a - | ^^^^^^^^^^^^^^^^ expected `i32`, found associated type - | - = note: expected type `i32` - found associated type `<::Fut<'a> as Future>::Output` -note: required by a bound in `MyTrait::Fut` - --> $DIR/async-associated-types2.rs:9:26 - | -LL | type Fut<'a>: Future - | ^^^^^^^^^^^^ required by this bound in `MyTrait::Fut` -help: consider constraining the associated type `<::Fut<'a> as Future>::Output` to `i32` - | -LL | type Fut<'a> = impl Future + 'a - | ++++++++++++++ - -error: `impl` item signature doesn't match `trait` item signature - --> $DIR/async-associated-types2.rs:24:5 - | -LL | fn foo(&self) -> Self::Fut<'a>; - | ------------------------------- expected `fn(&'1 i32) -> ::Fut<'static>` -... -LL | fn foo<'a>(&'a self) -> Self::Fut<'a> { - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 i32) -> ::Fut<'1>` - | - = note: expected `fn(&'1 i32) -> ::Fut<'static>` - found `fn(&'1 i32) -> ::Fut<'1>` -help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait` - --> $DIR/async-associated-types2.rs:13:22 - | -LL | fn foo(&self) -> Self::Fut<'a>; - | ^^^^ consider borrowing this type parameter in the trait - -error: aborting due to 4 previous errors - -Some errors have detailed explanations: E0261, E0271, E0658. -For more information about an error, try `rustc --explain E0261`. diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs new file mode 100644 index 0000000000000..faf9a43d352d5 --- /dev/null +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs @@ -0,0 +1,22 @@ +// check-pass +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + fn foo(&self) -> impl Future + '_ { + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs index f617a19ab3415..594baa91ad8ba 100644 --- a/src/test/ui/async-await/in-trait/fn-not-async-err2.rs +++ b/src/test/ui/async-await/in-trait/fn-not-async-err2.rs @@ -8,7 +8,7 @@ use std::future::Future; trait MyTrait { async fn foo(&self) -> i32; } - + impl MyTrait for i32 { fn foo(&self) -> impl Future { //~^ ERROR `impl Trait` only allowed in function and inherent method return types, not in `impl` method return [E0562] From 11b1439380d2e7e915207cb708e78a9060af82b8 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Fri, 7 Oct 2022 16:08:56 +0000 Subject: [PATCH 10/13] Update tests based on feedback --- .../in-trait/async-associated-types.rs | 4 ++-- .../in-trait/async-associated-types.stderr | 16 ++++++++-------- .../ui/async-await/in-trait/async-example.rs | 17 ++++++++++++++++- .../in-trait/async-generics-and-bounds.rs | 4 ++-- .../in-trait/async-generics-and-bounds.stderr | 12 ++++++------ .../ui/async-await/in-trait/async-generics.rs | 4 ++-- .../async-await/in-trait/async-generics.stderr | 12 ++++++------ .../in-trait/async-lifetimes-and-bounds.rs | 4 ++-- .../in-trait/async-lifetimes-and-bounds.stderr | 4 ++-- .../ui/async-await/in-trait/async-lifetimes.rs | 4 ++-- 10 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/test/ui/async-await/in-trait/async-associated-types.rs b/src/test/ui/async-await/in-trait/async-associated-types.rs index 8f679190d7a12..a6f928f3b1bac 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types.rs +++ b/src/test/ui/async-await/in-trait/async-associated-types.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -18,7 +20,5 @@ impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { (self, key) } } -//~^^^^ ERROR cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements -//~| ERROR cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-associated-types.stderr b/src/test/ui/async-await/in-trait/async-associated-types.stderr index c1c66f9023bd7..0985150eee0de 100644 --- a/src/test/ui/async-await/in-trait/async-associated-types.stderr +++ b/src/test/ui/async-await/in-trait/async-associated-types.stderr @@ -1,16 +1,16 @@ error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements - --> $DIR/async-associated-types.rs:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ | note: first, the lifetime cannot outlive the lifetime `'a` as defined here... - --> $DIR/async-associated-types.rs:14:6 + --> $DIR/async-associated-types.rs:16:6 | LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { | ^^ note: ...so that the types are compatible - --> $DIR/async-associated-types.rs:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ @@ -18,7 +18,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { found `(&U, &T)` = note: but, the lifetime must be valid for the static lifetime... note: ...so that the types are compatible - --> $DIR/async-associated-types.rs:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ @@ -26,18 +26,18 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { found `MyTrait<'_, '_, T>` error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'b` due to conflicting requirements - --> $DIR/async-associated-types.rs:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ | note: first, the lifetime cannot outlive the lifetime `'b` as defined here... - --> $DIR/async-associated-types.rs:14:10 + --> $DIR/async-associated-types.rs:16:10 | LL | impl<'a, 'b, T: Debug + Sized + 'b, U: 'a> MyTrait<'a, 'b, T> for U { | ^^ note: ...so that the types are compatible - --> $DIR/async-associated-types.rs:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ @@ -45,7 +45,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { found `(&U, &T)` = note: but, the lifetime must be valid for the static lifetime... note: ...so that the types are compatible - --> $DIR/async-associated-types.rs:17:43 + --> $DIR/async-associated-types.rs:19:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { | ^^^^^^^^^^^^^^ diff --git a/src/test/ui/async-await/in-trait/async-example.rs b/src/test/ui/async-await/in-trait/async-example.rs index 2c2297c6b584b..10387f09f0e9d 100644 --- a/src/test/ui/async-await/in-trait/async-example.rs +++ b/src/test/ui/async-await/in-trait/async-example.rs @@ -6,12 +6,27 @@ trait MyTrait { async fn foo(&self) -> i32; + async fn bar(&self) -> i32; } impl MyTrait for i32 { async fn foo(&self) -> i32 { *self } + + async fn bar(&self) -> i32 { + self.foo().await + } } -fn main() {} +fn main() { + let x = 5; + // Calling from non-async context + let _ = x.foo(); + let _ = x.bar(); + // Calling from async block in non-async context + async { + let _ = x.foo(); + let _ = x.bar(); + }; +} diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs index d748430c5ee8e..a73d55adfeced 100644 --- a/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -9,8 +11,6 @@ use std::hash::Hash; trait MyTrait { async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; } -//~^^ ERROR the parameter type `U` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl MyTrait for (T, U) { async fn foo(&self) -> &(T, U) { diff --git a/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr index 8975ec21a015d..5c8d64fc6cb4c 100644 --- a/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr +++ b/src/test/ui/async-await/in-trait/async-generics-and-bounds.stderr @@ -1,33 +1,33 @@ error[E0311]: the parameter type `U` may not live long enough - --> $DIR/async-generics-and-bounds.rs:10:28 + --> $DIR/async-generics-and-bounds.rs:12:28 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^^^^^^^ | note: the parameter type `U` must be valid for the anonymous lifetime as defined here... - --> $DIR/async-generics-and-bounds.rs:10:18 + --> $DIR/async-generics-and-bounds.rs:12:18 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^ note: ...so that the reference type `&(T, U)` does not outlive the data it points at - --> $DIR/async-generics-and-bounds.rs:10:28 + --> $DIR/async-generics-and-bounds.rs:12:28 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^^^^^^^ error[E0311]: the parameter type `T` may not live long enough - --> $DIR/async-generics-and-bounds.rs:10:28 + --> $DIR/async-generics-and-bounds.rs:12:28 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^^^^^^^ | note: the parameter type `T` must be valid for the anonymous lifetime as defined here... - --> $DIR/async-generics-and-bounds.rs:10:18 + --> $DIR/async-generics-and-bounds.rs:12:18 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^ note: ...so that the reference type `&(T, U)` does not outlive the data it points at - --> $DIR/async-generics-and-bounds.rs:10:28 + --> $DIR/async-generics-and-bounds.rs:12:28 | LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash; | ^^^^^^^ diff --git a/src/test/ui/async-await/in-trait/async-generics.rs b/src/test/ui/async-await/in-trait/async-generics.rs index 3e6f70cd49f66..67000e5770ee8 100644 --- a/src/test/ui/async-await/in-trait/async-generics.rs +++ b/src/test/ui/async-await/in-trait/async-generics.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -6,8 +8,6 @@ trait MyTrait { async fn foo(&self) -> &(T, U); } -//~^^ ERROR the parameter type `U` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl MyTrait for (T, U) { async fn foo(&self) -> &(T, U) { diff --git a/src/test/ui/async-await/in-trait/async-generics.stderr b/src/test/ui/async-await/in-trait/async-generics.stderr index 84c7b3095368c..6ae73d9e3a601 100644 --- a/src/test/ui/async-await/in-trait/async-generics.stderr +++ b/src/test/ui/async-await/in-trait/async-generics.stderr @@ -1,33 +1,33 @@ error[E0311]: the parameter type `U` may not live long enough - --> $DIR/async-generics.rs:7:28 + --> $DIR/async-generics.rs:9:28 | LL | async fn foo(&self) -> &(T, U); | ^^^^^^^ | note: the parameter type `U` must be valid for the anonymous lifetime as defined here... - --> $DIR/async-generics.rs:7:18 + --> $DIR/async-generics.rs:9:18 | LL | async fn foo(&self) -> &(T, U); | ^ note: ...so that the reference type `&(T, U)` does not outlive the data it points at - --> $DIR/async-generics.rs:7:28 + --> $DIR/async-generics.rs:9:28 | LL | async fn foo(&self) -> &(T, U); | ^^^^^^^ error[E0311]: the parameter type `T` may not live long enough - --> $DIR/async-generics.rs:7:28 + --> $DIR/async-generics.rs:9:28 | LL | async fn foo(&self) -> &(T, U); | ^^^^^^^ | note: the parameter type `T` must be valid for the anonymous lifetime as defined here... - --> $DIR/async-generics.rs:7:18 + --> $DIR/async-generics.rs:9:18 | LL | async fn foo(&self) -> &(T, U); | ^ note: ...so that the reference type `&(T, U)` does not outlive the data it points at - --> $DIR/async-generics.rs:7:28 + --> $DIR/async-generics.rs:9:28 | LL | async fn foo(&self) -> &(T, U); | ^^^^^^^ diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs index 6a2276c96c8c1..3f7448cecd157 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -8,8 +10,6 @@ use std::fmt::Debug; trait MyTrait<'a, 'b, T> { async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized; } -//~^^ ERROR the parameter type `Self` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { diff --git a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr index d74f80917a48d..0f02420274333 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr +++ b/src/test/ui/async-await/in-trait/async-lifetimes-and-bounds.stderr @@ -1,5 +1,5 @@ error[E0309]: the parameter type `Self` may not live long enough - --> $DIR/async-lifetimes-and-bounds.rs:9:43 + --> $DIR/async-lifetimes-and-bounds.rs:11:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized; | ^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug = note: ...so that the reference type `&'a Self` does not outlive the data it points at error[E0309]: the parameter type `T` may not live long enough - --> $DIR/async-lifetimes-and-bounds.rs:9:43 + --> $DIR/async-lifetimes-and-bounds.rs:11:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T) where T: Debug + Sized; | ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'b T` does not outlive the data it points at diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.rs b/src/test/ui/async-await/in-trait/async-lifetimes.rs index 1032a3435479d..5dc1672da68fe 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes.rs +++ b/src/test/ui/async-await/in-trait/async-lifetimes.rs @@ -5,9 +5,9 @@ trait MyTrait<'a, 'b, T> { async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); + //~^ ERROR the parameter type `Self` may not live long enough + //~| ERROR the parameter type `T` may not live long enough } -//~^^ ERROR the parameter type `Self` may not live long enough -//~| ERROR the parameter type `T` may not live long enough impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { async fn foo(&'a self, key: &'b T) -> (&'a U, &'b T) { From 9a05081d5fb13b3bee9d41b43692c9f38d521c96 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Fri, 7 Oct 2022 16:15:13 +0000 Subject: [PATCH 11/13] Add additional tests for static AFIT --- .../async-example-desugared-boxed-in-trait.rs | 21 +++++++++++++++++ ...nc-example-desugared-boxed-in-trait.stderr | 17 ++++++++++++++ .../in-trait/async-example-desugared-boxed.rs | 23 +++++++++++++++++++ .../async-example-desugared-in-trait.rs | 20 ++++++++++++++++ .../in-trait/async-example-desugared.rs | 22 ++++++++++++++++++ .../in-trait/async-recursive-generic.rs | 21 +++++++++++++++++ .../in-trait/async-recursive-generic.stderr | 12 ++++++++++ .../async-await/in-trait/async-recursive.rs | 21 +++++++++++++++++ .../in-trait/async-recursive.stderr | 12 ++++++++++ 9 files changed, 169 insertions(+) create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs create mode 100644 src/test/ui/async-await/in-trait/async-example-desugared.rs create mode 100644 src/test/ui/async-await/in-trait/async-recursive-generic.rs create mode 100644 src/test/ui/async-await/in-trait/async-recursive-generic.stderr create mode 100644 src/test/ui/async-await/in-trait/async-recursive.rs create mode 100644 src/test/ui/async-await/in-trait/async-recursive.stderr diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs new file mode 100644 index 0000000000000..38ba297189c69 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; +use std::pin::Pin; + +trait MyTrait { + fn foo(&self) -> Pin + '_>>; +} + +impl MyTrait for i32 { + async fn foo(&self) -> i32 { + //~^ ERROR method `foo` has an incompatible type for trait + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr new file mode 100644 index 0000000000000..22d2928f2f564 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed-in-trait.stderr @@ -0,0 +1,17 @@ +error[E0053]: method `foo` has an incompatible type for trait + --> $DIR/async-example-desugared-boxed-in-trait.rs:15:28 + | +LL | async fn foo(&self) -> i32 { + | ^^^ expected struct `Pin`, found opaque type + | +note: type in trait + --> $DIR/async-example-desugared-boxed-in-trait.rs:11:22 + | +LL | fn foo(&self) -> Pin + '_>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: expected fn pointer `fn(&i32) -> Pin>>` + found fn pointer `fn(&i32) -> impl Future` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0053`. diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs new file mode 100644 index 0000000000000..e1131753885b8 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs @@ -0,0 +1,23 @@ +// check-pass +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; +use std::pin::Pin; + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + fn foo(&self) -> Pin + '_>> { + Box::pin(async { + *self + }) + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs new file mode 100644 index 0000000000000..6f61781709e73 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs @@ -0,0 +1,20 @@ +// check-pass +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + fn foo(&self) -> impl Future + '_; +} + +impl MyTrait for i32 { + async fn foo(&self) -> i32 { + *self + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-example-desugared.rs b/src/test/ui/async-await/in-trait/async-example-desugared.rs new file mode 100644 index 0000000000000..faf9a43d352d5 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-example-desugared.rs @@ -0,0 +1,22 @@ +// check-pass +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![feature(return_position_impl_trait_in_trait)] +#![allow(incomplete_features)] + +use std::future::Future; + +trait MyTrait { + async fn foo(&self) -> i32; +} + +impl MyTrait for i32 { + fn foo(&self) -> impl Future + '_ { + async { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-recursive-generic.rs b/src/test/ui/async-await/in-trait/async-recursive-generic.rs new file mode 100644 index 0000000000000..6839abd381c3f --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive-generic.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo_recursive(&self, n: usize) -> T; +} + +impl MyTrait for T where T: Copy { + async fn foo_recursive(&self, n: usize) -> T { + //~^ ERROR recursion in an `async fn` requires boxing + if n > 0 { + self.foo_recursive(n - 1).await + } else { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-recursive-generic.stderr b/src/test/ui/async-await/in-trait/async-recursive-generic.stderr new file mode 100644 index 0000000000000..cab173bdd5b70 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive-generic.stderr @@ -0,0 +1,12 @@ +error[E0733]: recursion in an `async fn` requires boxing + --> $DIR/async-recursive-generic.rs:11:48 + | +LL | async fn foo_recursive(&self, n: usize) -> T { + | ^ recursive `async fn` + | + = note: a recursive `async fn` must be rewritten to return a boxed `dyn Future` + = note: consider using the `async_recursion` crate: https://crates.io/crates/async_recursion + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0733`. diff --git a/src/test/ui/async-await/in-trait/async-recursive.rs b/src/test/ui/async-await/in-trait/async-recursive.rs new file mode 100644 index 0000000000000..61119f8095bcc --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive.rs @@ -0,0 +1,21 @@ +// edition: 2021 + +#![feature(async_fn_in_trait)] +#![allow(incomplete_features)] + +trait MyTrait { + async fn foo_recursive(&self, n: usize) -> i32; +} + +impl MyTrait for i32 { + async fn foo_recursive(&self, n: usize) -> i32 { + //~^ ERROR recursion in an `async fn` requires boxing + if n > 0 { + self.foo_recursive(n - 1).await + } else { + *self + } + } +} + +fn main() {} diff --git a/src/test/ui/async-await/in-trait/async-recursive.stderr b/src/test/ui/async-await/in-trait/async-recursive.stderr new file mode 100644 index 0000000000000..9feff37b3fe46 --- /dev/null +++ b/src/test/ui/async-await/in-trait/async-recursive.stderr @@ -0,0 +1,12 @@ +error[E0733]: recursion in an `async fn` requires boxing + --> $DIR/async-recursive.rs:11:48 + | +LL | async fn foo_recursive(&self, n: usize) -> i32 { + | ^^^ recursive `async fn` + | + = note: a recursive `async fn` must be rewritten to return a boxed `dyn Future` + = note: consider using the `async_recursion` crate: https://crates.io/crates/async_recursion + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0733`. From 0b3b04643624027aadc891212dacfdcfa931ed0b Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Thu, 27 Oct 2022 15:47:30 -0700 Subject: [PATCH 12/13] Update src/test/ui/async-await/in-trait/async-example.rs Co-authored-by: Michael Goulet --- src/test/ui/async-await/in-trait/async-example.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/ui/async-await/in-trait/async-example.rs b/src/test/ui/async-await/in-trait/async-example.rs index 10387f09f0e9d..abf94ef7450fc 100644 --- a/src/test/ui/async-await/in-trait/async-example.rs +++ b/src/test/ui/async-await/in-trait/async-example.rs @@ -26,7 +26,7 @@ fn main() { let _ = x.bar(); // Calling from async block in non-async context async { - let _ = x.foo(); - let _ = x.bar(); + let _: i32 = x.foo().await; + let _: i32 = x.bar().await; }; } From bfdefdbfb740a2b41dbdf01299ecdf1610c54907 Mon Sep 17 00:00:00 2001 From: Bryan Garza <1396101+bryangarza@users.noreply.github.com> Date: Thu, 27 Oct 2022 22:55:50 +0000 Subject: [PATCH 13/13] Update tests based on feedback - Add comment to some tests that will break when #102745 is implemented - Mark a test with known-bug - Delete duplicate test --- .../in-trait/async-example-desugared-boxed.rs | 1 + .../async-example-desugared-in-trait.rs | 1 + .../in-trait/async-example-desugared.rs | 1 + .../async-await/in-trait/async-lifetimes.rs | 4 ++-- .../in-trait/async-lifetimes.stderr | 4 ++-- .../in-trait/fn-not-async-err2-rpitit.rs | 22 ------------------- 6 files changed, 7 insertions(+), 26 deletions(-) delete mode 100644 src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs index e1131753885b8..61d7e2520eab7 100644 --- a/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs +++ b/src/test/ui/async-await/in-trait/async-example-desugared-boxed.rs @@ -13,6 +13,7 @@ trait MyTrait { } impl MyTrait for i32 { + // This will break once a PR that implements #102745 is merged fn foo(&self) -> Pin + '_>> { Box::pin(async { *self diff --git a/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs index 6f61781709e73..feeda719e0306 100644 --- a/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs +++ b/src/test/ui/async-await/in-trait/async-example-desugared-in-trait.rs @@ -12,6 +12,7 @@ trait MyTrait { } impl MyTrait for i32 { + // This will break once a PR that implements #102745 is merged async fn foo(&self) -> i32 { *self } diff --git a/src/test/ui/async-await/in-trait/async-example-desugared.rs b/src/test/ui/async-await/in-trait/async-example-desugared.rs index faf9a43d352d5..1313c9edd861c 100644 --- a/src/test/ui/async-await/in-trait/async-example-desugared.rs +++ b/src/test/ui/async-await/in-trait/async-example-desugared.rs @@ -12,6 +12,7 @@ trait MyTrait { } impl MyTrait for i32 { + // This will break once a PR that implements #102745 is merged fn foo(&self) -> impl Future + '_ { async { *self diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.rs b/src/test/ui/async-await/in-trait/async-lifetimes.rs index 5dc1672da68fe..acbac471cf7e8 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes.rs +++ b/src/test/ui/async-await/in-trait/async-lifetimes.rs @@ -1,3 +1,5 @@ +// check-fail +// known-bug: #102682 // edition: 2021 #![feature(async_fn_in_trait)] @@ -5,8 +7,6 @@ trait MyTrait<'a, 'b, T> { async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); - //~^ ERROR the parameter type `Self` may not live long enough - //~| ERROR the parameter type `T` may not live long enough } impl<'a, 'b, T, U> MyTrait<'a, 'b, T> for U { diff --git a/src/test/ui/async-await/in-trait/async-lifetimes.stderr b/src/test/ui/async-await/in-trait/async-lifetimes.stderr index 780f75cf683d4..9a7d294bb1766 100644 --- a/src/test/ui/async-await/in-trait/async-lifetimes.stderr +++ b/src/test/ui/async-await/in-trait/async-lifetimes.stderr @@ -1,5 +1,5 @@ error[E0309]: the parameter type `Self` may not live long enough - --> $DIR/async-lifetimes.rs:7:43 + --> $DIR/async-lifetimes.rs:9:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); | ^^^^^^^^^^^^^^^^^ @@ -8,7 +8,7 @@ LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); = note: ...so that the reference type `&'a Self` does not outlive the data it points at error[E0309]: the parameter type `T` may not live long enough - --> $DIR/async-lifetimes.rs:7:43 + --> $DIR/async-lifetimes.rs:9:43 | LL | async fn foo(&'a self, key: &'b T) -> (&'a Self, &'b T); | ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'b T` does not outlive the data it points at diff --git a/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs b/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs deleted file mode 100644 index faf9a43d352d5..0000000000000 --- a/src/test/ui/async-await/in-trait/fn-not-async-err2-rpitit.rs +++ /dev/null @@ -1,22 +0,0 @@ -// check-pass -// edition: 2021 - -#![feature(async_fn_in_trait)] -#![feature(return_position_impl_trait_in_trait)] -#![allow(incomplete_features)] - -use std::future::Future; - -trait MyTrait { - async fn foo(&self) -> i32; -} - -impl MyTrait for i32 { - fn foo(&self) -> impl Future + '_ { - async { - *self - } - } -} - -fn main() {}