Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 8 pull requests #123762

Merged
merged 19 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
8923b58
Document restricted_std
adamgemmell Feb 28, 2024
8a24ddf
Be more specific when flagging imports that are redundant due to the …
fmease Mar 23, 2024
0ef49fe
Stabilize `cstr_count_bytes`
tgross35 Apr 9, 2024
796be88
Use `fn` ptr signature instead of `{closure@..}` in infer error
estebank Apr 10, 2024
c8490a0
skip `unused_parens`'s suggestion for `Paren` in macro.
surechen Apr 1, 2024
88bcc79
Revert "Put basic impls for f16 and f128 behind cfg(not(bootstrap))"
tgross35 Apr 5, 2024
454de78
Add basic library support for `f16` and `f128`
tgross35 Mar 6, 2024
143ecc3
Add basic f16 and f128 modules
tgross35 Mar 26, 2024
311ad55
Add primitive documentation for `f16` and `f128`
tgross35 Apr 6, 2024
f0fd5ad
clean up docs for `File::sync_*`
Apr 10, 2024
3764af6
Use suggest_impl_trait in return type suggestion
compiler-errors Apr 10, 2024
aac3f24
Rollup merge of #122470 - tgross35:f16-f128-step4-libs-min, r=Amanieu
fmease Apr 10, 2024
ccab2b1
Rollup merge of #122954 - fmease:defined-by-extern-prelude, r=petroch…
fmease Apr 10, 2024
82c6f18
Rollup merge of #123314 - surechen:fix_120642, r=Nadrieril
fmease Apr 10, 2024
084d27b
Rollup merge of #123360 - adamgemmell:dev/adagem01/restricted-std, r=…
fmease Apr 10, 2024
d7d5be0
Rollup merge of #123661 - tgross35:stabilize-cstr_count_bytes, r=dtolnay
fmease Apr 10, 2024
ae88766
Rollup merge of #123703 - estebank:diag-changes-2, r=Nadrieril
fmease Apr 10, 2024
b24d2ad
Rollup merge of #123756 - lukas-code:file-sync, r=jhpratt
fmease Apr 10, 2024
2930dce
Rollup merge of #123761 - compiler-errors:suggest-more-impl-trait, r=…
fmease Apr 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 56 additions & 42 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rustc_middle::query::Providers;
use rustc_middle::ty::util::{Discr, IntTypeExt};
use rustc_middle::ty::{self, AdtKind, Const, IsSuggestable, ToPredicate, Ty, TyCtxt};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::Span;
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::FieldIdx;
use rustc_target::spec::abi;
use rustc_trait_selection::infer::InferCtxtExt;
Expand Down Expand Up @@ -1383,7 +1383,9 @@ fn infer_return_ty_for_fn_sig<'tcx>(
Applicability::MachineApplicable,
);
should_recover = true;
} else if let Some(sugg) = suggest_impl_trait(tcx, ret_ty, ty.span, def_id) {
} else if let Some(sugg) =
suggest_impl_trait(&tcx.infer_ctxt().build(), tcx.param_env(def_id), ret_ty)
{
diag.span_suggestion(
ty.span,
"replace with an appropriate return type",
Expand Down Expand Up @@ -1426,11 +1428,10 @@ fn infer_return_ty_for_fn_sig<'tcx>(
}
}

fn suggest_impl_trait<'tcx>(
tcx: TyCtxt<'tcx>,
pub fn suggest_impl_trait<'tcx>(
infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
ret_ty: Ty<'tcx>,
span: Span,
def_id: LocalDefId,
) -> Option<String> {
let format_as_assoc: fn(_, _, _, _, _) -> _ =
|tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -1464,24 +1465,28 @@ fn suggest_impl_trait<'tcx>(

for (trait_def_id, assoc_item_def_id, formatter) in [
(
tcx.get_diagnostic_item(sym::Iterator),
tcx.get_diagnostic_item(sym::IteratorItem),
infcx.tcx.get_diagnostic_item(sym::Iterator),
infcx.tcx.get_diagnostic_item(sym::IteratorItem),
format_as_assoc,
),
(
tcx.lang_items().future_trait(),
tcx.get_diagnostic_item(sym::FutureOutput),
infcx.tcx.lang_items().future_trait(),
infcx.tcx.get_diagnostic_item(sym::FutureOutput),
format_as_assoc,
),
(tcx.lang_items().fn_trait(), tcx.lang_items().fn_once_output(), format_as_parenthesized),
(
tcx.lang_items().fn_mut_trait(),
tcx.lang_items().fn_once_output(),
infcx.tcx.lang_items().fn_trait(),
infcx.tcx.lang_items().fn_once_output(),
format_as_parenthesized,
),
(
infcx.tcx.lang_items().fn_mut_trait(),
infcx.tcx.lang_items().fn_once_output(),
format_as_parenthesized,
),
(
tcx.lang_items().fn_once_trait(),
tcx.lang_items().fn_once_output(),
infcx.tcx.lang_items().fn_once_trait(),
infcx.tcx.lang_items().fn_once_output(),
format_as_parenthesized,
),
] {
Expand All @@ -1491,36 +1496,45 @@ fn suggest_impl_trait<'tcx>(
let Some(assoc_item_def_id) = assoc_item_def_id else {
continue;
};
if tcx.def_kind(assoc_item_def_id) != DefKind::AssocTy {
if infcx.tcx.def_kind(assoc_item_def_id) != DefKind::AssocTy {
continue;
}
let param_env = tcx.param_env(def_id);
let infcx = tcx.infer_ctxt().build();
let args = ty::GenericArgs::for_item(tcx, trait_def_id, |param, _| {
if param.index == 0 { ret_ty.into() } else { infcx.var_for_def(span, param) }
let sugg = infcx.probe(|_| {
let args = ty::GenericArgs::for_item(infcx.tcx, trait_def_id, |param, _| {
if param.index == 0 { ret_ty.into() } else { infcx.var_for_def(DUMMY_SP, param) }
});
if !infcx
.type_implements_trait(trait_def_id, args, param_env)
.must_apply_modulo_regions()
{
return None;
}
let ocx = ObligationCtxt::new(&infcx);
let item_ty = ocx.normalize(
&ObligationCause::dummy(),
param_env,
Ty::new_projection(infcx.tcx, assoc_item_def_id, args),
);
// FIXME(compiler-errors): We may benefit from resolving regions here.
if ocx.select_where_possible().is_empty()
&& let item_ty = infcx.resolve_vars_if_possible(item_ty)
&& let Some(item_ty) = item_ty.make_suggestable(infcx.tcx, false, None)
&& let Some(sugg) = formatter(
infcx.tcx,
infcx.resolve_vars_if_possible(args),
trait_def_id,
assoc_item_def_id,
item_ty,
)
{
return Some(sugg);
}

None
});
if !infcx.type_implements_trait(trait_def_id, args, param_env).must_apply_modulo_regions() {
continue;
}
let ocx = ObligationCtxt::new(&infcx);
let item_ty = ocx.normalize(
&ObligationCause::misc(span, def_id),
param_env,
Ty::new_projection(tcx, assoc_item_def_id, args),
);
// FIXME(compiler-errors): We may benefit from resolving regions here.
if ocx.select_where_possible().is_empty()
&& let item_ty = infcx.resolve_vars_if_possible(item_ty)
&& let Some(item_ty) = item_ty.make_suggestable(tcx, false, None)
&& let Some(sugg) = formatter(
tcx,
infcx.resolve_vars_if_possible(args),
trait_def_id,
assoc_item_def_id,
item_ty,
)
{
return Some(sugg);

if sugg.is_some() {
return sugg;
}
}
None
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rustc_hir::{
CoroutineDesugaring, CoroutineKind, CoroutineSource, Expr, ExprKind, GenericBound, HirId, Node,
Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
};
use rustc_hir_analysis::collect::suggest_impl_trait;
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
use rustc_infer::traits::{self};
use rustc_middle::lint::in_external_macro;
Expand Down Expand Up @@ -814,17 +815,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
errors::AddReturnTypeSuggestion::Add { span, found: found.to_string() },
);
return true;
} else if let ty::Closure(_, args) = found.kind()
// FIXME(compiler-errors): Get better at printing binders...
&& let closure = args.as_closure()
&& closure.sig().is_suggestable(self.tcx, false)
{
} else if let Some(sugg) = suggest_impl_trait(self, self.param_env, found) {
err.subdiagnostic(
self.dcx(),
errors::AddReturnTypeSuggestion::Add {
span,
found: closure.print_as_impl_trait().to_string(),
},
errors::AddReturnTypeSuggestion::Add { span, found: sugg },
);
return true;
} else {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_infer/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ infer_fps_items_are_distinct = fn items are distinct from fn pointers
infer_fps_remove_ref = consider removing the reference
infer_fps_use_ref = consider using a reference
infer_fulfill_req_lifetime = the type `{$ty}` does not fulfill the required lifetime

infer_full_type_written = the full type name has been written to '{$path}'

infer_implicit_static_lifetime_note = this has an implicit `'static` lifetime requirement
infer_implicit_static_lifetime_suggestion = consider relaxing the implicit `'static` requirement
infer_label_bad = {$bad_kind ->
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_infer/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::infer::error_reporting::{
ObligationCauseAsDiagArg,
};

use std::path::PathBuf;

pub mod note_and_explain;

#[derive(Diagnostic)]
Expand Down Expand Up @@ -47,6 +49,9 @@ pub struct AnnotationRequired<'a> {
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
#[subdiagnostic]
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
#[note(infer_full_type_written)]
pub was_written: Option<()>,
pub path: PathBuf,
}

// Copy of `AnnotationRequired` for E0283
Expand All @@ -65,6 +70,9 @@ pub struct AmbiguousImpl<'a> {
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
#[subdiagnostic]
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
#[note(infer_full_type_written)]
pub was_written: Option<()>,
pub path: PathBuf,
}

// Copy of `AnnotationRequired` for E0284
Expand All @@ -83,6 +91,9 @@ pub struct AmbiguousReturn<'a> {
pub infer_subdiags: Vec<SourceKindSubdiag<'a>>,
#[subdiagnostic]
pub multi_suggestions: Vec<SourceKindMultiSuggestion<'a>>,
#[note(infer_full_type_written)]
pub was_written: Option<()>,
pub path: PathBuf,
}

// Used when a better one isn't available
Expand Down
Loading
Loading