Skip to content

Use related spans for "implement abstract class" errors #48030

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

Conversation

FlyingPumba
Copy link
Contributor

@gabritto Here is the PR again that was reverted. I didn't change the message. I don't actually remember how to do it correctly (it's a PR from one year ago after all 😅).

Old PR: #42546
Fixes #32848

Example compiling the following code.

abstract class A {
	abstract b(): void;
	abstract c(): void;
}

class B extends A { }

Error message before:

hello.ts:6:7 - error TS2515: Non-abstract class 'B' does not implement inherited abstract member 'b' from class 'A'.

6 class B extends A { }
~

hello.ts:6:7 - error TS2515: Non-abstract class 'B' does not implement inherited abstract member 'c' from class 'A'.

6 class B extends A { }
~

Found 2 errors.

Error message after:

hello.ts:6:7 - error TS18036: Non-abstract class 'B' does not implement all abstract members of 'A'

6 class B extends A { }
~

hello.ts:6:7
6 class B extends A { }
~
Non-abstract class 'B' does not implement inherited abstract member 'b' from class 'A'.
hello.ts:6:7
6 class B extends A { }
~
Non-abstract class 'B' does not implement inherited abstract member 'c' from class 'A'.

Found 1 error.

cc: @DanielRosenwasser

@gabritto
Copy link
Member

thanks a lot, I'll try not to mess things up with git this time

@gabritto gabritto self-assigned this Feb 25, 2022
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks reasonable. Just a couple of minor questions.

It's been a really long time since we last reviewed this, so if you're not interested in bringing this PR up-to-date, I understand.

@@ -7285,5 +7285,9 @@
"A 'return' statement cannot be used inside a class static block.": {
"category": "Error",
"code": 18041
},
"Non-abstract class '{0}' does not implement all abstract members of '{1}'": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"Non-abstract class '{0}' does not implement all abstract members of '{1}'": {
"Non-abstract class '{0}' does not implement all inherited abstract members of '{1}'": {

Comment on lines 5 to 6
Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2.code,
Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1.code,
Copy link
Member

@sandersn sandersn Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these needed anymore now that they're related spans? Is it even possible to fix just one missing member in the first place?

Edit: It's not, although I don't know how related spans display, so it's worth finding out whether the codefix can be invoked from a related span.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, of course. The related spans are not really related spans, just elaborations. So these are definitely not needed.

1. Lazily create top-level error instead of saving related errors in a
list. addRelatedInfo already creates a list.
2. Remove unused errors in codefix trigger.
3. Related span points to base properties instead.
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional things I noticed. I fixed them after I got this PR stuck in my head overnight.

Comment on lines 5 to 6
Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2.code,
Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1.code,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, of course. The related spans are not really related spans, just elaborations. So these are definitely not needed.

@@ -39533,12 +39534,16 @@ namespace ts {
}

if (derivedClassDecl.kind === SyntaxKind.ClassExpression) {
error(derivedClassDecl, Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1,
const err = createDiagnosticForNode(derivedClassDecl,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use the base node for the related span

@@ -39494,6 +39494,8 @@ namespace ts {

// NOTE: assignability is checked in checkClassDeclaration
const baseProperties = getPropertiesOfType(baseType);
const derivedClassDecl = getClassLikeDeclarationOfSymbol(type.symbol)!;
const inheritedAbstractMemberNotImplementedErrors: Diagnostic[] = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of saving a list, create an error lazily and push onto that. It already has a list internally.

@sandersn sandersn merged commit 2b57a88 into microsoft:main Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Use related error spans for "implement abstract class" errors
4 participants