Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
26cfeb1
Silence errors in experssions caused by bare traits in paths
estebank May 30, 2024
936b692
Silence unsized types errors in some expressions
estebank May 31, 2024
5028c30
Avoid potential for ICE by not stashing delayed as bug error and sile…
estebank May 31, 2024
f1f9842
Do not silence some sized errors as they would cause ICEs
estebank Jun 3, 2024
5441f71
Add more tracking for the HIR source of well-formedness requirement
estebank Jun 3, 2024
9a8f343
Add tests for various `dyn Trait` in path cases
estebank Jun 3, 2024
9ada8bc
Add more HIR tracking information to `ObligationCauseCode`
estebank Jun 3, 2024
5d85ffa
On object safety errors, point at specific object unsafe trait in path
estebank Jun 3, 2024
e2f2151
Move `FnCtxt::get_fn_decl` to `TyCtxt`
estebank Jun 3, 2024
230a787
Deduplicate more E0038 object safety errors by pointing at type more …
estebank Jun 3, 2024
e55ab93
Move suggestions out of main error logic
estebank Jun 8, 2024
c9e8029
Tweak wording and only suggest `dyn Trait` if `Trait` is object safe
estebank Jun 8, 2024
adc750f
Suggest `<_ as Default>::default()` when given `<Default>::default()`
estebank Jun 8, 2024
dd7b36f
review comment: `guard` -> `guar`
estebank Jun 8, 2024
e6d9050
review comment: unnecessary check for object safety on sized trait
estebank Jun 8, 2024
5f22c02
Add explanatory comment
estebank Jun 8, 2024
84be44b
Fix tidy
estebank Jun 10, 2024
d8f5b14
Remove conditional delay_as_bug from "missing `dyn` error"
estebank Jun 10, 2024
8ee6812
Remove `delay_as_bug` for fn call with unsized `Self` that return `Se…
estebank Jun 10, 2024
8140dcd
Emit method not found error for object safe traits without `dyn`
estebank Jun 10, 2024
f116d4d
Move tests that no longer ICE
estebank Jun 10, 2024
aec9eeb
Do not produce incorrect `dyn Trait` suggestion in `fn`
estebank Jun 10, 2024
05bffa0
Mention associated type with same name as trait in `E0782`
estebank Jun 11, 2024
88ccfa4
Account for `static` and `const` in suggestion
estebank Jun 11, 2024
a62bfcd
Use verbose format for fully-qualified path suggestion
estebank Jun 11, 2024
7f923f3
Fix tidy
estebank Jun 11, 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
Prev Previous commit
Next Next commit
Do not produce incorrect dyn Trait suggestion in fn
```
  error[E0782]: trait objects must include the `dyn` keyword
    --> $DIR/not-on-bare-trait-2021.rs:8:12
     |
  LL | fn foo(_x: Foo + Send) {
     |            ^^^^^^^^^^
     |
  help: use a new generic type parameter, constrained by `Foo + Send`
     |
  LL | fn foo<T: Foo + Send>(_x: T) {
     |       +++++++++++++++     ~
  help: you can also use an opaque type, but users won't be able to specify the type parameter when calling the `fn`, having to rely exclusively on type inference
     |
  LL | fn foo(_x: impl Foo + Send) {
     |            ++++
  help: alternatively, use a trait object to accept any type that implements `Foo + Send`, accessing its methods at runtime using dynamic dispatch
     |
  LL | fn foo(_x: &(dyn Foo + Send)) {
     |            +++++           +
- help: add `dyn` keyword before this trait
-    |
- LL | fn foo(_x: dyn Foo + Send) {
-    |            +++
```
  • Loading branch information
estebank committed Jun 10, 2024
commit aec9eeba18e357f905e453081bf21764b01c9797
34 changes: 9 additions & 25 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let mut diag =
rustc_errors::struct_span_code_err!(tcx.dcx(), self_ty.span, E0782, "{}", msg);
if self_ty.span.can_be_used_for_suggestions() {
self.maybe_suggest_impl_trait(self_ty, &mut diag);
if object_safe {
if !self.maybe_suggest_impl_trait(self_ty, &mut diag) && object_safe {
// Only emit this suggestion if the trait is object safe.
diag.multipart_suggestion_verbose(
label,
Expand Down Expand Up @@ -212,34 +211,25 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let parent_id = tcx.hir().get_parent_item(self_ty.hir_id).def_id;
// FIXME: If `type_alias_impl_trait` is enabled, also look for `Trait0<Ty = Trait1>`
// and suggest `Trait0<Ty = impl Trait1>`.
let (sig, generics, owner) = match tcx.hir_node_by_def_id(parent_id) {
let (sig, generics) = match tcx.hir_node_by_def_id(parent_id) {
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, generics, _), .. }) => {
(sig, generics, None)
(sig, generics)
}
hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(sig, _),
generics,
owner_id,
..
}) => (sig, generics, Some(tcx.parent(owner_id.to_def_id()))),
}) => (sig, generics),
_ => return false,
};
let Ok(trait_name) = tcx.sess.source_map().span_to_snippet(self_ty.span) else {
return false;
};
let impl_sugg = vec![(self_ty.span.shrink_to_lo(), "impl ".to_string())];
let mut is_downgradable = true;
let mut downgrade = false;
let is_object_safe = match self_ty.kind {
hir::TyKind::TraitObject(objects, ..) => {
objects.iter().all(|o| match o.trait_ref.path.res {
Res::Def(DefKind::Trait, id) => {
if Some(id) == owner {
// For recursive traits, don't downgrade the error. (#119652)
is_downgradable = false;
}
tcx.object_safety_violations(id).is_empty()
}
Res::Def(DefKind::Trait, id) => tcx.object_safety_violations(id).is_empty(),
_ => false,
})
}
Expand Down Expand Up @@ -267,11 +257,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
],
Applicability::MachineApplicable,
);
} else if is_downgradable {
// We'll emit the object safety error already, with a structured suggestion.
downgrade = true;
return true;
}
return downgrade;
return false;
}
for ty in sig.decl.inputs {
if ty.hir_id != self_ty.hir_id {
Expand All @@ -293,10 +281,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
if !is_object_safe {
diag.note(format!("`{trait_name}` it is not object safe, so it can't be `dyn`"));
if is_downgradable {
// We'll emit the object safety error already, with a structured suggestion.
downgrade = true;
}
} else {
let sugg = if let hir::TyKind::TraitObject([_, _, ..], _, _) = self_ty.kind {
// There are more than one trait bound, we need surrounding parentheses.
Expand All @@ -316,9 +300,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Applicability::MachineApplicable,
);
}
return downgrade;
return true;
}
downgrade
false
}

fn maybe_suggest_assoc_ty_bound(&self, self_ty: &hir::Ty<'_>, diag: &mut Diag<'_>) {
Expand Down
12 changes: 0 additions & 12 deletions tests/ui/traits/bound/not-on-bare-trait-2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ help: alternatively, use a trait object to accept any type that implements `Foo
|
LL | fn foo(_x: &(dyn Foo + Send)) {
| +++++ +
help: add `dyn` keyword before this trait
|
LL | fn foo(_x: dyn Foo + Send) {
| +++

error[E0782]: trait objects must include the `dyn` keyword
--> $DIR/not-on-bare-trait-2021.rs:11:11
Expand All @@ -39,10 +35,6 @@ help: alternatively, use a trait object to accept any type that implements `Foo`
|
LL | fn bar(x: &dyn Foo) -> Foo {
| ++++
help: add `dyn` keyword before this trait
|
LL | fn bar(x: dyn Foo) -> Foo {
| +++

error[E0782]: trait objects must include the `dyn` keyword
--> $DIR/not-on-bare-trait-2021.rs:11:19
Expand All @@ -58,10 +50,6 @@ help: alternatively, you can return an owned trait object
|
LL | fn bar(x: Foo) -> Box<dyn Foo> {
| +++++++ +
help: add `dyn` keyword before this trait
|
LL | fn bar(x: Foo) -> dyn Foo {
| +++

error: aborting due to 3 previous errors

Expand Down