Skip to content

Implement assist to add missing lifetime to function #17265

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

Closed
Closed
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
4 changes: 4 additions & 0 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ pub enum InferenceDiagnostic {
expr: ExprId,
expected: Ty,
},
FunctionMissingLifetime {
function: ExprId,
lifetime: LifetimeRef,
},
}

/// A mismatch between an expected and an inferred type.
Expand Down
19 changes: 16 additions & 3 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl ImplTraitLoweringState {
}
}

#[derive(Debug)]
// #[derive(Debug)]
pub struct TyLoweringContext<'a> {
pub db: &'a dyn HirDatabase,
resolver: &'a Resolver,
Expand All @@ -134,6 +134,8 @@ pub struct TyLoweringContext<'a> {
expander: RefCell<Option<Expander>>,
/// Tracks types with explicit `?Sized` bounds.
pub(crate) unsized_types: RefCell<FxHashSet<Ty>>,
/// Call this function to add a diagnostic to
missing_lifetime_callback: Option<RefCell<&'static mut dyn FnMut(LifetimeRef)>>,
}

impl<'a> TyLoweringContext<'a> {
Expand All @@ -158,6 +160,7 @@ impl<'a> TyLoweringContext<'a> {
type_param_mode,
expander: RefCell::new(None),
unsized_types: RefCell::default(),
missing_lifetime_callback: None,
}
}

Expand All @@ -174,6 +177,7 @@ impl<'a> TyLoweringContext<'a> {
impl_trait_mode,
expander: RefCell::new(expander),
unsized_types: RefCell::new(unsized_types),
missing_lifetime_callback: None,
..*self
};
let result = f(&new_ctx);
Expand All @@ -198,6 +202,10 @@ impl<'a> TyLoweringContext<'a> {
pub fn with_type_param_mode(self, type_param_mode: ParamLoweringMode) -> Self {
Self { type_param_mode, ..self }
}

pub fn with_lifetime_callback<F: Fn(LifetimeRef)>(self, callback: &'static mut F) -> Self {
Self { missing_lifetime_callback: Some(RefCell::new(callback)), ..self }
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -278,7 +286,7 @@ impl<'a> TyLoweringContext<'a> {
}
TypeRef::Reference(inner, lifetime, mutability) => {
let inner_ty = self.lower_ty(inner);
// FIXME: It should infer the eldided lifetimes instead of stubbing with static
// FIXME: It should infer the elided lifetimes instead of stubbing with static
let lifetime =
lifetime.as_ref().map_or_else(error_lifetime, |lr| self.lower_lifetime(lr));
TyKind::Ref(lower_to_chalk_mutability(*mutability), lifetime, inner_ty)
Expand Down Expand Up @@ -1377,7 +1385,12 @@ impl<'a> TyLoweringContext<'a> {
}
.intern(Interner),
},
None => error_lifetime(),
None => {
if let Some(add_diagnostic) = self.missing_lifetime_callback.as_ref() {
add_diagnostic.borrow_mut()(lifetime.clone());
}
error_lifetime()
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/hir/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ diagnostics![
MismatchedArgCount,
MismatchedTupleStructPatArgCount,
MissingFields,
FunctionMissingLifetime,
MissingMatchArms,
MissingUnsafe,
MovedOutOfRef,
Expand Down Expand Up @@ -370,6 +371,13 @@ pub struct RemoveUnnecessaryElse {
pub if_expr: InFile<AstPtr<ast::IfExpr>>,
}

#[derive(Debug)]
pub struct FunctionMissingLifetime {
pub file: HirFileId,
pub function: AstPtr<ast::Fn>,
pub lifetime: InFile<AstPtr<ast::Lifetime>>,
}

impl AnyDiagnostic {
pub(crate) fn body_validation_diagnostic(
db: &dyn HirDatabase,
Expand Down Expand Up @@ -626,6 +634,10 @@ impl AnyDiagnostic {
};
MismatchedTupleStructPatArgCount { expr_or_pat, expected, found }.into()
}
InferenceDiagnostic::FunctionMissingLifetime { function, lifetime } => {
// let file_id = function.
FunctionMissingLifetime { file_id: todo!(), function, lifetime }.into()
}
})
}
}
17 changes: 17 additions & 0 deletions crates/ide-assists/src/tests/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ fn main() {
)
}

#[test]
fn doctest_add_lifetime_to_function() {
check_doc_test(
"add_lifetime_to_function",
r#####"
fn print(s: &$0'a str) {
println!("{s}");
}
"#####,
r#####"
fn print<'a>(s: &'a str) {
println!("{s}");
}
"#####,
)
}

#[test]
fn doctest_add_lifetime_to_type() {
check_doc_test(
Expand Down
Loading