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

checker,cgen: fix duplicates in generic sumtype #20936

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

MCausc78
Copy link
Contributor

@MCausc78 MCausc78 commented Mar 1, 2024

  1. Fix cgen error for Name[int](123), when type Name[T] = string | int | T #19159
  2. Add tests
PS D:\Games\Proekti\V\vl> type test.v
@[on_duplicate: 'thats sentinel lol'; notice_if_duplicate]
type Name[T] = string | int | T

fn main() {
        _ := Name[int](123)
}
PS D:\Games\Proekti\V\vl> .\v2 run test.v
test.v:5:7: notice: duplicate type: thats sentinel lol
    3 |
    4 | fn main() {
    5 |     _ := Name[int](123)
      |          ~~~~~~~~~~~~~~
    6 | }

Note: you can control, whether to raise a error, notice or ignore if user used type, which is present as T in generic sumtype

vlib/v/checker/checker.v Outdated Show resolved Hide resolved
Comment on lines 3095 to 3099
if print_notice {
c.note('duplicate type: ${msg}', node.pos)
} else {
c.error('duplicate type: ${msg}', node.pos)
}
Copy link
Member

Choose a reason for hiding this comment

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

hm, there is no existing precedent for using an attribute for toggling between an error and a notice 🤔. I am not sure if it is worth the complexity.
Would not either just a notice, or either just an error be simpler?

Copy link
Contributor Author

@MCausc78 MCausc78 Mar 1, 2024

Choose a reason for hiding this comment

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

sometimes this is bad idea to give error. Consider that:
firstapi:

pub struct Row[T] {
pub mut:
	// children components
	components []Component[T]
}

@[on_duplicate: 'T is present in sumtype']
@[notice_if_duplicate]
pub type Component[T] = Row[Button]
	| Button
	| StringSelect
	| TextInput
	| T

secondapi:

struct Default[T] {
	v T
}

pub fn c[T]() Component[T] {
	return Component[T](Default[T]{}.v)
}

user (not dev of firstapi and secondapi packages), not knowing that e.g. TextInput is present in Component type, uses c[TextInput](), and gets an notice, instead of hard error. That also would allow deprecating duplicates in generic sumtypes.
Yet one example:

struct Undefined {}

@[on_duplicate: 'Sentinels are internal']
pub type UndefinedOr[T] = Undefined | T

That would give a error if user will do UndefinedOr[Undefined].

Copy link
Member

Choose a reason for hiding this comment

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

@medvednikov what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @spytheman here. This complicates the language and gives too many options. I'd just stick with one option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed attribute and made error by default

Copy link
Member

Choose a reason for hiding this comment

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

There are still 2 attributes:
ignore_generic_duplicates and on_generic_duplicate.

I do not know how to document/explain that in doc/docs.md.

@MCausc78
Copy link
Contributor Author

Stale

@MCausc78 MCausc78 closed this Jul 29, 2024
@medvednikov medvednikov reopened this Jul 29, 2024
@medvednikov
Copy link
Member

Sorry, we'll figure something out to merge this.

@enghitalo
Copy link
Contributor

I don't think using attributes or custom error messages is a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cgen error for Name[int](123), when type Name[T] = string | int | T
4 participants