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 10 pull requests #105720

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
259c37e
Ensure async trait impls are async (or otherwise return an opaque type)
ComputerDruid Nov 3, 2022
bc92321
Suggest constraining type parameter with `Clone`
estebank Dec 14, 2022
0fb8d84
Suggest `#[derive(Clone)]`
estebank Dec 14, 2022
687b4d9
Add regression test for #104678
JohnTitor Dec 14, 2022
5f5ae17
Consider discriminant fields that are ordered before variant fields
compiler-errors Dec 12, 2022
bdc3c4b
Make print_type_sizes test not use feature(start)
compiler-errors Dec 12, 2022
10368c6
Point at method chains on `E0271` errors
estebank Dec 13, 2022
36b60e7
Use `with_forced_trimmed_paths` more
estebank Dec 13, 2022
50e269f
Shorten trimmed display of closures
estebank Dec 14, 2022
e9ef36a
Trim paths in E0599
estebank Dec 14, 2022
7f8fdf4
Consider lifetimes when comparing assoc types in method chain
estebank Dec 14, 2022
ae60015
Use impl's def id when calculating type to specify UFCS
compiler-errors Nov 12, 2022
1225a65
drive-by: Fix path spans
compiler-errors Nov 12, 2022
7bf36de
Make report_projection_error more term agnostic
compiler-errors Dec 12, 2022
cfa6a93
Auto traits in dyn are suggestable
compiler-errors Dec 12, 2022
34d194d
Highlight conflicting param-env candidates, again
compiler-errors Nov 15, 2022
d10f6b4
Add test
compiler-errors Dec 5, 2022
c99f1b7
Run `x test tidy` sooner in mingw-check
jyn514 Dec 14, 2022
fc6d59d
Make `RUN_CHECK_WITH_PARALLEL_QUERIES` the last thing to run
jyn514 Dec 14, 2022
ef10913
Rollup merge of #104334 - compiler-errors:ufcs-sugg-wrong-def-id, r=e…
matthiaskrgr Dec 14, 2022
79653c8
Rollup merge of #104592 - ComputerDruid:async_check, r=compiler-errors
matthiaskrgr Dec 14, 2022
a60bb44
Rollup merge of #105285 - compiler-errors:conflicting-param-env-2, r=…
matthiaskrgr Dec 14, 2022
fbe00f0
Rollup merge of #105623 - compiler-errors:generator-type-size-fix, r=…
matthiaskrgr Dec 14, 2022
4366327
Rollup merge of #105627 - compiler-errors:dyn-auto-suggestable, r=dav…
matthiaskrgr Dec 14, 2022
ae6dfa6
Rollup merge of #105633 - compiler-errors:term-agnostic, r=oli-obk
matthiaskrgr Dec 14, 2022
567d887
Rollup merge of #105674 - estebank:iterator-chains, r=oli-obk
matthiaskrgr Dec 14, 2022
62b1646
Rollup merge of #105679 - estebank:suggest-clone, r=compiler-errors
matthiaskrgr Dec 14, 2022
1c35840
Rollup merge of #105692 - JohnTitor:issue-104678, r=compiler-errors
matthiaskrgr Dec 14, 2022
cb19a7f
Rollup merge of #105714 - jyn514:tidy-first, r=Mark-Simulacrum
matthiaskrgr Dec 14, 2022
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
4 changes: 4 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/hir_analysis.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ hir_analysis_lifetimes_or_bounds_mismatch_on_trait =
.where_label = this `where` clause might not match the one in the trait
.bounds_label = this bound might be missing in the impl

hir_analysis_async_trait_impl_should_be_async =
method `{$method_name}` should be async because the method from the trait is async
.trait_item_label = required because the trait method is async

hir_analysis_drop_impl_on_wrong_item =
the `Drop` trait may only be implemented for local structs, enums, and unions
.label = must be a struct, enum, or union in the current crate
Expand Down
32 changes: 32 additions & 0 deletions compiler/rustc_hir_analysis/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ pub(crate) fn compare_impl_method<'tcx>(
return;
}

if let Err(_) = compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span) {
return;
}

if let Err(_) = compare_predicate_entailment(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)
{
return;
Expand Down Expand Up @@ -323,6 +327,34 @@ fn compare_predicate_entailment<'tcx>(
Ok(())
}

fn compare_asyncness<'tcx>(
tcx: TyCtxt<'tcx>,
impl_m: &ty::AssocItem,
impl_m_span: Span,
trait_m: &ty::AssocItem,
trait_item_span: Option<Span>,
) -> Result<(), ErrorGuaranteed> {
if tcx.asyncness(trait_m.def_id) == hir::IsAsync::Async {
match tcx.fn_sig(impl_m.def_id).skip_binder().output().kind() {
ty::Opaque(..) => {
// allow both `async fn foo()` and `fn foo() -> impl Future`
}
ty::Error(rustc_errors::ErrorGuaranteed { .. }) => {
// We don't know if it's ok, but at least it's already an error.
}
_ => {
return Err(tcx.sess.emit_err(crate::errors::AsyncTraitImplShouldBeAsync {
span: impl_m_span,
method_name: trait_m.name,
trait_item_span,
}));
}
};
}

Ok(())
}

#[instrument(skip(tcx), level = "debug", ret)]
pub fn collect_trait_impl_trait_tys<'tcx>(
tcx: TyCtxt<'tcx>,
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ pub struct LifetimesOrBoundsMismatchOnTrait {
pub ident: Ident,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_async_trait_impl_should_be_async)]
pub struct AsyncTraitImplShouldBeAsync {
#[primary_span]
// #[label]
pub span: Span,
#[label(trait_item_label)]
pub trait_item_span: Option<Span>,
pub method_name: Symbol,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_drop_impl_on_wrong_item, code = "E0120")]
pub struct DropImplOnWrongItem {
Expand Down
30 changes: 23 additions & 7 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer;
use rustc_infer::traits::{self, StatementAsExpression};
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty};
use rustc_middle::ty::{
self, suggest_constraining_type_params, Binder, DefIdTree, IsSuggestable, ToPredicate, Ty,
};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::symbol::sym;
use rustc_span::Span;
Expand Down Expand Up @@ -1276,17 +1278,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&& !results.expr_adjustments(callee_expr).iter().any(|adj| matches!(adj.kind, ty::adjustment::Adjust::Deref(..)))
// Check that we're in fact trying to clone into the expected type
&& self.can_coerce(*pointee_ty, expected_ty)
&& let predicate = ty::Binder::dummy(ty::TraitRef {
def_id: clone_trait_did,
substs: self.tcx.mk_substs([expected_ty.into()].iter()),
})
.without_const()
.to_predicate(self.tcx)
// And the expected type doesn't implement `Clone`
&& !self.predicate_must_hold_considering_regions(&traits::Obligation {
cause: traits::ObligationCause::dummy(),
param_env: self.param_env,
recursion_depth: 0,
predicate: ty::Binder::dummy(ty::TraitRef {
def_id: clone_trait_did,
substs: self.tcx.mk_substs([expected_ty.into()].iter()),
})
.without_const()
.to_predicate(self.tcx),
predicate,
})
{
diag.span_note(
Expand All @@ -1295,6 +1298,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"`{expected_ty}` does not implement `Clone`, so `{found_ty}` was cloned instead"
),
);
let owner = self.tcx.hir().enclosing_body_owner(expr.hir_id);
if let ty::Param(param) = expected_ty.kind()
&& let Some(generics) = self.tcx.hir().get_generics(owner)
{
suggest_constraining_type_params(
self.tcx,
generics,
diag,
vec![(param.name.as_str(), "Clone", Some(clone_trait_did))].into_iter(),
);
} else {
self.suggest_derive(diag, &[(predicate, None, None)]);
}
}
}

Expand Down
24 changes: 17 additions & 7 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
use rustc_middle::traits::util::supertraits;
use rustc_middle::ty::fast_reject::DeepRejectCtxt;
use rustc_middle::ty::fast_reject::{simplify_type, TreatParams};
use rustc_middle::ty::print::with_crate_prefix;
use rustc_middle::ty::print::{with_crate_prefix, with_forced_trimmed_paths};
use rustc_middle::ty::{self, DefIdTree, GenericArgKind, Ty, TyCtxt, TypeVisitable};
use rustc_middle::ty::{IsSuggestable, ToPolyTraitRef};
use rustc_span::symbol::{kw, sym, Ident};
Expand Down Expand Up @@ -270,7 +270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let tcx = self.tcx;

let rcvr_ty = self.resolve_vars_if_possible(rcvr_ty);
let ty_str = self.ty_to_string(rcvr_ty);
let ty_str = with_forced_trimmed_paths!(self.ty_to_string(rcvr_ty));
let is_method = mode == Mode::MethodCall;
let item_kind = if is_method {
"method"
Expand Down Expand Up @@ -565,7 +565,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let term = pred.skip_binder().term;

let obligation = format!("{} = {}", projection_ty, term);
let quiet = format!("{} = {}", quiet_projection_ty, term);
let quiet = with_forced_trimmed_paths!(format!(
"{} = {}",
quiet_projection_ty, term
));

bound_span_label(projection_ty.self_ty(), &obligation, &quiet);
Some((obligation, projection_ty.self_ty()))
Expand All @@ -575,7 +578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let self_ty = p.self_ty();
let path = p.print_only_trait_path();
let obligation = format!("{}: {}", self_ty, path);
let quiet = format!("_: {}", path);
let quiet = with_forced_trimmed_paths!(format!("_: {}", path));
bound_span_label(self_ty, &obligation, &quiet);
Some((obligation, self_ty))
}
Expand Down Expand Up @@ -798,7 +801,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(None, None)
};
let primary_message = primary_message.unwrap_or_else(|| format!(
"the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, but its trait bounds were not satisfied"
"the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, \
but its trait bounds were not satisfied"
));
err.set_primary_message(&primary_message);
if let Some(label) = label {
Expand Down Expand Up @@ -895,7 +899,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
} else {
err.span_label(span, format!("{item_kind} cannot be called on `{ty_str}` due to unsatisfied trait bounds"));
err.span_label(
span,
format!(
"{item_kind} cannot be called on `{ty_str}` due to unsatisfied \
trait bounds",
),
);
}
};

Expand Down Expand Up @@ -1843,7 +1853,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.suggest_derive(err, &preds);
}

fn suggest_derive(
pub fn suggest_derive(
&self,
err: &mut Diagnostic,
unsatisfied_predicates: &[(
Expand Down
15 changes: 2 additions & 13 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use std::ops::ControlFlow;

use crate::ty::{
visit::TypeVisitable, AliasTy, Const, ConstKind, DefIdTree, ExistentialPredicate, InferConst,
InferTy, Opaque, PolyTraitPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor,
visit::TypeVisitable, AliasTy, Const, ConstKind, DefIdTree, InferConst, InferTy, Opaque,
PolyTraitPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitor,
};

use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -469,17 +469,6 @@ impl<'tcx> TypeVisitor<'tcx> for IsSuggestableVisitor<'tcx> {
}
}

Dynamic(dty, _, _) => {
for pred in *dty {
match pred.skip_binder() {
ExistentialPredicate::Trait(_) | ExistentialPredicate::Projection(_) => {
// Okay
}
_ => return ControlFlow::Break(()),
}
}
}

Param(param) => {
// FIXME: It would be nice to make this not use string manipulation,
// but it's pretty hard to do this, since `ty::ParamTy` is missing
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use rustc_session::config::TrimmedDefPaths;
use rustc_session::cstore::{ExternCrate, ExternCrateSource};
use rustc_session::Limit;
use rustc_span::symbol::{kw, Ident, Symbol};
use rustc_span::FileNameDisplayPreference;
use rustc_target::abi::Size;
use rustc_target::spec::abi::Abi;
use smallvec::SmallVec;
Expand Down Expand Up @@ -818,11 +819,16 @@ pub trait PrettyPrinter<'tcx>:
p!("@", print_def_path(did.to_def_id(), substs));
} else {
let span = self.tcx().def_span(did);
let preference = if FORCE_TRIMMED_PATH.with(|flag| flag.get()) {
FileNameDisplayPreference::Short
} else {
FileNameDisplayPreference::Remapped
};
p!(write(
"@{}",
// This may end up in stderr diagnostics but it may also be emitted
// into MIR. Hence we use the remapped path if available
self.tcx().sess.source_map().span_to_embeddable_string(span)
self.tcx().sess.source_map().span_to_string(span, preference)
));
}
} else {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ impl RealFileName {
FileNameDisplayPreference::Remapped => {
self.remapped_path_if_available().to_string_lossy()
}
FileNameDisplayPreference::Short => self
.local_path_if_available()
.file_name()
.map_or_else(|| "".into(), |f| f.to_string_lossy()),
}
}
}
Expand Down Expand Up @@ -302,6 +306,9 @@ pub enum FileNameDisplayPreference {
/// Display the path before the application of rewrite rules provided via `--remap-path-prefix`.
/// This is appropriate for use in user-facing output (such as diagnostics).
Local,
/// Display only the filename, as a way to reduce the verbosity of the output.
/// This is appropriate for use in user-facing output (such as diagnostics).
Short,
}

pub struct FileNameDisplay<'a> {
Expand Down
15 changes: 11 additions & 4 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,20 +438,27 @@ impl SourceMap {
}
}

fn span_to_string(&self, sp: Span, filename_display_pref: FileNameDisplayPreference) -> String {
pub fn span_to_string(
&self,
sp: Span,
filename_display_pref: FileNameDisplayPreference,
) -> String {
if self.files.borrow().source_files.is_empty() || sp.is_dummy() {
return "no-location".to_string();
}

let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi());
format!(
"{}:{}:{}: {}:{}",
"{}:{}:{}{}",
lo.file.name.display(filename_display_pref),
lo.line,
lo.col.to_usize() + 1,
hi.line,
hi.col.to_usize() + 1,
if let FileNameDisplayPreference::Short = filename_display_pref {
String::new()
} else {
format!(": {}:{}", hi.line, hi.col.to_usize() + 1)
}
)
}

Expand Down
Loading