Skip to content

Apply mismatched-lifetime-syntaxes to trait and extern functions #142129

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 33 additions & 7 deletions compiler/rustc_lint/src/lifetime_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,45 @@ impl<'tcx> LateLintPass<'tcx> for LifetimeSyntax {
_: rustc_span::Span,
_: rustc_span::def_id::LocalDefId,
) {
let mut input_map = Default::default();
let mut output_map = Default::default();
check_fn_like(cx, fd);
}

for input in fd.inputs {
LifetimeInfoCollector::collect(input, &mut input_map);
#[instrument(skip_all)]
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, ti: &'tcx hir::TraitItem<'tcx>) {
match ti.kind {
hir::TraitItemKind::Const(..) => {}
hir::TraitItemKind::Fn(fn_sig, _trait_fn) => check_fn_like(cx, fn_sig.decl),
hir::TraitItemKind::Type(..) => {}
}
}

if let hir::FnRetTy::Return(output) = fd.output {
LifetimeInfoCollector::collect(output, &mut output_map);
#[instrument(skip_all)]
fn check_foreign_item(
&mut self,
cx: &LateContext<'tcx>,
fi: &'tcx rustc_hir::ForeignItem<'tcx>,
) {
match fi.kind {
hir::ForeignItemKind::Fn(fn_sig, _idents, _generics) => check_fn_like(cx, fn_sig.decl),
hir::ForeignItemKind::Static(..) => {}
hir::ForeignItemKind::Type => {}
}
}
}

fn check_fn_like<'tcx>(cx: &LateContext<'tcx>, fd: &'tcx hir::FnDecl<'tcx>) {
let mut input_map = Default::default();
let mut output_map = Default::default();

report_mismatches(cx, &input_map, &output_map);
for input in fd.inputs {
LifetimeInfoCollector::collect(input, &mut input_map);
}

if let hir::FnRetTy::Return(output) = fd.output {
LifetimeInfoCollector::collect(output, &mut output_map);
}

report_mismatches(cx, &input_map, &output_map);
}

#[instrument(skip_all)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
Self: Sized,
{
type I: for<'a> FamilyLt<'a>;
fn inject(_: &()) -> <Self::I as FamilyLt>::Out;
fn inject(_: &()) -> <Self::I as FamilyLt<'_>>::Out;
}

impl<T: 'static> Inject for RefMutFamily<T> {
Expand Down
39 changes: 39 additions & 0 deletions tests/ui/lifetimes/mismatched-lifetime-syntaxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,45 @@ mod diagnostic_output {
}
}

/// Trait functions are represented differently in the HIR. Make sure
/// we visit them.
mod trait_functions {
#[derive(Copy, Clone)]
struct ContainsLifetime<'a>(&'a u8);

trait TheTrait {
fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime;
//~^ ERROR lifetime flowing from input to output with different syntax

fn method_implicit_ref_to_implicit_path(&self) -> ContainsLifetime;
//~^ ERROR lifetime flowing from input to output with different syntax
}

impl TheTrait for &u8 {
fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime {
//~^ ERROR lifetime flowing from input to output with different syntax
ContainsLifetime(v)
}

fn method_implicit_ref_to_implicit_path(&self) -> ContainsLifetime {
//~^ ERROR lifetime flowing from input to output with different syntax
ContainsLifetime(self)
}
}
}

/// Extern functions are represented differently in the HIR. Make sure
/// we visit them.
mod foreign_functions {
#[derive(Copy, Clone)]
struct ContainsLifetime<'a>(&'a u8);

extern "Rust" {
fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime;
//~^ ERROR lifetime flowing from input to output with different syntax
}
}

/// These usages are expected to **not** trigger the lint
mod acceptable_uses {
#[derive(Copy, Clone)]
Expand Down
67 changes: 66 additions & 1 deletion tests/ui/lifetimes/mismatched-lifetime-syntaxes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,70 @@ help: one option is to consistently use `'a`
LL | fn multiple_outputs<'a>(v: &'a u8) -> (&'a u8, &'a u8) {
| ++ ++

error: aborting due to 34 previous errors
error: lifetime flowing from input to output with different syntax can be confusing
--> $DIR/mismatched-lifetime-syntaxes.rs:230:45
|
LL | fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime;
| ^^^ ---------------- the lifetime gets resolved as `'_`
| |
| this lifetime flows to the output
|
help: one option is to remove the lifetime for references and use the anonymous lifetime for paths
|
LL | fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime<'_>;
| ++++

error: lifetime flowing from input to output with different syntax can be confusing
--> $DIR/mismatched-lifetime-syntaxes.rs:233:49
|
LL | fn method_implicit_ref_to_implicit_path(&self) -> ContainsLifetime;
| ^^^^^ ---------------- the lifetime gets resolved as `'_`
| |
| this lifetime flows to the output
|
help: one option is to remove the lifetime for references and use the anonymous lifetime for paths
|
LL | fn method_implicit_ref_to_implicit_path(&self) -> ContainsLifetime<'_>;
| ++++

error: lifetime flowing from input to output with different syntax can be confusing
--> $DIR/mismatched-lifetime-syntaxes.rs:238:45
|
LL | fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime {
| ^^^ ---------------- the lifetime gets resolved as `'_`
| |
| this lifetime flows to the output
|
help: one option is to remove the lifetime for references and use the anonymous lifetime for paths
|
LL | fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime<'_> {
| ++++

error: lifetime flowing from input to output with different syntax can be confusing
--> $DIR/mismatched-lifetime-syntaxes.rs:243:49
|
LL | fn method_implicit_ref_to_implicit_path(&self) -> ContainsLifetime {
| ^^^^^ ---------------- the lifetime gets resolved as `'_`
| |
| this lifetime flows to the output
|
help: one option is to remove the lifetime for references and use the anonymous lifetime for paths
|
LL | fn method_implicit_ref_to_implicit_path(&self) -> ContainsLifetime<'_> {
| ++++

error: lifetime flowing from input to output with different syntax can be confusing
--> $DIR/mismatched-lifetime-syntaxes.rs:257:45
|
LL | fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime;
| ^^^ ---------------- the lifetime gets resolved as `'_`
| |
| this lifetime flows to the output
|
help: one option is to remove the lifetime for references and use the anonymous lifetime for paths
|
LL | fn implicit_ref_to_implicit_path(v: &u8) -> ContainsLifetime<'_>;
| ++++

error: aborting due to 39 previous errors

2 changes: 1 addition & 1 deletion tests/ui/traits/associated_type_bound/hrtb-associated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait Provides<'a> {
pub trait Selector: for<'a> Provides<'a> {
type Namespace: PartialEq + for<'a> PartialEq<<Self as Provides<'a>>::Item>;

fn get_namespace(&self) -> <Self as Provides>::Item;
fn get_namespace(&self) -> <Self as Provides<'_>>::Item;
}

pub struct MySelector;
Expand Down
Loading