-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
cmd/compile: instantiation cycle in closure function created by method of generic type #50215
Comments
I think the error is correct. |
@mdempsky thank you, i got it. it' may better be reported as infinite instantiation instead of cycle ? |
Could you explain how this follows from the previous part? There are no method calls for Similar code in (say) Rust compiles just fine: struct StringParser<T> {
f: Box<dyn Fn(String) -> Option<T>>
}
impl<T> StringParser<T> {
fn to_list_parser(self) -> StringParser<Vec<T>> {
todo!()
}
}
fn parse_list<T>(p: StringParser<T>, s: String) -> Option<Vec<T>> {
todo!()
}
fn main() {
let p = StringParser { f: Box::new(|s| str::parse::<i64>(&s).ok()) };
(p.to_list_parser().f)("1,2,3".to_string());
} |
Go allows method calls to happen dynamically through interface assertions or reflection. As a result, if a type is instantiated, all of its methods must be instantiated too, even if not statically called. There are certainly cases where we could determine this isn't actually needed, but that would complicate the language specification. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://go.dev/play/p/l9gzFUOBT5W?v=gotip
ToListParser
is fine to compile,, but the compiler reports errors onStringParser.ToListParser
, :as i can see,
[]T
is depends onT
, rather thanStringParser[T]
, there shouldn't be a cycle.I don't know whether this is a bug, or go doesn't allow this usage ?
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: